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

Archived topics about LMDE 1 and LMDE 2
Locked
xircon

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

Post by xircon »

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

Cheers

Steve
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
eanfrid

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

Post by eanfrid »

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)
xircon

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

Post by xircon »

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?
eanfrid

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

Post by eanfrid »

Maybe you have file names found containing spaces. Try

Code: Select all

find /home/molly3/temp -type f -name "*.txt" -exec rm -f {} \;
xircon

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

Post by xircon »

That did it cheers.

Steve
anmys

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

Post by anmys »

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

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

Post by widget »

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.
xircon

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

Post by xircon »

True, but unfortunately I did not create the files :(
Locked

Return to “LMDE Archive”