Deleting files using find

We will learn how to delete files using command find from given folder.

Suppose you want to delete .png files from /home/directory.

Use following command

find /home/directory/ \( -name "*.png" \) -type f -exec rm {} +

This will delete file with extension .png from /home/directory and sub directories of /home/directory like *.png files from /home/directory/directory2/ as well .

But suppose if you need to delete .png files only from directory /home/directory and not from sub directories then use following command.

find /home/directory/ -maxdepth 1  \( -name "*.png" \) -type f -exec rm {} +

Leave a Reply

Your email address will not be published. Required fields are marked *