質問

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

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

役に立ちましたか?

解決

With grep:

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

With find:

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

他のヒント

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

Try with regExp:

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

You can do that without 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.

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

ls -d ????-*

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top