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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Irish Jet
  • 19
  • 2
  • 1
    Related: https://unix.stackexchange.com/questions/134684/what-is-the-linux-command-to-display-how-many-file-names-in-the-directory-end-in – Jeff Schaller Jan 19 '19 at 16:06
  • Also: https://unix.stackexchange.com/questions/234201/regex-file-name-with-multiple-digits – Jeff Schaller Jan 19 '19 at 16:06
  • This command line should do it. `find . -name "*[0-9][0-9][0-9][0-9][0-9]*"`. It can probably be simplified. If there must be exactly 5 digits, that expression will be more complicated. – sudodus Jan 19 '19 at 16:08
  • See also https://unix.stackexchange.com/q/414226/117549 – Jeff Schaller Jan 19 '19 at 16:18
  • If you want to search subdirectories as well: `find -E directory/ -regex '.*[0-9]{5,}.*'` – Nick Garvey Jan 19 '19 at 17:46
  • If any of the answers solved your problem, please [accept it](https://unix.stackexchange.com/help/someone-answers) by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 27 '19 at 15:34

2 Answers2

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:]]*
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
-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}'
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14