64

I changed my wordpress theme. The older one created so much images on server. My new theme doesnt need them, so I want to remove all. How can I do that?

For example:
Default image: 12_angry_men_lone_holdout.jpg

I want to delete:

12_angry_men_lone_holdout-290x166.jpg
12_angry_men_lone_holdout-700x300.jpg 
12_angry_men_lone_holdout-50x50.jpg

Using Digitalocean, Ubuntu 13.10.

Mala
  • 103

5 Answers5

85

Use find to recursively find and delete files with "text" in their names:

find -type f -name '*text*' -delete

You might also want run find -type f -name '*text*' (without the -delete) before that to make sure you won't delete any files you didn't intend to delete.


In fact, you can place wildcards anywhere in the search string, so -name '12_angry_men_lone_holdout-*.jpg' might be more suitable in your case.

n.st
  • 1,470
55

If they are in the same folder use * wildcard to achieve that:

rm *text*

Where text is string that filename contains.

3
find . -type f -name '*[0-9]x[0-9]*' -delete

Run this in the parent directory. This is going to delete all files that have a digit followed by an 'x' character followed by another digit in their name.

Still be careful, this might delete original files too, if their name contains the above pattern (unlikely). Run it first without '-delete' to see if you have any files that have such a name. If that's the case, you'll just need to find a more restrictive pattern.

Tamas
  • 31
1

I found out, that if you want to delete a directory which starts with a specific letter, you can use the next command:

Let's say you have created the next folders:

  • Baka
  • baka
  • Aka
rm -rf B* b*

This will delete all directories starting with those letters (uppercase B and lowercase b).

After executing this command, you will only keep the folder named Aka. You can check it using ls to list the remaining folders in the current directory.

Eliah Kagan
  • 119,640
1

Try this:

rm -rf 12_angry_men_lone_holdout-*

This will keep 12_angry_men_lone_holdout.jpg and remove files with dimensions (290x166)

And please remember

rm -rf 12_angry_men_lone_holdout.*

will delete the default file too, that you needed.

edwinksl
  • 24,109
Aneesh
  • 11