LinuxQuestions.org
Review your favorite Linux distribution.
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 09-11-2009, 03:28 PM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Rep: Reputation: 46
Remove multiple folders based on name in one command


is is possible to do a

rm -r 01*2009

or something to remove all folders (and their subdirectories) whose name starts with 01 and ends with 2009 with anything in between those numbers?
 
Old 09-11-2009, 03:32 PM   #2
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,

find . -type f -name "01*2009" -exec ls {} \;

This will, from the directory you are in, recursively look for files (type f) with names matching 01*2009 and list them (ls).

If you are happy with the output change ls to rm.

Hope this helps.
 
Old 09-11-2009, 03:35 PM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
I actually just want to remove the folders, they are all empty anyway.
 
Old 09-11-2009, 03:39 PM   #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,

Me reading to fast, again.....

find . -type d -name "01*2009"

The -type d will tell find to only look for directories.

If the output is as expected add the following to the previous command: -exec rm -rf {} \;
 
Old 09-11-2009, 03:40 PM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
ok, thanks. so is there not a way to do it with rm? or is it bad to do it that way? i'm trying to learn.
 
Old 09-11-2009, 03:54 PM   #6
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,

Rm -r <something> is very blunt. It removes <something> and all its sub dirs/files. Find can be more selective. It can search <something> (and all its sub dirs/files) and look for a specific pattern and limit the action(s) taken to only those files.

In your case (remove all folders (and their subdirectories) whose name starts with 01 and ends with 2009), rm is not the correct tool to use.

BTW: you are using rm, with the -exec option that comes with find, you can execute just about any other linux command. In this case rm. I know that's not what you meant, but strictly speaking all the deleting is done by rm, find "just" gives rm the appropriate files/dirs.

Hope this clears things up a bit.
 
Old 09-11-2009, 04:04 PM   #7
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
Yeah, i think we're on the same page. its just better to use find to get exactly what you want, then pipe it to the rm

what's the \; at the end of the line for?
 
Old 09-11-2009, 04:13 PM   #8
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,

The ; is part of the -exec command (-exec <command> ; ). Find needs to know where the command it executes ends. It needs to be escaped due to bash, so that's why it is \; I have seen other terminators being used, but cannot recall them (I always use \; ).

The {} is "replaced" with the files/dirs that are found by the find command (one at the time, not all at once).

BTW: find is rather powerful and has some great options, but things can go wrong. Always test the output without the -exec part (or substitute the command with ls if possible).
 
Old 09-11-2009, 04:26 PM   #9
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
ok, i get how you have to close the exec portion of find with the semicolon, but then just because you use bash you throw a \ in before the semicolon? guess i dont see the logic with that one.
 
Old 09-11-2009, 04:42 PM   #10
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,

It, the need to escape the ;, has to do with the order in which all the actions are done (command line processing).

In very short: bash first looks at the command and looks if anything "special" needs to be done. It sees the ; as a command separator and it is stripped (if no \ is present) from the command that is given to find.

It is a much longer story, but definitely worth looking into (kinda essential in my opinion). Feed google with bash cookbook command-line Processing. The links found can explain it better then I can...

Hope this helps.
 
Old 09-11-2009, 04:43 PM   #11
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
ok, i will google that. sounds like its kinda just telling bash not to mess with the semicolon if i'm following you. thanks for all the help.
 
Old 09-11-2009, 05:02 PM   #12
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
After running it, it tells me no such file or directory for each folder that it just deleted. is it supposed to do that?
 
Old 09-11-2009, 05:11 PM   #13
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
It's not clear to me what you did.

If you ran the command (with the -exec rm part), and then ran it a second time, the second one will tell you the no such file or directory message. That would be correct, the first time you ran it all the appropriate files/dirs are removed, so the second cannot find anything.

Also, what was the exact command you used?
 
Old 09-11-2009, 05:16 PM   #14
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
find . -type d -name "08*2008*" -exec rm -rf {} \;


it pops up with those the first time i run the command though. i do just the find part first to make sure it showed the right folders, then did a ll to see the folders were there, then when i run the command, it gives me those errors for each folder it deleted. almost like it's running the exec part a 2nd time on the list generated by the find.
 
Old 09-11-2009, 05:19 PM   #15
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
Dumb that I didn't spot that earlier: It should be "08.*2008.*" and not "08*2008*" ........

Sorry about the above, not thinking straight (getting late).

Scrap the above. Its BS.

You get that message because some dirs (and subdirs) are already deleted before rm tries to delete directories in that deleted directory.

In other words: dir structure looks like this:

a--b--c (b subdir for a and c subdir for b). If I delete a and its subdirs first and then try to delete b (or c) you get an error. Because find first makes a list of what it finds (order is random) this can happen.

Sorry again for the mix-up

Last edited by druuna; 09-11-2009 at 05:31 PM.
 
  


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 05:35 PM
copying multiple files in multiple folders ?? stratotak Linux - Newbie 2 02-21-2009 10:41 AM
remove ~ from all files and folders BoraX Linux - General 1 10-25-2006 08:58 PM
How to remove folders Gins Linux - General 7 08-15-2006 02:31 PM


All times are GMT -5. The time now is 06:32 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