Question

How can I remove all .txt files present in several directories

Dir1 >
  Dir11/123.txt
  Dir12/456.txt
  Dir13/test.txt
  Dir14/manifest.txt

In my example I want to run the remove command from Dir1.

I know the linux command rm, but i don't know how can I make this works to my case.

PS.: I'm using ubuntu.

Was it helpful?

Solution 2

We're not going to enter sub directories so no need to use find; everything is at the same level. I think this is what you're looking for: rm */*.txt

Before you run this you can try echo */*.txt to see if the correct files are going to be removed.

Using find would be useful if you want to search subfolders of subfolders, etc.

There is no Dir1 in the current folder so don't do find Dir1 .... If you run the find from the prompt above this will work:

find . -type f -name "*.txt" -delete

OTHER TIPS

To do what you want recursively, find is the most used tool in this case. Combined with the -delete switch, you can do it with a single command (no need to use -exec (and forks) in find like other answers in this thread) :

find Dir1 -type f -name "*.txt" -delete

if you use , you can do too :

( shopt -s globstar; rm Dir1/**/*.txt )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top