LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-19-2004, 01:15 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Recusively deleting files?


Hi! Is there a command to recursively parse through a directory tree removing files? Like running rm in each folder matching wildcards. Example:

rrm * *.jpg

would search all directories under current one and delete all jpgs??

Any way I can do this? I already wrote one of my own, but it doesn't work too well...
 
Old 05-19-2004, 01:28 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
find is pretty good for this:
find /path/to/dir -name "*.jpg" -exec rm -f {} \;
 
Old 05-19-2004, 01:28 PM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
might be better to combine the "find" and the "rm" commands in this case.
to print the names of all .jpg files recursively starting from this directory, you would use a
find . -name "*.jpg"
to remove all those files, you would pipe the output to "rm" like this:
find . -name "*.jpg" | xargs rm
(xargs just arranges for the output of "find" to be given as arguments to "rm")

Last edited by Berhanie; 05-19-2004 at 01:36 PM.
 
Old 05-19-2004, 01:31 PM   #4
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
sorry, david. i didn't see your reply when i answered. i didn't mean to contradict you, only to say that using "find" is better than using a recursive "rm".
 
Old 05-19-2004, 07:33 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Ok Cool! Thanks guys! Could you just tell me what {} \; means at the end of the command????
 
Old 05-19-2004, 09:28 PM   #6
zakaluka
Member
 
Registered: Oct 2003
Posts: 58

Rep: Reputation: 15
Quote:
Originally posted by The_Nerd
Ok Cool! Thanks guys! Could you just tell me what {} \; means at the end of the command????
The_Nerd:

From the find man page:

-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\\') or quoted to protect them from expansion by the shell. The command is executed in the starting directory.

Regards,

zakaluka
 
Old 05-20-2004, 11:54 AM   #7
Aeiri
Member
 
Registered: Feb 2004
Posts: 307

Rep: Reputation: 30
Or... for an easier more understandable way...

rm -r directory/

rm has a "-r" switch for recursive deletion.
 
Old 05-20-2004, 01:32 PM   #8
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Quote:
Originally posted by Aeiri
Or... for an easier more understandable way...

rm -r directory/

rm has a "-r" switch for recursive deletion.
This would delete all files and directories, not just jpg files.
 
  


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
Deleting files in C monil Programming 4 03-13-2005 11:33 AM
Deleting files! a_priebe47 Slackware 6 10-17-2004 10:58 PM
deleting recent files Santas Linux - General 2 01-19-2004 07:26 AM
Deleting files ShakyJake Linux - Newbie 12 05-08-2003 04:07 PM
Deleting files Tingle Linux - Newbie 4 01-16-2003 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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