LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Recusively deleting files? (https://www.linuxquestions.org/questions/linux-software-2/recusively-deleting-files-183342/)

The_Nerd 05-19-2004 12:15 PM

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...

david_ross 05-19-2004 12:28 PM

find is pretty good for this:
find /path/to/dir -name "*.jpg" -exec rm -f {} \;

Berhanie 05-19-2004 12:28 PM

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")

Berhanie 05-19-2004 12:31 PM

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".

The_Nerd 05-19-2004 06:33 PM

Ok Cool! Thanks guys! Could you just tell me what {} \; means at the end of the command????

zakaluka 05-19-2004 08:28 PM

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

Aeiri 05-20-2004 10:54 AM

Or... for an easier more understandable way...

rm -r directory/

rm has a "-r" switch for recursive deletion.

david_ross 05-20-2004 12:32 PM

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.


All times are GMT -5. The time now is 01:22 PM.