grep命令删除所有目录而不只是dirs匹配正则表达式?

时间:2016-03-03 23:49:35

标签: regex bash grep

我正在使用Linux / grep命令并正在创建一个“清理”bash脚本,它会删除匹配特定正则表达式模式的目录。

命令为:ls | grep -v "\[([a-zA-Z]){0,}\]/g" | xargs -d"\n" rm -rf

此处图片中的所有目录名都突出显示,应该是删除的;但是,上面的命令最终删除所有当前目录中的文件夹/文件

我的正则表达式模式或管道命令的方式有问题吗?我已尝试删除rm -rf,但这样就不会删除任何内容。

正则表达式模式匹配目录名称,如下所示:enter image description here

这是我在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!

1 个答案:

答案 0 :(得分:2)

是的,你的正则表达式是错误的。以下命令对我有用:

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

请参阅grep命令,使用g修饰符没有意义,因为grep每次运行正则表达式模式时只输入一行输入。此外,{0,}*量词相同。

提示:在尝试删除所有目录之前,根据您的设置可能会遇到麻烦,只需运行匹配命令的反向,如下所示:

ls | grep "\[([a-zA-Z]){0,}\]/g"
这样你就可以知道的目录列表将被删除。对于原始正则表达式,列表为空。

这个命令比图像中显示的目录更多,因为你的正则表达式不仅仅匹配那些目录,但我打赌你已经知道了。您可以看到匹配目录的完整列表here

相关问题