Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Forum Archive > Main Support Categories > General Help
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Hello, Unregistered You are browsing a READ only archive of the main support categories pre 4/21/2008. You will not be able to post or reply any threads in this section.

General Help
All your general support questions for Ubuntu, Kubuntu, Edubuntu and Xubuntu.

 
Thread Tools Display Modes
Old January 13th, 2007   #1
oroboro
First Cup of Ubuntu
 
Join Date: Nov 2006
My beans are hidden!
Recursively remove files with wildcard

I've been searching for a way to do this extremely simple task but cannot seem to find one. What I want to do is remove all the files of a certain type (.class) recursively.
I tried this:
Code:
rm -rv *.class
But it doesn't work (because Linux takes care of wildcards rather than the program?)

I know that I can use a for loop to do it, but is there no simpler way?
oroboro is offline   Reply With Quote
Old January 13th, 2007   #2
Ramses de Norre
I Ubuntu, Therefore, I Am
 
Ramses de Norre's Avatar
 
Join Date: Jan 2006
Location: Leuven, Belgium
Beans: 3,414
Re: Recursively remove files with wildcard

That command should work.. I use wildcards like that all the time.
Are you sure you're in the right directory etc? Does rm output something?
Ramses de Norre is offline   Reply With Quote
Old January 13th, 2007   #3
oroboro
First Cup of Ubuntu
 
Join Date: Nov 2006
My beans are hidden!
Re: Recursively remove files with wildcard

That worked, thanks a lot.

Quote:
Originally Posted by Ramses de Norre View Post
That command should work.. I use wildcards like that all the time.
Are you sure you're in the right directory etc? Does rm output something?
Yes I am sure that I was in the same directory. It says that it cannot find the specified file. If you can get it to somehow work, could you post how you did it?
oroboro is offline   Reply With Quote
Old January 13th, 2007   #4
souki
Gee! These Aren't Roasted!
 
Join Date: Jan 2005
Location: Europe/France
Beans: 219
Re: Recursively remove files with wildcard

rm -r is for deleting directories recursively, this is not what you want to do

instead, you can use find, something like that (replace the path and the pattern) :

first check :
Quote:
find /path_to_search -type f -name "*the_patern*"
and then, delete :
Quote:
find /path_to_search -type f -name "*the_patern*" -exec rm {} \;
NB: the '\' and the ';' are important

Last edited by souki; January 13th, 2007 at 01:34 PM..
souki is offline   Reply With Quote
Old January 13th, 2007   #5
souki
Gee! These Aren't Roasted!
 
Join Date: Jan 2005
Location: Europe/France
Beans: 219
Re: Recursively remove files with wildcard

sorry, double post
souki is offline   Reply With Quote
Old January 13th, 2007   #6
souki
Gee! These Aren't Roasted!
 
Join Date: Jan 2005
Location: Europe/France
Beans: 219
Re: Recursively remove files with wildcard

for your security, I have to tell you,

I'm sure that rm -r works only on directories
if you don't believe me, check the manual

or try this proof :

Quote:
touch blah.class
mkdir sub1
touch sub1/blah.class
rm -r *.class
ls sub1/
it will only remove the first blah.class, because it's the only one matching the patern
that's it, the pattern cannot be recursive
all you can do is

Quote:
rm *.class */*.class */*/*.class ... ans so on
now, if you have a directory named "sub.class", rm -rf *.class will remove the directory and all the files inside (recursively)

please, always use the "find" method I mention earlier, it is safe
souki is offline   Reply With Quote
Old January 13th, 2007   #7
Hossie
A Carafe of Ubuntu
 
Hossie's Avatar
 
Join Date: Mar 2006
Beans: 83
Ubuntu Jaunty Jackalope (testing)
Send a message via ICQ to Hossie
Re: Recursively remove files with wildcard

There is an easy explanation for that: All wildcards get expanded by the shell _before_ it gets passed to the program. Programs never see wildcards. So the shell expands *.class to 1.class 2.class in the current directory, and then passes these arguments to the program. So you just pass 2 "class" files to rm, and it just deletes that.
__________________
The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners.
Hossie is offline   Reply With Quote
Old January 13th, 2007   #8
Ramses de Norre
I Ubuntu, Therefore, I Am
 
Ramses de Norre's Avatar
 
Join Date: Jan 2006
Location: Leuven, Belgium
Beans: 3,414
Re: Recursively remove files with wildcard

Code:
ramses@icarus:~$ mkdir test
ramses@icarus:~$ cd test/
ramses@icarus:~/test$ touch 1.class
ramses@icarus:~/test$ touch 2.class
ramses@icarus:~/test$ touch foo.class
ramses@icarus:~/test$ touch test
ramses@icarus:~/test$ ls
1.class  2.class  foo.class  test
ramses@icarus:~/test$ rm *.class
ramses@icarus:~/test$ ls
test
ramses@icarus:~/test$
Is it this kind of thing you were trying to do?
Ramses de Norre is offline   Reply With Quote
Old January 13th, 2007   #9
souki
Gee! These Aren't Roasted!
 
Join Date: Jan 2005
Location: Europe/France
Beans: 219
Re: Recursively remove files with wildcard

I don't think so, he's pretty clear with what he wants to do
"remove all *.class files recursively"
souki is offline   Reply With Quote
Old September 3rd, 2007   #10
sstucke
First Cup of Ubuntu
 
Join Date: Apr 2007
Beans: 7
Re: Recursively remove files with wildcard

I wrote a howto for deleting files recursively (you may even preview which files will be deleted):
http://en.tuxero.com/2007/09/how-to-...-files-in.html
Cheers!
sstucke is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:25 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry