|
|
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. 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) That might not be exactly what you need, but it should certainly help you get going in the right direction. Good luck!
More Useful Shell Script Programming Articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔ Parsing "id" strings in a Shell Script?Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔ Copy and Paste from the Mac OS X Command Line?I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Script to test line lengths for Twitter compatibility?I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Shell Script Programming
(Article 4305,
Written by Dave Taylor)
Tagged: Previous: Putting text around a Paypal button? Next: How can shell scripts check user ID? Reader Comments To Date: 7Dave Taylor said, on December 12, 2005 4:14 PM:
Yeah, and theoretically you could "tar" the directory up, remove everything, and "untar" it, but where's the challenge in a simple solution, Paul? :-) govind songara said, on June 23, 2006 12:39 AM:
This also can be use to delete all dir except savedir rm -rf $(find . -type d|grep -v savedir) Octavio said, on September 24, 2007 9:53 PM:
Even more elegant solution: $ shopt -s extglob you Korn shell users can ommit the first line. blackrosezy said, on February 5, 2008 5:49 AM:
$ 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
Tony Atkins said, on April 17, 2008 6:23 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. Mads said, on February 27, 2009 3:10 AM:
Yeat-another-go (without using sed/grep/find/...): ls -A1 | while read entry; do [ "$entry" = "DirectoryOrFileThatIWantToSave" ] || rm -r "$entry"; done
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
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?
;-)