0

This might be a silly one, but I'm unable to get the required output.

I've a file named Abc_sfgf_kjwefgk.txt20180929_040845 in a directory.

I'm trying to list the file using the command: ls -l Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}

But it's giving me error: ls: cannot access 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}': No such file or directory

While, with this command:ls -l Abc_sfgf_kjwefgk.txt[0-9]*?_[0-9]*?

It's giving me the correct result.

What's the problem with my initial command?

1
  • 2
    The problem is globs don't work like regular expressions. Commented Sep 29, 2018 at 11:06

2 Answers 2

1

Seems like everything is okay with your SECOND command. Here you correctly use globbing.

If you want to parse ls, then you should use smth like this:

ls | grep -E 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Arfeo, i think i was missing egrep..uhhh.
1

{8} is a literal string. Since you don't have any file names containing { the glob regex [0-9] does not expand. Your command is literally ls -l 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}'. Since there is no file Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4} you get an error message from ls, that there is no such file.

You probably wanted to write

ls -l Abc_sfgf_kjwefgk.txt[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]

or

find -maxdepth 1 -regextype egrep -regex '.*/Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}' \
     -exec ls -l {} \;

1 Comment

yeah, i tried the first one : ls -l Abc_sfgf_kjwefgk.txt[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9], then of course it seemed too lengthy...i thought there would be some better way to do it ;-). Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.