Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selective rm? 2

Status
Not open for further replies.

sbrews

Technical User
Jun 11, 2003
413
US
I have a directory that looks similar to this:

These files are located in: /some/location

drwxr-xr-x 23 someuser somegrp 4096 Feb 26 13:26 .
drwxr-xr-x 5 root system 256 May 18 2006 ..
-rw-r--r-- 1 someuser somegrp 1436 Nov 01 13:02 .eai
-rw------- 1 someuser somegrp 3276 Jan 23 2006 .old
-rwxr-xr-x 1 someuser somegrp 1290 May 19 2004 .someuser
-rw-r--r-- 1 someuser somegrp 30 Oct 06 10:43 .dxfcreated
drwxr-xr-x 2 someuser somegrp 4096 Feb 26 13:22 .ps
-rwxr-xr-x 1 someuser somegrp 729 Dec 05 08:27 .sde
-rw-r----- 1 someuser somegrp 0 May 16 2006 .sh_history
drwxrwxrwx 14 root system 4096 Mar 02 07:00 .snapshot
drwxr-xr-x 2 someuser somegrp 4096 Feb 26 13:22 Scripts
drwxr-xr-x 5 someuser somegrp 73728 Mar 02 03:57 LOGS
drwxr-xr-x 10 someuser somegrp 4096 Feb 26 13:25 SQL_LOGS
drwxr-xr-x 5 someuser somegrp 4096 Feb 26 13:22 dev
drwxr-xr-x 7 someuser somegrp 4096 Feb 26 13:22 utilities

What I want to be able to do is a 'rm -rf' in /some/location' to remove all files and directories EXCEPT .snapshot (and its subdirs/files). I've been playing with find and its various options, but havent had much luck in deleting everything BUT the .snapshot.

Have any of you gurus done something like this? If so, how did you do it?
 
how about:

Code:
find /some/location -type f | grep -v .snapshot | xargs rm -f {} \;
find /some/location -depth -type d | grep -v .snapshot | xargs  rmdir {} \;

That's two lines, each starting with "find".

- Rod


IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

Wish you could view posts with a fixed font? Got Firefox & Greasemonkey? Give yourself the option.
 
How about:

Code:
find /some/location -name '.snapshot' -prune -o -exec rm -rf {} \;

To check to see if you are listing the files you need first, you can change the -exec and everything past it to -print.

Regards,
Chuck
 
cspilman

that was the find command I basically started with. The problem with it is that even though the .snapshot directory was pruned, when find finds the top level directory (./), the recursive rm "re-finds" the .snapshot directory and tries to remove it anyway.

(didnt know I could use the word "find" in so many different ways. :> )

Rodknowlton

While I havent tested your solution (yet), it looks like will do the trick.

########

Here is what I came up with (subject to further testing):

Take care of any/all files in /some/location/:

find . -maxdepth 1 -type f -exec rm -f {} \;

Once that is done, take care of all the subdirs - excluding .snapshot:

find . -maxdepth 1 -type d ! -name .snapshot ! -regex ^. -exec rm -rf {} \;

The ! (negative) regex will exclude the . that find spits out
The ! (negative) -name .snapshot will exclude the .snapshot dir.
Maxdepth of 1 - dont need to travel the tree because I am going to do a recursive remove of the top level dirs in /some/location.

Note: the find command above is/will be issued from the /some/location directory - hence the "." and not the whole path.

 
Try it without using the relative pathname.

I was able to successfully remove all files and directories under a test directory while omitting .snapshot directory by using the absolute path in my find statement.

Regards,
Chuck
 
curses - while maxdepth is a valid option on some find commands, its not valid on the AIX find command.

cspilman - I will give that a try.
 
I was testing with relative pathnames and I came up with this that should work for you while in the current directory:

Code:
find . -name '.snapshot' -prune -o ! -name . -exec rm -rf {} \;



Regards,
Chuck
 
This
Code:
find . -type d ! -name . ! -name .snapshot -exec rm -rf {} \;

should work also (typed, untested), but I'd try it with -ls or -print first ;-).


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top