Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

the goal is to find directories with given name and delete all files inside them, keeping actual directories

find /home/www/sites/ -iname '_cache' -exec du -hs {} \;

this gives me a list of files with their size

204K    /home/www/sites/
904K    /home/www/sites/

is it possible to achieve with Linux find command?

share|improve this question
Can't you rm -rf the directory and recreate it, since you know the directory name? – cjc Aug 24 '12 at 17:58
I have a lot of folders with such name, will be very time consuming – DmitrySemenov Aug 24 '12 at 18:28

I tried something and it seems it is working, it is a similar solution which Alex posted here.

find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -exec rm {} \;

It should delete only files which are found in _cache directory and only in this directory. It will not delete any files from subdirectories of _cache directory.

Of course try it before using and instead of rm, put there ls or something harmless.

share|improve this answer
WORKS! :) Thanks Panaroik! – DmitrySemenov Aug 24 '12 at 18:44
actually doesn't :) rm: cannot remove `/home/www/sites/': Is a directory – DmitrySemenov Aug 24 '12 at 19:05
It won't delete directories as there is missing -r option. It will delete only files, not a directories. I can look at it later and test it once more. – panaroik Aug 24 '12 at 19:09
it didn't delete files as well, I checked – DmitrySemenov Aug 25 '12 at 5:19
hmm, strange, I will have to check it out, thanks for comments – panaroik Aug 25 '12 at 9:33

I haven't thoroughly tested this logic...but you could do something inside a loop like:

for findname in $(find /path/to/search -name '_pattern')
  find $findname -type f

So you get the list of files that match your search pattern, and then loop through each of those with a new search looking for files to delete.

The way that is written will give you a list of files, so you could redirect that to a file and then loop through that with an rm. You could also append an exec to the find w/in the for loop. I'd certainly recommend running as written first to test the logic and make sure the matches look good.

share|improve this answer
yes, I can do .sh script, but thought is it possible to achieve by one command (maybe using pipes) – DmitrySemenov Aug 24 '12 at 18:29
You can do this on a single line using ;'s to separate the lines. – 3dinfluence Aug 24 '12 at 18:32
for findname in find /path/to/search -name 'pattern'; do find $findname -type f -delete; done – 3dinfluence Aug 24 '12 at 18:33
I can't get the formatting to show the backticks...but the grey portion has backticks on either side of it. Also before adding the -delete I suggest running this without it to verify what it's going to delete. – 3dinfluence Aug 24 '12 at 18:35
up vote 1 down vote accepted

Correct command to erase it

find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -delete 
share|improve this answer

find has a -delete option. This will delete any matches.

From the man page


          Delete files; true if removal succeeded.  If the removal failed,
          an  error message is issued.  If -delete fails, find's exit stat-
          us will be nonzero (when it eventually exits).  Use of  -delete
          automatically turns on the -depth option.

          Warnings:  Don't  forget that the find command line is evaluated
          as an expression, so putting -delete first will make find try to
          delete everything below the starting points you specified.  When
          testing a find command line that you later intend  to  use  with
          -delete,  you should explicitly specify -depth in order to avoid
          later surprises.  Because -delete  implies  -depth,  you  cannot
          usefully use -prune and -delete together.
share|improve this answer
this will delete the directory with name _cache, however I want to delete files within that directory keeping the actual directory – DmitrySemenov Aug 24 '12 at 18:27
ah sorry mis-read your question. – 3dinfluence Aug 24 '12 at 18:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.