How to use linux find command to exclude a specific directory when using with rm


I have a find command that polices a directory and removes files older than 7 days. The command is as follows:

find /documents -name "*" -follow -mtime +7 -exec rm {} \;

The situation is that I need to exclude one single specific subdirectory off of /documents, for example, /documents/software. All other files not in the "software" subdirectory are eligible for removal.

I don't have to use the find command if there is another way to accomplish what is needed.

Thanks in advance for your help.
dhite99Asked:
Who is Participating?
 
Maciej SConnect With a Mentor sysadminCommented:
I forgot about \; at the end. Correct version below.
find -L /documents/ -mtime +7 -not -path "/documents/software/*" -type f -exec rm {} \;

Open in new window

0
 
nognewCommented:
Hi dhite99!

Please consider the following one liner:

for i in `ls -d *`; do if [ "$i" != "software" ]; then find $i -mtime +7 -exec rm {} \;; fi;  done

you need to be in /documents before you start a command.
Regards,
t.
0
 
McNeticConnect With a Mentor Commented:
To use find, you can use the option -path ./documents/software -prune, which means to ignore the whole documents/software directory. Please test before applying with rm ;-)
0
 
Maciej SsysadminCommented:

find -L /documents/ -mtime +7 -not -path "/documents/software/*" -type f -exec rm {}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.