LinuxQuestions.org
Help answer threads with 0 replies.
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 01-28-2010, 08:17 AM   #1
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Rep: Reputation: 49
How to delete all folders and their contents except 3


I have a USB drive that has a TON of folders on its root level. I want to remove all those folders and their contents except three of them. I know if I do rm -rf that will kill everything, is there a way to exclude three folders, say folder1 folder2 newfolder, and do it all in one statement?
 
Old 01-28-2010, 08:36 AM   #2
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
Code:
mv dir1 dir2 dir3 /some/backup/dir
rm -rf ./
why waste time? it will also save you data in case you do it wrong.
well, in fact you can use rm with ls --hide if you wish.
 
Old 01-28-2010, 08:39 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Trying to write simple code to do something like this will be "fragile"---meaning that you have to be very careful in how you specify things.

Here's a simple example of the kind of thing that might work, but it's just using rm for files. TEST any code before actually using on real data.

Code:
rm $(ls|egrep -v "(file1|file2)")  ##removes everything except file1 and file2
 
1 members found this post helpful.
Old 01-28-2010, 08:41 AM   #4
anon091
Senior Member
 
Registered: Jun 2009
Posts: 1,795

Original Poster
Rep: Reputation: 49
thanks pixellany, I figured there would be some way to do it other than moving all my folders i want to keep to another location. I'll have to look into it to see if i can figure out how to make it keep only certain folders at that root level.
 
  


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
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
Contents of folders not showing up tardigrade Linux - General 1 02-02-2009 06:34 AM
comparing contents of folders newbiesforever Linux - Software 2 02-01-2009 09:44 PM
merge contents of folders minimole Linux - Newbie 5 05-25-2007 05:46 AM
How to compare two folders contents? rvoigt Linux - Newbie 6 01-07-2005 12:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:33 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
Open Source Consulting | Domain Registration