Directory/file sorting

Place to discuss anything, almost. No politics, religion, Microsoft, or anything else that I (the nazi censor) deem inappropriate.

Directory/file sorting

Postby Tux » Wed Aug 13, 2003 4:30 pm

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
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Postby Void Main » Wed Aug 13, 2003 5:28 pm

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
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby Tux » Wed Aug 13, 2003 5:35 pm

xargs, neat solution. Thanks void main you do my thinking for me :)
Tux
guru
guru
 
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am


Return to The Lounge

Who is online

Users browsing this forum: No registered users and 2 guests

cron