Delete all files - *.txt from a directory and sub directorys

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read this

Delete all files - *.txt from a directory and sub directorys

Postby xircon on Mon Jul 23, 2012 8:58 am

Stumped! rm -r *.txt does not do it. Google foo has deserted me!

Cheers

Steve
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Linux Mint is funded by ads and donations.
 

Re: Delete all files - *.txt from a directory and sub direct

Postby eanfrid on Mon Jul 23, 2012 9:15 am

Be careful with rm and its syntax but...
Code: Select all
find (directory) -type f -name *.txt -exec rm -f {} \;
should do what you want. Validate your regex syntax before with:
Code: Select all
find (directory) -type f -name (regex)
No, Linux is not a Windows clone for free - Before asking any question here...
"Never attribute to malice that which is adequately explained by stupidity." (Hanlon's razor)

Debian GNU/Linux Wheezy 64bit w/custom 3.12 kernel - MATE 1.8
User avatar
eanfrid
Level 6
Level 6
 
Posts: 1204
Joined: Mon Apr 30, 2012 2:49 am
Location: France

Re: Delete all files - *.txt from a directory and sub direct

Postby xircon on Mon Jul 23, 2012 10:38 am

Must be doing something wrong:
Code: Select all
 find /home/molly3/temp -type f -name *.txt -exec rm -f {} \;
find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


Any ideas?
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Re: Delete all files - *.txt from a directory and sub direct

Postby eanfrid on Mon Jul 23, 2012 10:55 am

Maybe you have file names found containing spaces. Try
Code: Select all
find /home/molly3/temp -type f -name "*.txt" -exec rm -f {} \;
No, Linux is not a Windows clone for free - Before asking any question here...
"Never attribute to malice that which is adequately explained by stupidity." (Hanlon's razor)

Debian GNU/Linux Wheezy 64bit w/custom 3.12 kernel - MATE 1.8
User avatar
eanfrid
Level 6
Level 6
 
Posts: 1204
Joined: Mon Apr 30, 2012 2:49 am
Location: France

Re: Delete all files - *.txt from a directory and sub direct

Postby xircon on Mon Jul 23, 2012 10:57 am

That did it cheers.

Steve
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Re: Delete all files - *.txt from a directory and sub direct

Postby anmys on Mon Jul 23, 2012 11:58 am

The solution will give you trouble when there are files with space in their names. It is also better to use xargs rather than exec. To get round that problem use the following command :

Code: Select all
find dirname -name "*.txt" -print0 | xargs -0 rm


Regards.
anmys
Level 1
Level 1
 
Posts: 44
Joined: Sat Feb 05, 2011 8:30 pm

Re: Delete all files - *.txt from a directory and sub direct

Postby widget on Mon Jul 23, 2012 5:39 pm

After this experience you can see why, under Linux, it is best not to have file names with spaces.

Use some thing like - or _ between words if you must have a space.

I use capitols for things like GrubLegacy.

Makes life easier if you go with what the file system wants.
Dell XPS 420 Core2 Quad Q 6600, audigy5.1, Radeon HD 6450 - currently 4 320Gb HDD, Debian Squeeze for secure use, Debian testing for daily use, Debian Sid for fun.
widget
Level 3
Level 3
 
Posts: 167
Joined: Thu May 14, 2009 2:49 am
Location: S.E. Montana

Re: Delete all files - *.txt from a directory and sub direct

Postby xircon on Mon Jul 23, 2012 5:41 pm

True, but unfortunately I did not create the files :(
Dell Inspiron N5010 / Core I5 / ATI Mobility Radeon HD5400, LMDE Tracking Sid & Cinnamon Desktop
"Ubuntu is an African word meaning 'I can't configure Debian'"
User avatar
xircon
Level 5
Level 5
 
Posts: 775
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

Linux Mint is funded by ads and donations.
 

Return to Newbie Questions

Who is online

Users browsing this forum: DarKlon and 6 guests