Command to delete all files from folders matching name recursivly?

12,573

Solution 1

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

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

See Pattern Matching

Solution 2

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

Solution 3

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!

Solution 4

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
Share:
12,573

Related videos on Youtube

Hailwood
Author by

Hailwood

I could tell you all about me... but I'd prefer to let my work do the talking for me!

Updated on September 18, 2022

Comments

  • Hailwood
    Hailwood over 1 year

    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?

    • Hailwood
      Hailwood almost 12 years
      Someone downvoted this yet gave no comment?
    • amanthethy
      amanthethy almost 10 years
      It was probably downvoted because it's more to do with general scripting than do with Ubuntu. Should have been asked over at StackOverflow.
  • Flint
    Flint almost 12 years
    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