user1769686
user1769686

Reputation: 535

dealing filenames with shell regex

138.096.000.015.00111-138.096.201.072.38717
138.096.000.015.01008-138.096.201.072.00790
138.096.201.072.00790-138.096.000.015.01008
138.096.201.072.33853-173.194.020.147.00080
138.096.201.072.34293-173.194.034.009.00080
138.096.201.072.38717-138.096.000.015.00111
138.096.201.072.41741-173.194.034.025.00080
138.096.201.072.50612-173.194.034.007.00080
173.194.020.147.00080-138.096.201.072.33853
173.194.034.007.00080-138.096.201.072.50612
173.194.034.009.00080-138.096.201.072.34293
173.194.034.025.00080-138.096.201.072.41741

I have many folders inside which there are many files, the file names are like the above I want to remove those files with file names having substring "138.096.000"

and sometimes I want to get the list of files with filenames with substring "00080"

Upvotes: 0

Views: 63

Answers (2)

Brian Agnew
Brian Agnew

Reputation: 272307

rm $(find . -name \*138.096.000\*)

This uses the find command to find the appropriate files. This is executed within a subshell, and the output (the list of files) is used by rm. Note the escaping of the * pattern, since the shell will try and expand * itself.

This assumes you don't have filenames with spaces etc. You may prefer to do something like:

for i in $(find . -name \*138.096.000\*); do
   rm $i
done

in this scenario, or even

find . -name \*138.096.000\* | xargs rm

Note that in the loop above you'll execute rm for each file, and the xargs variant will execute rm multiple times (dependin gon the number of files you have - it may only execute once).

However, if you're using zsh then you can simply do:

rm **/*138.096.000*

(I'm assuming your directories aren't named like your files. Note the -f flag as used in Kamil's answer if this is the case)

Upvotes: 0

Kamil Šrot
Kamil Šrot

Reputation: 2221

To delete files with name containing "138.096.000":

find /root/of/files -type f -name '*138.096.000*' -exec rm {} \;

To list files with names containing "00080":

find /root/of/files -type f -name '*00080*'

Upvotes: 1

Related Questions