Solved

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

Posted on 2010-09-20
4
962 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

717 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