2

I'm trying to make a zip file of all the files in a specific directory which were modified in the last x minutes.

Here's my command:

find "/path/to/dir" -mmin -30 -not -name ".*" -exec zip -r "testfile.zip" "{}" \+

If I make the modify time small enough, then it correctly identifies the one file that I recently changed.

The problem is... if I increase the minimum modify time to, say, 50 (it keeps changing), then the zip file adds all of the files in /path/to/dir. Even though all the files were definitely not changed.

So somehow, there is a specific file that is causing the whole find command to blow up. I looked through all the recently modified files (within last day) for a file with spaces in it or something, but I didn't find any. And it shouldn't matter, since I'm quoting the braces, right?

What else could be causing this weird behavior?

  • 2
    Was the directory recently modified? Try putting -not -type d in there so that find won't match the directory itself. – Mark Plotnick Dec 2 '14 at 18:41
3

By default, find includes everything in its search: directories, files, and symlinks.

find "/path/to/dir" -mmin -30 -not -name ".*" -exec zip -r "testfile.zip" "{}" \+

If /path/to/dir was modified in the last 30 minutes, it will pass all the tests, and zip, since it was given the -r option, will add the directory and everything under it to the archive.

You can approach this in a few ways, using either (or both) of the following:

  • Don't give zip the -r option. You're only interested in recently-modified files rather than every file that is in a recently-modified directory.
  • Add the -not -type d test to the find expression, so that it doesn't match directories.
find "/path/to/dir" -not -type d -mmin -30 -not -name ".*" -exec zip "testfile.zip" "{}" \+
  • Thanks again - leaving out the '-r' turned out to be the better option. I didn't realize that the files would still retain their parent folder structure with that option turned off. – AmadeusDrZaius Dec 2 '14 at 19:55

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.