1

Normally, I would ask a professor or classmate this, but as it's a Saturday and everyone is gone. I have to find a file in a directory that has five digits in a row somewhere in the file name. They don't have to be ascending or descending digits.

5

Wildcards (or globs) can accomplish this, with a numeric range:

ls -d /path/to/directory/*[0-9][0-9][0-9][0-9][0-9]*

This tells the shell to look in /path/to/directory for filenames that start with:

  • * -- anything (or nothing)
  • [0-9] -- a digit
  • (four more digits)
  • * -- and ending in anything (or nothing)

That list of filenames is then passed to ls to list them.

More expansively, bash also allows character classes as wildcards, so if you have numbers in your language that aren't covered by [0-9], you could use:

ls -d *[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]*
| improve this answer | |
-1

I have achieved by below method and i worked fine

find . -maxdepth 1  -type f| sed "s/\.\///g"| awk -F "." '{print $1}'|sed '/^$/d'| awk '/[0-9]/{print $0}'| awk '{print $1,gsub("[0-9]",$1)}'| awk '$2 == 5 {print $1}'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.