4

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
  • 79,330
  • 30
  • 216
  • 245
Jeeva
  • 245
  • 1
  • 4
  • 10
  • @don_crissti it contains directories and files .i want to delete everything under the output folder. – Jeeva May 17 '17 at 13:35
  • [How to recursively delete a dir after find](https://unix.stackexchange.com/questions/175674/how-to-recursively-delete-a-dir-after-find) or [How to delete directories based on `find` output?](https://unix.stackexchange.com/q/89925/170373) ? – ilkkachu May 17 '17 at 13:37
  • @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] (https://unix.stackexchange.com/questions/89925/how-to-delete-directories-based-on-find-output) 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. –  May 17 '17 at 14:58

2 Answers2

2

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

find . -name output -type d -exec find {} -mindepth 1 -delete \;
ilkkachu
  • 133,243
  • 15
  • 236
  • 397
1

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 {} \;

POSIX-find

find . -type d -name output -exec sh -c 'find "$1"/. ! -name . -prune -type f -exec rm -f \{\} \;' {} {} \;