Search code examples
regexshellls

regex for ls -ld


I have a directory consists of different types of files, including directory and individual files with similar names - so pattern must be very exact.

I need to use ls -ld to just to extract directories, and look for particular patterned directory. After getting this, I need to find out what that particular string_w_pattern is to store as variable.

I am running this in sh.

Known string is my_dir and my_prop_dir

/my_dir/string_w_pattern/my_prop_dir

string_w_pattern consists of 2-3 characters in capital letters with one number always comes on the second place.

I have tried following but getting more than two output since names for string_w_pattern are similar.

ls -ld /my_dir/[A-Z]*[A-Z0-9]\/my_prop_dir

Solution

  • The shell uses glob expression syntax here, not regular expresssion. The * will expand like the regex .*. Also, drop the \ masking your /. Try this:

    ls -ld /my_dir/[A-Z][A-Z][0-9]/my_prop_dir /my_dir/[A-Z][A-Z][A-Z][0-9]/my_prop_dir
    

    To filter out the desired string from the result, use sed or cut.