Solved

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

Posted on 2010-09-20
4
946 Views
Last Modified: 2012-05-10

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
Comment
Question by:dhite99
  • 2
4 Comments
 
LVL 8

Expert Comment

by:nognew
ID: 33716627
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
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 150 total points
ID: 33716842
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
 
LVL 23

Expert Comment

by:Maciej S
ID: 33717674

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

Open in new window

0
 
LVL 23

Accepted Solution

by:
Maciej S earned 350 total points
ID: 33717686
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now