I have a find command that looks for files that was modified recently and outputs the date

find /path/on/server -mtime -1 -name '*.js' -exec ls -l {} \;

I would like it to exclude any deeply nested folder that matches a certain pattern e.g. there are a number of folders that have a "statistics" directory and ".svn" directories. So i'd like to be able to say if the file that was modified yesterday is in a folder named statistics ignore it. Or perhaps not search for files in those folders at all.


You can use the -path and -prune command line parameters to do this

find /path/on/server -mtime -1 -o -path '*.svn' -prune -o -path '*statistics' \ 
-prune -o -exec ls -l {} \;

This has the advantage of not searching the directories that you want excluding.

Also note that your ls -l may not be doing what you expect as it will list the entire contents of directories that are passed to it so you will get some files listed multiple times.

From the comments ls -ld would be better.

  • Good note on ls, but you should include the alternatives such as ls -ld and simply -ls to find. Both should work since the question is tagged Linux. – jordanm Jul 6 '12 at 21:22
  • @jordanm: I didn't think it necessary as I haver previously supplied the OP with a better solution serverfault.com/a/405117/9517 – user9517 Jul 6 '12 at 21:38
  • I tried this and it listed all the files (it didn't restrict the find to the modified time of 1 day ago) – user40570 Jul 9 '12 at 21:56

Use the negate modifier !. For example like this find /path/on/server -mtime -1 -name '*.js' -o -name '!.svn' -o -name '!statistics' -exec ls -l {} \;.

The clue here is -o (meaning simply OR) and ''s around the exclamation mark as the shell (bash or similar) use exclamation marks for special commands.

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.