The more I read this question, the less sure I am that I understand it.
I’m going to assume that it is as follows:
You have a directory tree that looks like
├───dir1 ⋯ Modified 140 days ago.
│ ├───dir11 ⋯ Modified 160 days ago.
│ ├───dir12 ⋯ Modified 140 days ago.
│ ├───dir13 ⋯ (Don’t care.)
│ ├───dir14 ⋯ (Don’t care.)
│ ├───dir15 .
│ ├───dir16 .
│ ├───dir17 .
│ └─── ⋮
└───dir2 ⋯ Modified 160 days ago.
├───dir21 ⋯ Modified 140 days ago.
├───dir22 ⋯ Modified 160 days ago.
├───dir23 ⋯ Modified 140 days ago.
├───dir24 ⋯ Modified 160 days ago.
├───dir25 ⋯ Modified 140 days ago.
├───dir26 ⋯ Modified 160 days ago.
├───dir27 ⋯ Modified 140 days ago.
… and you want to archive
everything in and under
dir1 (including everything
in and under
and everything in and under
but nothing else in or under
dir2. (Because you want everything
in and under any directory that’s been modified in the past 150 days.)
You can do this with
find . -type d -mtime -151 -prune -print0 | xargs -0 zip -r tnsd3801_Postfeb16_7106.zip
find . -type d, obviously,
finds directories in and under the current directory.
find … -mtime -151, as you know,
find to objects whose modification date is 150 days ago or less.
When I read your question, my first thought was
Then I saw that you were trying
and I thought, yeah, that makes sense. Still, you might want to try
find . -type d -mtime -150 > dirs150
find . -type d -mtime -151 > dirs151
diff dirs150 dirs151
to make sure which one you want.
find … -prune says,
when you find something that meets the criteria specified up to this point,
don’t recurse into this directory,
but just go on to the next object at this level.
find . -type d -mtime -151 –prune
on the example directory structure that I showed above should report
It doesn’t list
because it isn’t searching
because it was modified less than 150 days ago.
xargs -0 to handle directory names
that might have whitespace characters (spaces, tabs, and newlines)
in their names.
- Then invoke
zip with the
to cause it to search the specified directories recursively.
Note: If you have a lot of directories, i.e., so many
that the list of their names is hundreds of thousands of characters long,
xargs might need to execute
zip a few times.
This shouldn’t be a problem, as
zip myarc file1 file2
zip myarc file3 file4
should result in
zip normally adds to existing archives,
rather than clobbering them and overwriting them.
But note, by the same argument,
that you should ensure that
tnsd3801_Postfeb16_7106.zip does not exist
before you issue the above command,
unless you want to add to its pre-existing contents.