0

How do I delete a folder that contains a file or folder with a specific name?

E.g. I want to delete all folders that contain the file "things.txt":

/dirA/
/dirB/things.txt
/dirC/
/dirD/things.txt
/dirE/things.txt
/dirF/

It would then delete dirB, dirD, and dirE -- not just the files inside.

Thanks!

1

A solution without piping somewhere else would be to pass the directory name to a subshell in find's exec function.

This will not be as efficient, and of course it'll try to remove directories that don't exist if they've already been deleted.

find . -depth -type f -name 'things.txt' -exec sh -c 'rm -rf "$(dirname $0)"' {} \;

If you want to use xargs, you could do it very simply like so. Note that dirname only takes one argument, so you have to specify -n 1 to have xargs only pass one:

find . -depth -type f -name 'things.txt' | xargs -n 1 dirname | uniq | xargs rm -rf

This will not be safe if your directory name contains a newline, so be aware of that.

  • Thanks for these! One issue: When I tested the second one, xargs -n 1 splits directory names with spaces in them, making it unusable. Is there a way to fix that? – MynockSpit Aug 28 '14 at 20:30
  • This worked with a few minor changes! Adding -print0 to the find and -0 to the xargs fixed the spaces issue, and since I decided I wanted to move everything to the trash and not delete it immediately, I ended up changing the xargs to xargs -0 -n 1 -I {} sh -c 'filename=$(dirname "{}"); mv "$filename" "./Trash";' Thanks a bunch! – MynockSpit Sep 20 '14 at 3:32
0

This has been tested on Linux as these commands below are also executable on Unix system.

find -type f -name 'things.txt' | xargs dirname | xargs rm -r

Explanation:

find -type f -name 'things.txt' --> search for file named 'things.txt' (file only, not directory)

| --> piping the result to next command

xargs dirname | --> grabbing the directory name the file is located in then pipe the result to next command

xargs rm -r --> proceed to remove all of these directories that has the file by name of 'things.txt'

Voila!

  • When you pipe from find to xargs, please use find ... -print0 and xargs -0. – slhck Jul 31 '14 at 7:55
  • It have resulted as this: rm: cannont remove './DirD\n./DirB\n./DirE\n': No such file or directory. with -0, it is preventing bash from looping all files with sets of functions combined? – Faron Jul 31 '14 at 8:01
  • Actually, have you tested your command? It should return the error dirname: extra operand ... things.txt'`. – slhck Jul 31 '14 at 8:10
  • Then it must be because of different version of bash im using. Thank you for pointing that out. – Faron Jul 31 '14 at 8:13
  • You'd need to tell xargs to only pass one argument to dirname (see my answer). There could be different default behavior depending on your utility versions. OS X is a bit different than Linux in this regard. – slhck Jul 31 '14 at 8:31

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.