Reputation: 2645
The pattern below is supposed to delete all files that dont start with 1_
but instead it matches all files that don't contain 1
.
For example, it'll not match 11_xxx.sql.bz2
and 1_xxx.sql.bz2
but will match all the others correctly.
How can I ensure the pattern only matches the exact number and not any number which contains the number?
For example, i would like the script below only to not match 1_xxx.sql.bz2
ls | grep -P "^[^1]+_([^_]+).+$" | xargs -d"\n" rm
Upvotes: 1
Views: 106
Reputation: 785098
I will need to keep items without a number at the start
I suggest using find
like this to match all files in current directory excluding those that start with 1_
:
find . -maxdepth 1 -type f -name '[0-9]*' -not -name '1_*' -delete
If your find
doesn't support -delete
then use:
find . -maxdepth 1 -type f -name '[0-9]*' -not -name '1_*' -exec rm {} +
Upvotes: 4
Reputation: 780889
use grep -v
to invert the match, so you exclude files that match the pattern.
grep -v '^1_'
Upvotes: 0