• Home
  • Uncategorized
  • Searching for and deleting multiple directories at once using Linux shell

Searching for and deleting multiple directories at once using Linux shell

I wanted to find and delete all files of Season 1 of a particular TV show.

[[email protected]] ~/ # find . -iname '*S01*' -type d
./Poirot S01E09 - The King of Clubs (1989)
./Poirot S01E02 - Murder In The Mews (1989)
./Poirot S01E04 - Four And Twenty Blackbirds (1989)
./Poirot S01E05 - The Third Floor Flat (1989)
./Poirot S01E03 - The Adventure Of Johnnie Waverly (1989)
./Poirot S01E10 - The Dream (1989)
./Poirot S01E01 - The Adventure of the Clapham Cook (1989)
./Poirot S01E08 - The Incredible Theft (1989)

Now trying to delete them with the find -delete option:

[[email protected]] ~/ # find . -iname '*S01*' -type d -delete
find: cannot delete `./Poirot S01E05 - The Third Floor Flat (1989)': Directory not empty
find: cannot delete `./Poirot S01E03 - The Adventure Of Johnnie Waverly (1989)': Directory not empty
find: cannot delete `./Poirot S01E10 - The Dream (1989)': Directory not empty
find: cannot delete `./Poirot S01E01 - The Adventure of the Clapham Cook (1989)': Directory not empty
find: cannot delete `./Poirot S01E08 - The Incredible Theft (1989)': Directory not empty

What I wanted was this:

[[email protected]] ~/ #find . -iname ‘*S01*’ -type d -exec rm -r {} +
[[email protected]] ~/ #find . -iname ‘*S01*’ -type d

There, the directories have been deleted.