0

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?

4 Answers4

3

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.

panaroik
  • 832
  • 1
  • 5
  • 12
  • 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 05:19
  • hmm, strange, I will have to check it out, thanks for comments – panaroik Aug 25 '12 at 09:33
  • IMHO, it's dangerous to use such a command. I wonder what will happen with a file named "_cache *" –  Aug 25 '12 at 23:56
  • find . -iname '_cache' | xargs -I {} ls {} ===> This gives ls: cannot access ./_cache : No such file or directory –  Aug 25 '12 at 23:57
  • Eric what you mention I think can be fixed by adding -type d in the first find command, but I did not test it, just thought – panaroik Aug 26 '12 at 14:06
1

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.

Alex
  • 6,623
  • 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
1

Correct command to erase it

find . -iname '_cache' | xargs -I {} find {} -type f -maxdepth 1 -delete 
user9517
  • 116,228
0

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.
3dinfluence
  • 12,479