Bash Pattern Matching with rm command -
i have directory need remove subdirectories contain space , number in directory name. how pattern match given parameters? need 1 rm command. thanks
just include 2 patterns in same rm command:
rm -rf -- \ *" "*[0-9]*/ \ # 1 gets directories spaces before numbers... *[0-9]*" "*/ # ...and 1 gets directories numbers before spaces. if 1 of 2 classes exists, class doesn't exist doesn't expanded (unless nullglob shell option enabled). that's harmless, though, since rm -f ignores arguments don't exist. so, let's had file foo 5, no files spaces following numbers. when run this, shell following:
rm -rf -- "foo 5" "*[0-9]* */" ...which harmless rm, might cause trouble non-rm program requires arguments it's passed exist.
to work around that, enable nullglob option:
shopt -s nullglob ...and non-matching patterns deleted.
another interesting case when want avoid duplicate names. instance, client 15 jenkins matches both *" "*[0-9]* , *[0-9]*" "*, if put both patterns on single line, file listed twice. can avoid using bash extglobs:
shopt -s extglob # turn on extended globbing rm -rf -- *@([0-9]*" "|" "*[0-9])*/ # ...and emit 1 result
Comments
Post a Comment