0

to delete files on under folder we can use the following approach with find

find /home -type f -delete

but how to delete recursive only the files that exists under temp folder ?

lets say we have the following example of temp path

/home/bla/bla/temp
/home/test/temp
/home/subf/subf/subf/temp
.
.
.
/home/1/temp

how to change the find syntax in order to delete only the files under temp directory

the target is to use find command in order to match only the temp folders and remove the files under temp directory

King David
  • 1,001

1 Answers1

4

The following command will find regular files in any directory named temp and below, below /home.

find /home -type f -path '*/temp/*'

To actually delete them:

find /home -type f -path '*/temp/*' -delete

Notes:

  • By default find does not follow symlinks. See man 1 find for options -L, -H and (if supported) -P.

  • -delete is not portable. If your find does not support -delete, use -exec rm {} +. -delete is better not only because it doesn't create an additional process. It is safer as it (at least -delete implemented in GNU find) removes the race condition where someone may be able to make you remove the wrong file(s) by changing some directory to a symlink in-between the time find finds a file and rm removes it. The fact our find command does not follow symlinks is irrelevant at this point.