I have directories with sub directories containing .srt files. I need to go through the directories and delete them all. I know how to find them like so:

find ./directory -name *.srt

but I'm not sure how to pipe them to rm.

  • First of all you should quote *.srt to avoid shell globbing. Without the quotes it will work as you expect only when there are no *.srt files in the current directory. – Kamil Maciorowski Jun 9 '17 at 16:57

The syntax is a little bit tricky:

find ./directory -name "*.srt" -exec rm {}  \;
  • 1
    it'd be nicer to use -delete – djsmiley2k Jun 9 '17 at 17:00
  • 1
    -exec rm {} \; spawns rm for every file. -exec rm {} + removes many files at once; -delete is the best because it doesn't create new processes, I believe. – Kamil Maciorowski Jun 9 '17 at 17:04
  • 1
    @djsmiley2k though -delete isn't strictly portable, though BSD and GNU both support it. It isn't specified in POSIX though, but if you've got it that's the way to go – Eric Renouf Jun 9 '17 at 17:56
  • Here's what I used and it worked fine. Thanks to all those who answered and commented: find TV_Recordings/ -name "*.srt" -delete – Widgeteye Jun 9 '17 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.