Recursively delete files with specific name by using find command

Usage

  • Delete all the files which have same name with ${filename}.
find . -name "${filename}" -type f -delete  
  • Delete all files with .bak file extension in the current dir
find . -name "*.bak" -type f -delete  

Doc

-name pattern  
    True if the last component of the pathname being examined matches pattern.  
    Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern.  
    These characters may be matched explicitly by escaping them with a backslash (``\'').  

or you can use regular expression for file matching.

-regex pattern  
    True if the whole path of the file matches pattern using regular expression.  
    To match a file named ``./foo/xyzzy'', you can use the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'',  
    but not ``xyzzy'' or ``/foo/''.  

Reference


Share


Donation

如果覺得這篇文章對你有幫助, 除了留言讓我知道外, 或許也可以考慮請我喝杯咖啡, 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。

If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.


Related Posts