How to delete all the files with a certain extension from a folder and all of its subfolders?

124,264

Solution 1

The easiest way (if you are using Ubuntu Desktop):

Go to your Music folder in Nautilus, press Ctrl+F and search for .jpg.

enter image description here & then delete it


You can also change the location and you can make your search more specific.

enter image description here


Updated

Be More specific after searching .jpg Clicking on green button Select File type Picture & then remove jpg from search only . dot & then reload as shown in pic below

What it does it will search Picture file like .jpg .png .gif & all other file which are in Picture Format

enter image description here

Solution 2

A quick and clean solution for the command line would be

cd <dir>
find . -type f -iname \*.jpg -delete
  • . tells to start searching in the current folder.
  • -type f tells find only to look for files.
  • -iname makes the search case insensitive.
  • -delete tells find to delete/remove all files found.

CAUTION! I recommend running the command without -delete first to get a list of the files that will be removed when -delete is included in the command. This way a small typo won't delete anything you didn't intend to.

For more information on how to use find and what it can do see man find

Note that find will not produce any output when -delete is used.

Regarding the comment on multiple extensions

find . -type f \( -name \*jpg -o -name \*png \) -delete

  • ( .. ) Group expression. Needs to be escaped from the shell, hence \( .. \) in the example
  • -o logical or

So the above example will match any file which has either jpg or png at the end of it's name. More extensions can be added in the same way. Just make sure to remember -o between every -name statement, if -o is not specified find will default to -a (and) which won't work since a file can't end in both jpg and png at the same time.

Solution 3

This should do it

sudo rm -rf -d ~/Music/*.JPG

which will remove all .JPG files within the Music folder.

Solution 4

Bash's shopt -s globstar can be useful here for recursive globbing:

bash-4.3$ tree
.
├── 10.jpg
└── subfolder
    ├── 5.jpg
    └── another_subfolder
        └── 15.jpg

2 directories, 3 files
bash-4.3$ shopt -s globstar
bash-4.3$ rm ./**/*.jpg
bash-4.3$ tree
.
└── subfolder
    └── another_subfolder
Share:
124,264

Related videos on Youtube

mega.venik
Author by

mega.venik

Updated on September 18, 2022

Comments

  • mega.venik
    mega.venik over 1 year

    I want to remove all the .jpg files from my Music folder in order to save room. My Music folder contains subfolders, and I would like to know if there is a command to remove all the .jpg files from all these folders regardless of their level. Thanks for your help!

  • Rinzwind
    Rinzwind over 12 years
    even better :) :)
  • knittl
    knittl over 12 years
    This will not remove files from subfolders …
  • Casper
    Casper over 7 years
    How about I want to delete multiple types of extensions?
  • Nimbuz
    Nimbuz over 7 years
    CAUTION: This will delete images inside SUBFOLDERS as well. Google brought me here to my misfortune. Do find . -maxdepth 1 -type f -iname \*.jpg -delete if you want to protect the subfolders.
  • Marcelo Ágil
    Marcelo Ágil over 5 years
    This is a good command when you don't want to clean subfolders, nice one.
  • We are Borg
    We are Borg over 5 years
    The question states that the OP is looking for deleting all files including subfolders.
  • Raf
    Raf over 5 years
    Do not run this in the home directory, it would go inside directories starting with . and delete things