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

Archived topics about LMDE 1
Forum rules
User avatar
xircon
Level 5
Level 5
Posts: 770
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

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

Postby xircon » 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
eanfrid
Level 7
Level 7
Posts: 1865
Joined: Mon Apr 30, 2012 2:49 am
Location: FR

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

Postby eanfrid » 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)
Main desktop: Debian GNU/Linux Jessie 64bit - MATE
(i5 2400@3.7GHz - 16GB DDR3 - HD6770 w/radeon driver - SSD+RAID1)
Safer than Dropbox

User avatar
xircon
Level 5
Level 5
Posts: 770
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

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

Postby xircon » 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
eanfrid
Level 7
Level 7
Posts: 1865
Joined: Mon Apr 30, 2012 2:49 am
Location: FR

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

Postby eanfrid » 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 {} \;
Main desktop: Debian GNU/Linux Jessie 64bit - MATE
(i5 2400@3.7GHz - 16GB DDR3 - HD6770 w/radeon driver - SSD+RAID1)
Safer than Dropbox

User avatar
xircon
Level 5
Level 5
Posts: 770
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

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

Postby xircon » 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'"

anmys
Level 1
Level 1
Posts: 47
Joined: Sat Feb 05, 2011 8:30 pm

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

Postby anmys » 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.

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 widget » 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.

User avatar
xircon
Level 5
Level 5
Posts: 770
Joined: Sun Aug 01, 2010 7:59 am
Location: Sheffield, UK

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

Postby xircon » 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'"


Return to “Archive”

Who is online

Users browsing this forum: No registered users and 0 guests