Search code examples
linuxfilerm

Remove files for a lot of directories - Linux


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.


Solution

  • 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