grep command deletes all dirs and not just dirs matching regex?

I'm playing around with Linux/grep commands and am creating a "cleanup" bash script, that removes directories that don't match a specific regex pattern.

The command is: ls | grep -v "[([a-zA-Z]){0,}]/g" | xargs -d"n" rm -rf ls | grep -v "[([a-zA-Z]){0,}]/g" | xargs -d"n" rm -rf

And all the directory names in the image here that do not have something highlighted, should be the ones that are removed; however, the command above ends up deleting all the folders/files in my current directory

Is there something wrong with my regex pattern or the way I pipe commands? I've tried removing rm -rf , but that results in nothing being deleted at all.

The regex pattern matches directory names like so: 在这里输入图像描述

And here is the group of directory names I used for testing on http://regex101.com

Challenge #96 [difficult] (Water Droplets)/
Challenge #96 [easy] (Controller Chains)/
Challenge #96 [intermediate] (Parsing English Values)/
Challenge #99 [difficult] (Animated unemployment map of the United States)/
Challenge #99 [easy] (Words with letters in alphabetical order)/
Challenge #99 [intermediate] (Unemployment map of the United States)/
Challenge 208 [Bonus] The Infinite Stallman Theorem/
Challenge#172 [Intermediate] Image Rendering 101...010101000101/
Challenge#180 [Easy] Look'n'Say/
Contest #1 - IDE Intellisense/
EXTENSIONS: Week-Long Challenge #1 due FRIDAY!
Honour Roll #1/
New moderator needed/
News, Mods, getting Wiki with it/
REMINDER: Week-Long Challenge #1 due today!
There are gonna be some changes here/
This isn't a challenge, just a thank you/
WINNERS: Week-Long Challenge #1
WINNERS: Week-Long Challenge #2
Want to contribute to this subreddit?
We need some feedback!/
Week-Long Challenge #1: Make a (tiny) video game!
[Discussion] Challenge tags [Easy] [Intermediate] [Hard]/
[Easy] Longest Two-Character Sub-String/
[Extra] Poetic Justice/
[Mod Post] Do you want a 4-hour, 24-hour, or 48-hour programming challenge set?
[Request] The Ultimate Wordlist/
[Weekly #11] Challenges you want/
[Weekly #12] Learning a new language/
[Weekly #16] Standards and Unwritten Standards/
[Weekly #17] Mini Challenges/
[Weekly #21] Recap and Updates/
[Weekly #22] Machine Learning/
[Weekly #23] Computational Complexity and Algorithm Design/
[Weekly #24] Mini Challenges/
[Weekly #2] Pre-coding Work/
[Weekly #6] Python Tips and Tricks/
[Weekly #8] Sorting algorithms/
[Weekly] #1 -- Handling Console Input/
[difficult] challenge #1/
[difficult] challenge #2/
[easy] challenge #1/
[easy] challenge #2/
[intermediate] challenge #2/
challenge #3 [difficult]/
cleanup-clone.sh*
cleanup.sh*
for the artistically inclined... Have something extra!

Yes, your regex is wrong. The following command worked for me:

ls | grep -v "[[a-zA-Z]*]" | xargs -d"n" rm -rf

See, with the grep command there's no sense in using the g modifier, since grep is only fed one line of input per run of the regex pattern. Also, {0,} is the same as the * quantifier.

Tip : before trying to delete all directories, what could be troublesome depending on your settings, just run the inverse of you matching command, like so:

ls | grep "[([a-zA-Z]){0,}]/g"

this way you're going to know the list of directories that are not going to be deleted. In the case of your original regex, the list is empty.

This command spares more directories than the ones shown in your image, because your regex matches more than just those directories, but I bet you already knew that. You can see the whole list of matched directories here.

链接地址: http://www.djcxy.com/p/78062.html

上一篇: 从模式文件grep到搜索目录不工作

下一篇: grep命令删除所有dirs,而不仅仅是dirs匹配正则表达式?