Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find & delete with regex

Tags:

regex

find

how to match then delete files with regex through commandline?

matched files have 4 digits then .tmp eg

something0001.tmp
something-else-0090.tmp
something-6548.tmp

but not

something001.tmp
somethingelse99.tmp

tried various versions of

find . -type f -regex '.*\d{4}.tmp' -exec rm -rf {} \;

RegExr testing ok for matches, but ssh syntax is eluding me and am sure the regex is at fault.

like image 477
TwoCups Avatar asked Nov 30 '25 00:11

TwoCups


1 Answers

find is using by default Emacs syntax of regex. This means that there is nothing like \d or {4}. You if you want to achieve this result, you need to use (also escape the second dot .).

find . -type f -regex '.*[0-9][0-9][0-9][0-9]\.tmp' -exec rm -rf {} \;

or use different -regextype option which supports your regex syntax.

like image 163
Jakuje Avatar answered Dec 02 '25 12:12

Jakuje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!