How to recursively delete a file type in Linux in a directory and all its subdirectories

Recursively delete a file type

Surely on some occasion you have verified that you have a useless file type in several folders, which takes up little space but you simply do not want it on your computer. For example, in macOS some files are created .DS_Store that save information about the icon of a folder, its size and position, and in Windows there are desktop.ini that are equivalent. What do we do if we have a directory full of subdirectories and we want to delete these or other types of files? What we have to do is recursively erase Each and every one of them.

This is something we can do with all types of files. For example, if we have a hard drive full of music, we have a player that has downloaded the covers and we don't want to have them saved, we can use the same command that we will detail below to delete them. Of course, taking into account that we are going to recursively delete directory files and their subdirectories with the terminal and that these will be lost forever, it doesn't hurt to be careful and / or do a test before deleting everything we want to delete.

Recursively delete files of a type from terminal

The commands for the example macOS .DS_Store files would be as follows:

cd ruta/a/directorio
find . -name '*.DS_Store' -type f -delete

Of the previous commands, the first will place us in the folder where we want to start, that is, the one that contains all the .DS_Store files that we want to delete. The second is the one that will delete all the .DS_Store files from the first folder and its subfolders or directories. If what we want is to delete the photos from the previously mentioned music folders, we have to put the extension type between the quotation marks, such as '* .jpg'.

To be 100% honest, this is something I will do the day I stop using Cantata as the default music player. Cantata creates and hides information files with the same name as the songs that have a dot and a low bar in front (for example, ._Song). When I need it, the command I will have to use will be find. -name '._ *' -type f -delete.

Has this little guide helped you to recursively delete files from the terminal?

Related article:
Grep command: powerful tool to find text from Terminal

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

2 comments, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Sebastian Castro said

    Thanks, but there is an error in your code. You need to use the regular expression '*', that is '* .DS_STORE'. I recommend that you correct it. Well, for those who have less knowledge of bash, the code you gave will not work. Greetings!

    1.    pablinux said

      Hello Sebastian- Done. Thanks for the note.

      A greeting.