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.
Asked
Active
Viewed 2,482 times
1
2 Answers
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
- 67,283
- 35
- 116
- 255
-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,211
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:08find -E directory/ -regex '.*[0-9]{5,}.*'
– Nick Garvey Jan 19 '19 at 17:46