Page 1 of 1

Directory/file sorting

PostPosted: Wed Aug 13, 2003 4:30 pm
by Tux
Hello people, I have a question.

Say I have a directory, with multiple subdirectories, all full of various filetypes.
Is there a program/script or something that could recurse through all the directories in a particular branch and delete files matching a certain name?

If there isn't a program/script already in existance, could on of you l337 scriptorz :p suggest where to start with a bash script to do it.

TIA

PostPosted: Wed Aug 13, 2003 5:28 pm
by Void Main
There are several ways that you can do this with "find", and it doesn't have to be based on name, it can be by file type, date, etc:

Code: Select all
$ find /somebasedir/somestartingsubdir -name '*.bak' -exec rm -f {} \;


would delete all *.bak files anywhere under /somebasedir/somestartingsubdir. Or you could do it this way:

Code: Select all
$ find /somebasedir/somestartingsubdir -name '*.bak' | xargs rm -f


Those are probably the easiest ways. If you wanted to just run the command first without deleting files so you can make sure it will only delete the files you want then run it withou the "-exec ... ..." or without the "|xargs rm -f" then add the rm command on the end when you get the search list the way you want it. The find comand is extremely powerful and useful. It would be good to just brush through the man page:

http://voidmain.is-a-geek.net/man/?parm ... ocType=man

PostPosted: Wed Aug 13, 2003 5:35 pm
by Tux
xargs, neat solution. Thanks void main you do my thinking for me :)