57

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
  • 4
Ibrahim Mumcu
  • 673
  • 1
  • 5
  • 5
  • on a terminal type `man rm` to see the manual page of the rm command. – hmayag Apr 05 '14 at 22:38
  • possible duplicate of [How to search and delete files who contain specific string in name](http://askubuntu.com/questions/625219/how-to-search-and-delete-files-who-contain-specific-string-in-name) – A.B. May 18 '15 at 13:26
  • Looks like these images are the automatically created thumbnails from images uploaded to your WorldPress media library. If so, then don't delete those files in terminal on the server. Open your WordPress admin page, open 'Settings' > 'Media'. Set desired thumbnail image sizes there. Then install the plugin [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/) and re-create all thumbnails (may take a few minutes, depending of the amount of images in your media library). – Bob Feb 09 '18 at 19:39

5 Answers5

72

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,330
  • 11
  • 19
  • 1
    Is that no need to represent file path – Avinash Raj Apr 06 '14 at 02:03
  • @AvinashRaj If the first parameter isn't a path, `find` searches the current working directory. – n.st Apr 06 '14 at 10:30
  • Somebody put . just after find for searching inside current working directory. – Avinash Raj Apr 06 '14 at 11:16
  • 1
    @AvinashRaj That would be redundant. As per `man find`: *If no paths are given, the current directory is used.* – n.st Apr 06 '14 at 15:10
  • @AvinashRaj Turns out the [POSIX](http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html) specification for `find` actually *does* require a path. Defaulting to `.` is a modification added by GNU find. If this were [Unix.SE], I'd add that to my answer, but since Ubuntu comes with GNU find by default, I'd rather not confuse newcomers more than necessary. ;) – n.st Nov 15 '16 at 05:50
53

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

rm *text*

Where text is string that filename contains.

Michal Fudala
  • 816
  • 1
  • 8
  • 9
2
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
  • 21
  • 2
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
  • 116,445
  • 54
  • 318
  • 493
Szczepan
  • 11
  • 1
  • Just remember to add `-rf` to your command, or you will end with empty directories. `rm B* b* -r` removes all the contents inside every single directory starting with `B` and `b`. But if you wish to remove the whole directory and its contents, you should use the `-f` flag to instruct `rm` to also "force" delete the directory. Please see `man rm` or `rm --help` for further information. – Geppettvs D'Constanzo Jun 02 '20 at 17:02
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
  • 23,569
  • 16
  • 74
  • 100
Aneesh
  • 11
  • 2