Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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/test.site.com/html/development/Temporary/_cache
904K    /home/www/sites/test.site2.com/html/development/Temporary/_cache

is it possible to achieve with Linux find command?

share|improve this question
1  
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
add comment

4 Answers

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/test.site.com/html/development/Temporary/_cache': 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
show 3 more comments

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')
do
  find $findname -type f
done

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
add comment
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
add comment

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

From the man page

-delete

          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
add comment

Your Answer

 
discard

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.