Find but exclude directories

Hello,

I have a line in my script to find the files changed in the last 24 hours. It is as below:

find /home/hary -type f -mtime -1

I now want to exclude a directory named "/home/hary/temp/cache" from the above find command. How do I add it to my script?

Any help is appreciated.

Thanks,
Hary

add this:

   ! -name '/hary/temp/cache/*' 

With some versions of find you can use the path option:

find /home/hary -path '/home/hary/temp/cache/*' \
-prune -o -type f -mtime -1 -print

Otherwise you could use something like this:

find /home/hary -name cache -exec test {} = /home/hary/temp/cache \; \
-prune -o -type f -mtime -1 -print

Thanks radoulov! It works but i have a smaill problem with that. I tan this command as yu mentioned:

root@nlvmas711:PROD:~> find /u01/tomcat4/webapps -path '/u01/tomcat4/webapps/pwp/WEB-INF/cache' -prune -o -type f -mtime -1

/u01/tomcat4/webapps/pwp/WEB-INF/cache

It does not return me any files under the "cache" directory. However,. it returns me the excluded directory name itself. What do I need to add so that it goes to command prompt when it finds nothing.

You're missing the print switch (see my post).

You can check the wholename switch also, if your find supports it.

Another way...

find /home/hary -name "cache" -prune -o -type f -mtime -1 -print

Thanks Guys! It works now.

Consider the following:

$ ls -lR
.:
total 0
drwxr-xr-x+ 2 sysadmin None 0 Sep 11 19:56 cache
-rw-r--r--  1 sysadmin None 0 Sep 11 19:49 file_ok
drwxr-xr-x+ 2 sysadmin None 0 Sep 11 19:57 other
drwxr-xr-x+ 3 sysadmin None 0 Sep 11 19:49 temp

./cache:
total 0
-rw-r--r-- 1 sysadmin None 0 Sep 11 19:49 file_ok

./other:
total 0
-rw-r--r-- 1 sysadmin None 0 Sep 11 19:57 cache

./temp:
total 0
drwxr-xr-x+ 2 sysadmin None 0 Sep 11 19:49 cache
-rw-r--r--  1 sysadmin None 0 Sep 11 19:49 file_ok

./temp/cache:
total 0
-rw-r--r-- 1 sysadmin None 0 Sep 11 19:49 file_KO
$ find -name "cache" -prune -o -type f -mtime -1 -print         
./file_ok
./temp/file_ok
$ find -path './temp/cache/*' -prune -o -type f -mtime -1 -print
./cache/file_ok
./file_ok
./other/cache
./temp/file_ok

Good point radoulov...yep that find will give incorrect results if the cache directory is found under the include list of directories. In that case the find you posted will do the job nicely. If the find on the O/P's system has the -path option then this would work just as well.

find /home/hary ! -path "/home/hary/temp/cache/*" -type f -mtime -1

Good point,
definitely cleaner.