Solved

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

Posted on 2010-09-20
4
952 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Streaming 6 73
How to update  and reset admin password for Linux 5 55
installing docker on ubuntu 6 29
EXCHANGE. LINUX, SYMANTEC MESSAGING GATEWAY 2 18
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

777 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