I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
bashregexrmunix
I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
Best Answer
You can use the following command to delete all files matching your criteria:
How it works:
ls
lists all files (one by line since the result is piped).grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.xargs -d"\n" rm
executesrm line
once for everyline
that is piped to it.For starters,
rm
doesn't accept a regular expression as an argument. Besides the wildcard*
, every other character is treated literally.Also, your regular expression is slightly off. For example,
*
means any occurrences of...
in a regular expression, soA*
matchesA
,AA
, etc. and even an empty string.For more information, visit Regular-Expressions.info.