Search code examples
bashgrepdirectoryls

How to grep only specified folder names


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.


Solution

  • 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