LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to search and delete files in Unix/Linux (https://www.linuxquestions.org/questions/linux-general-1/how-to-search-and-delete-files-in-unix-linux-167901/)

puzz_1 04-08-2004 05:49 PM

How to search and delete files in Unix/Linux
 
I would like to recursively delete files of type *.txt inside a directory (and its subdirectory and so on). How do I do thaT?

I tried

rm -rf 'find . -name *.txt"

but this doesn't work.

wapcaplet 04-08-2004 07:35 PM

Try:

Code:

find . -name '*.txt' -exec rm '{}' ';'
Funky syntax, but it oughta work.

hw-tph 04-08-2004 07:36 PM

Try find . -iname *.txt -exec rm -f {} \;


Håkan

Edit: Ahh, you beat me to it. :)

jailbait 04-08-2004 07:43 PM

'I would like to recursively delete files of type *.txt inside a directory (and its subdirectory and so on). How do I do thaT?

I tried

rm -rf 'find . -name *.txt"'


You are on the right track. Try it this way:

find /home/user/sampdir -iname "*.txt" -exec rm {} \;

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites

puzz_1 04-09-2004 10:18 AM

A little explanation would be helpful. I get the '-exec rm' part but what are the two switches after that??

{} \;

??

jailbait 04-09-2004 10:35 AM

"A little explanation would be helpful. I get the '-exec rm' part but what are the two switches after that??"

{} \;


The {} tells find to drop each file name that is a match to the pattern into the rm command and then execute the rm command. The \; is necessary for bash to understand the syntax. The return is used by bash as "end of command". In this command you need two "end of commands" in a row (or nested "end of commands" ) or bash will get confused. The first "end of command" is the \; and the second is the return.

See:
man find

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites


All times are GMT -5. The time now is 11:24 PM.