I have my base folder (let's call it base).

I have a bunch of folders inside, scattered at various depths within these folders are cache folders.

I want to delete all files from within the cache folders, but not the folders themselves.

I have tried

cd base
#then one of...
sudo rm -rf cache/*
sudo rm -rf *cache/*
sudo rm -rf cache*/*
sudo rm -rf *cache*/*

But really am just guessing, what would be the right command?

  • Someone downvoted this yet gave no comment? – Hailwood Jul 10 '12 at 7:35
  • It was probably downvoted because it's more to do with general scripting than do with Ubuntu. Should have been asked over at StackOverflow. – amanthethy Aug 7 '14 at 4:22

In bash, you can enable globstar and use ** to match directories recursively

shopt -s globstar
echo rm -rf ./**/*cache*/*

See Pattern Matching

| improve this answer | |

This will recursively delete all of the files within folders named cache, starting at the folder base. The cache folders themselves will still be intact, but all of the files within each will be removed.

find base -ipath "*/cache/*" -type f -delete
| improve this answer | |

You could use the extremely powerful find command.

I would use something like this:

find . -iname *cache* -ok rm -rf {} \;

Now let me explain it for you. Find is the name of the application, it has a lot of options but for you, you won't need many of them.

The . means look in this current directory. This means you'll have to be in the right directory to start off with. I assume for you that is ~

-iname means search my case insensitive name.

*cache* means that the name must contain cache.

Now the next part is important.

-ok means perform the commands that follow but as me if I want to do it first. This can be replaced with -exec but I wouldn't advise it. That would delete things without telling you and you don't want that.

Ok, so the next line (which is the stuff that -ok runs) is rm -rf {} \;

The rm -rf I hope is self explanatory. The {} is basically a placeholder for the name of the file it has found. The \; at the end just means it is the end of that line.

I hope that makes sense.

I advise running the find command without everything right of and including -ok. It will pump out a list of all the cache files first and you can review them. Then add the -ok section and get cracking!

| improve this answer | |
  • You forgot to quote the pattern for -iname; that'll lead to errors if there happens to be files matching that pattern in the current directory. Also, this approach deletes the cache directories as well, not just the contents. – geirha Jul 9 '12 at 10:06
  • This will delete all folders named cache including the files in them. OP wants to delete files in folders named cache, not the folders themselves – Flint Jul 9 '12 at 10:18

Just replace basedir by the path of the base directory you want to remove

$ find basedir -type f -delete

If you feel more confortable moving previously to your base dir, then:

$ cd basedir
$ find . -type f -delete
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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