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.

5
  • @don_crissti it contains directories and files .i want to delete everything under the output folder.
    – Jeeva
    Commented May 17, 2017 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
    Commented May 17, 2017 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
    Commented May 17, 2017 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
    Commented May 17, 2017 at 14:58

2 Answers 2

2

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

find . -name output -type d -exec find {} -mindepth 1 -delete \;
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 \{\} \;' {} {} \;
8
  • find . -type f -path '/output/' -exec echo rm -f {} \; it doesnt work
    – Jeeva
    Commented May 17, 2017 at 14:06
  • Where did you make the * vanish around /output/? Look at the command again.
    – user218374
    Commented May 17, 2017 at 14:09
  • @RakeshSharma excatly wat don said.sorry for the editing part
    – Jeeva
    Commented May 17, 2017 at 14:12
  • Ok then that means your find ain't GNU. So need to use the POSIX version then.
    – user218374
    Commented May 17, 2017 at 14:12
  • @RakeshSharma Which part of the first command is not POSIX?
    – Kusalananda
    Commented May 17, 2017 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .