bash 如何仅grep指定的文件夹名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7010193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:33:38  来源:igfitidea点击:

How to grep only specified folder names

bashgrepdirectoryls

提问by Roman

I have structure like this

我有这样的结构

201
202
203
204
205
206
2011-08-04_03-01-15
2011-08-05_03-01-15
2011-08-08_03-01-15
2011-08-09_03-00-02
2011-08-10_14-16-37

And I need grep only folders with date names like "2011-08-05_03-01-15" I tried ls | grep '201' but in output comes 201 folder to

我只需要 grep 日期名称为“2011-08-05_03-01-15”的文件夹我试过 ls | grep '201' 但在输出中有 201 文件夹到

way with ls | grep '2011' is do not acceptable, because it is a hardcode.

与 ls 的方式 | grep '2011' 是不可接受的,因为它是硬编码。

回答by Costi Ciudatu

You can do that without grep:

你可以在没有 grep 的情况下做到这一点:

ls -d ????-??-??_??-??-??

This works fine assuming you don't have other folder names following the same pattern without being dates, like aaaa-bb-cc_dd-ee-ff, as those will also be listed by the above command.

假设您没有遵循相同模式的其他文件夹名称而不是日期,aaaa-bb-cc_dd-ee-ff这将正常工作,例如,因为上面的命令也会列出这些名称。

You can even be less restrictive and list everything that has a -on the 5th position:

你甚至可以不那么严格,列出所有-在第 5 位有 的东西:

ls -d ????-*

回答by Prince John Wesley

With grep:

grep

ls -d */ | grep -E '[0-9]{4}(-[0-9]{2}){2}_([0-9]{2}-){2}[0-9]{2}'


With find:

find

find * -regextype posix-extended \ 
       -regex '[0-9]{4}(-[0-9]{2}){2}_([0-9]{2}-){2}[0-9]{2}' \
       -type d

回答by gpojd

ls | egrep '^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}$'

回答by jDourlens

Try with regExp:

尝试使用正则表达式:

ls | grep -E "....--_*--"

ls | grep -E "....- -_*- -"

回答by Vijay

You have all the information in man pages. Have a look at find

您在手册页中有所有信息。看看找到