1

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.

2 Answers 2

1

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.

3
  • 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, 2012 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, 2012 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, 2012 at 21:56
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .