LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2010, 04:29 AM   #1
supulton
LQ Newbie
 
Registered: Feb 2010
Posts: 21

Rep: Reputation: 0
delete all except three specified directories


How would I delete everything in a directory except for three specified directories?

So far I discovered I can omit the directories like so in a find search:
Code:
find . -type d \( -name media -o -name images -o -name backups \) -prune -o -print
But if I pass the '-delete' parameter, I get the error:

Code:
find: The -delete action atomatically turns on -depth, but -prune does nothing when -depth is in effect.  If you want to carry on anyway, just explicitly use the -depth option.
Any takers?
 
Old 07-29-2010, 05:21 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Hi supulton,

Is this OKay ?

Code:
ls | sed 's/test1\|test2\|test3//' | xargs rm -r -f
where test1,test2 and test3 are the directories which needs to be preserved from deletion..
 
Old 07-29-2010, 05:26 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Quote:
Originally Posted by supulton View Post
Any takers?
Pipe 'find' output (not 'ls'!) through 'xargs'.
 
Old 07-29-2010, 05:34 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

A little warning:

If media, images and/or backups are in a subdirectory they will be removed even if you "exclude" them!!

I.e.:
You are standing here:

/temp

Directory structure looks like this:

/temp/dira
/temp/dira/images

Even though you exclude images, its parent (dira) is _not_ excluded and find will pass this directory to rm -r/delete and all (including all subdirs) will be removed.

Hope this helps.
 
Old 07-29-2010, 05:36 AM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by unSpawn View Post
Pipe 'find' output (not 'ls'!) through 'xargs'.
It works both ways..As we are using -r switch in rm command

In fact if we use find instead of ls we get following warnings, although it performs the required task

Code:
[vinay@localhost workspace]$ find . | sed 's/test1\|test2\|test3//' | xargs rm -r -f
rm: cannot remove directory `.'
rm: cannot remove directory `./'
rm: cannot remove directory `./'
rm: cannot remove directory `./'
 
Old 07-29-2010, 06:12 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Bash 4
Code:
#!/bin/bash
shopt -s globstar
for dir in **/; do  
  case "$dir" in
   *dir1*|*dir2*|*dir3* ) continue;;
   *) 
       echo rm -rf "$dir"
  esac
done
 
Old 07-29-2010, 06:19 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Use the real path as in 'find $PWD -args' or 'find `readlink -f .` -args' instead? BTW I'm saying "not ls" since these commands may or may not work in similar ways when executed from the command line as 'ls' often will be set as an alias. Besides 'find' output has not problem with IFS whitespace ("-print0" vs 'ls --quoting-style=c') and is infinitely more customizable without requiring help from other binaries or scripts.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Safe to delete these directories; v. chkrootkit Skillz Linux - Security 3 09-04-2007 01:12 PM
How to delete files in many directories? pandronic Linux - Newbie 3 10-23-2006 10:20 AM
How can i delete Directories and Subdirectories? OrganicX Linux - Newbie 15 03-05-2003 09:48 PM
How does one delete directories with content as the SU? Ferrell Ramey Linux - General 2 08-22-2002 08:16 PM
Cant delete directories/files skopje909 Linux - General 2 11-07-2001 06:59 PM


All times are GMT -5. The time now is 08:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration