LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-08-2004, 05:49 PM   #1
puzz_1
Member
 
Registered: May 2003
Posts: 49

Rep: Reputation: 15
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.
 
Old 04-08-2004, 07:35 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Try:

Code:
find . -name '*.txt' -exec rm '{}' ';'
Funky syntax, but it oughta work.
 
Old 04-08-2004, 07:36 PM   #3
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Try find . -iname *.txt -exec rm -f {} \;


Håkan

Edit: Ahh, you beat me to it.
 
Old 04-08-2004, 07:43 PM   #4
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
'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
 
Old 04-09-2004, 10:18 AM   #5
puzz_1
Member
 
Registered: May 2003
Posts: 49

Original Poster
Rep: Reputation: 15
A little explanation would be helpful. I get the '-exec rm' part but what are the two switches after that??

{} \;

??
 
Old 04-09-2004, 10:35 AM   #6
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"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

Last edited by jailbait; 04-09-2004 at 10:41 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search files in linux using GUI? SAnton Linux - Newbie 3 09-21-2005 09:01 AM
how to search and delete Xiangbuilder Linux - Newbie 2 11-30-2004 04:56 AM
Library function to search for files on Linux vtluu Red Hat 3 04-16-2004 01:31 PM
Script on linux to delete certain files ForumKid Linux - General 2 06-22-2002 01:09 PM
Search and Delete ryanstrayer Linux - General 9 02-10-2002 12:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:02 PM.

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