How to find and delete files by their name recursively in GNU/Linux

Miguel Menéndez

To find and delete files in GNU/Linux we usually use the find command.

In the following example we will search and delete in the current directory (.) and recursively in all subdirectories all files (-type f) that contain in their name (-name) the text “sync-conflict” ('*sync-conflict*'):

$ find . -type f -name '*sync-conflict*' - exec rm "{}" +
  • "{}" will be replaced by the output of find
  • + will consider all files returned by find, ; will only consider one (first or last returned by find?)

You can limit the depth of find recursion with -maxdepth (find -maxdepth 2, for example).

See also

Comments

Found a bug? Do you think something could be improved? Feel free to let me know and I will be happy to take a look.