I downloaded a project from web.When building the project it produces output files in folder named output.The problem is there are many folders with same name.I need to manually delete the files in all output directories.

I used this command

find . -type d -name "output" -print

It displays the location of all directories with same name.

Then i used this command

find . -type d -name "output" -delete

find: cannot delete ‘./output’: Directory not empty

I don't want to delete the output folder I want to delete all the files under the output folder name.

  • @don_crissti it contains directories and files .i want to delete everything under the output folder. – Jeeva May 17 '17 at 13:35
  • @ilkkachu sorry i dont know how to do that thats y asked for help.i learned that find command to delete files so i tried to use it to delete files under directory also. – Jeeva May 17 '17 at 13:39
  • @ilkkachu thanks for the link. i tried this link [link] (unix.stackexchange.com/questions/89925/…) it completely removes my directory but i need to delete only files or directories contained in it. – Jeeva May 17 '17 at 13:54
  • @Jeeva You just changed your spec from deleting all files under the output folder to delete files or directories contained in it. – user218374 May 17 '17 at 14:58

If your find has -mindepth and -delete (at least GNU or BSD):

find . -name output -type d -exec find {} -mindepth 1 -delete \;
| improve this answer | |

You could use the following ways depending on the version of find you may have.

GNU find:

find . -type f -path '*/output/*' -exec echo rm -f {} \;


find . -type d -name output -exec sh -c 'find "$1"/. ! -name . -prune -type f -exec rm -f \{\} \;' {} {} \;
| improve this answer | |
  • find . -type f -path '/output/' -exec echo rm -f {} \; it doesnt work – Jeeva May 17 '17 at 14:06
  • Where did you make the * vanish around /output/? Look at the command again. – user218374 May 17 '17 at 14:09
  • @RakeshSharma excatly wat don said.sorry for the editing part – Jeeva May 17 '17 at 14:12
  • Ok then that means your find ain't GNU. So need to use the POSIX version then. – user218374 May 17 '17 at 14:12
  • @RakeshSharma Which part of the first command is not POSIX? – Kusalananda May 17 '17 at 14:17

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.