Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I delete all but one directory in Linux?

I have a series of boxes (over 1,000), that have the same directory that has been used over time as a dumping ground for various 'stuff', and now I need to have them cleaned up, save one sub-directory. What I need to know, is there a way to create a script that will allow any user to delete everything (hidden, directories, links, etc.) from a particular directory, except one sub-directory.


Dave's Answer:

This is actually reasonably straightforward if you remember that anywhere you can use a shell expansion variable you can also invoke a script, simple or complex. So, if we were to tackle this basic Unix/Linux scripting task without any special requirements, we'd do this:

rm -rf *

Or, a safer way to do this might be to use:

find . -type d -print | xargs /rm -rf

this would ultimately do the same thing, of course.

To omit one directory while removing everything else, however, the basic logic is to list all the files and directories in the specified common parent directory, but "screen out" the one we'd like to preserve. This can be done with:

ls * | grep -v SAVETHISDIRECTORY

(well, if you have files or directories with spaces in their names you have a little bit more work you'd need to do, but remember that sed or tr can temporarily change spaces to another character, let you filter out the directory to save, then change things back)

Using the preferred shell notation of $( ) for subshell commands (rather than the more traditional backticks), we're now looking at a base command of:

rm -rf $(ls * | grep -v SAVETHISDIRECTORY)

See where I'm heading with this?

Now let's make it a bit more flexible by dropping it into a for loop:

savedir="SAVETHISDIRECTORY"
for dirname in $(ls * | sed 's/ /---/g' | grep -v "$savedir")
do
  remove=$(echo $dirname | sed 's/---/ /g')
  echo "removing file or directory $remove"
  rm -rf "$remove"
done

Now you can see where I'm going with all of this.

The other half of this challenge is to distribute the script onto the many, many boxes you have, and for that I'm wondering if you have a list of every system hostname / IP address? If so, you could create a temporary mount point directory on the master system, then iteratively mount - process - unmount each system until you'd gone through them all. Something vaguely like this:

mountpoint="/home/taylor/mount.point"
systemnamelist="/etc/clientboxes.txt"

for name in $(cat $systemnamelist)
do
  echo "Attempting to mount $name"
  mount $name $mountpoint
  echo " .. processing directory"
  code goes here to clean out directory
  umount $name
  echo " .. done"
done

That might not be exactly what you need, but it should certainly help you get going in the right direction.

Good luck!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Wouldn't it be rather simpler to just move the directory you want to keep to /tmp, do a quick rm -rf * and then move it back again?

;-)

Posted by: Paul Howie at December 12, 2005 9:12 AM

Yeah, and theoretically you could "tar" the directory up, remove everything, and "untar" it, but where's the challenge in a simple solution, Paul? :-)

Posted by: Dave Taylor at December 12, 2005 4:14 PM

This also can be use to delete all dir except savedir

rm -rf $(find . -type d|grep -v savedir)

Posted by: govind songara at June 23, 2006 12:39 AM

Even more elegant solution:

$ shopt -s extglob
$ rm -rf !(savedir)

you Korn shell users can ommit the first line.

Posted by: Octavio at September 24, 2007 9:53 PM

$ rm -rf `find * -type d -prune -o -type f ! -name 'myfile.cfg'`

In English : Find all directory(do not go inside that directory) or file which name is not equal myfile.cfg


Posted by: blackrosezy at February 5, 2008 5:49 AM

For the problem as originally stated, I'd use:

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

Where "keep" is either the full name of the directory that should be retained. No unnecessary traversals, should be very quick.

Posted by: Tony Atkins at April 17, 2008 6:23 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]