• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

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.
0
dhite99
Asked:
dhite99
  • 2
2 Solutions
 
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
 
McNeticCommented:
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
 
Maciej SsysadminCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now