I want to zip the directories, sub directories, files in main and sub directories from today till 150 days ago.

I only consider the date of the directories where I am running the command, no matter what the date of files and sub directories.

I used the command

find . -mtime -151 | xargs  zip tnsd3801_Postfeb16_7106.zip

to create the zip of directories. But it is creating the zip of files/sub directories which are only modified between 150 day from today. Could someone help me out.

  • Do you have directories within directories (e.g., dir1/dir17)?  If dir1 was modified 140 days ago, but dir1/dir17 was modified 160 days ago, do you want the contents of dir17 to be zipped, or just the first-level files in dir1? – Scott Jul 18 '15 at 9:25
  • I want the dir17 also dir16, dir14 .. file1, file2 .. even though there is no change in them. I want the whole main directory even if a single file update has been done. – mamatha Jul 25 '15 at 8:41

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 dir11, dir12, dir13, dir14, dir15, dir16, and dir17), and everything in and under dir21, dir23, dir25, and dir27, 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, restricts the find to objects whose modification date is 150 days ago or less.

    When I read your question, my first thought was -mtime -150.  Then I saw that you were trying -mtime -151, 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.  So,

    find . -type d -mtime -151 –prune

    on the example directory structure that I showed above should report


    It doesn’t list ./dir1/dir12, ./dir1/dir14, and./dir1/dir16, because it isn’t searching ./dir1, because it was modified less than 150 days ago.

  • Use -print0 and xargs -0 to handle directory names that might have whitespace characters (spaces, tabs, and newlines) in their names.
  • Then invoke zip with the -r option 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 myarc.zip containingfile1, file2, file3, and file4, because 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.