grep命令删除所有dirs,而不仅仅是dirs匹配正则表达式?
我正在玩Linux / grep命令,并且正在创建一个“清理”bash脚本,它将删除不匹配特定正则表达式模式的目录。
该命令是: 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
并且图像中没有突出显示的所有目录名称应该是被删除的目录名称; 但是,上述命令最终会删除当前目录中的所有文件夹/文件
我的正则表达式模式或管道命令的方式有问题吗? 我尝试删除rm -rf
,但是结果是根本没有被删除。
正则表达式模式匹配目录名称,如下所示:
以下是我在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!
是的,你的正则表达式是错误的。 以下命令适用于我:
ls | grep -v "[[a-zA-Z]*]" | xargs -d"n" rm -rf
请参阅grep
命令,使用g
修饰符是没有意义的,因为grep
只在每次正则表达式模式中输入一行。 另外, {0,}
与*
量词相同。
提示 :在尝试删除所有目录之前,根据您的设置,可能会有麻烦,只需运行匹配命令的反转命令即可:
ls | grep "[([a-zA-Z]){0,}]/g"
这样你就会知道不会被删除的目录列表。 在你的原始正则表达式中,列表是空的。
这个命令会保存比图像中显示的更多的目录,因为你的正则表达式不仅仅匹配这些目录,而且我敢打赌你已经知道了。 你可以在这里看到匹配目录的整个列表。
链接地址: http://www.djcxy.com/p/78061.html上一篇: grep command deletes all dirs and not just dirs matching regex?