How can I get files with numeric names using ls command?
Solution 1
Using ls
piped to grep -E
(extended grep with additional regexp capabilities) to search for all filenames with only numeric characters:
ls | grep -E '^[0-9]+$'
Solution 2
If you're wanting to use ls
, you could use:
ls *[[:digit:]]*
The *
splat operator will match any [[:digit:]]
. You could also use:
ls *[0-9]*
Which also matches file or directory with a digit 0-9.
If you have subdirectories that match the glob pattern, you can use the -d
switch to make ls not recurse into them.
Solution 3
Despite of your question title, I want to present you a solution with find
. It has a regex option (-regex
), so [0-9]*
will match file names consisting solely out of digits.
To find only files (-type f
) recursively below the current directory (.
) use
find . -type f -regex ".*/[0-9]*"
The .*/
in the regex is necessary, because regex "is a match on the whole path, not a search." (man find
). So if you want to find only files in the current dir, use \./
instead:
find . -type f -regex "\./[0-9]*"
However, this is not very optimal, as find searches recursively also in this case and only filters out the desired results afterwards.
Related videos on Youtube
techfun
Updated on September 18, 2022Comments
-
techfun over 1 year
In linux commandline, how can I list down only numeric(only names with 0 to 9) file names in the current directory?
This is a follow-on question to How can I get the list of process ids on the system in linux command prompt?, where I've run
ls
on/proc/
. I'm now trying to exclude everything except the process ID directories. -
ckujau about 10 yearsNote: this will list files starting with a digit. Add another "*" at the end to match also files with digits inbetween.
-
suspectus about 10 yearsThese patterns will only match files which end with a numeric character.
-
techfun about 10 yearsHello, Thanks for reply. I have some confusion with ls *[0-9] I am getting list of files inside sub directories and files with numeric. And for sub directories it is again listing all directories.
-
suspectus about 10 yearswhy downvote please?
-
user about 10 years@techfun Add the
-d
switch. -
user about 10 yearsNot my downvote, but I strongly suspect (no pun intended) that it's because you shouldn't parse the output of
ls
. Even more so in a case like this, where perfectly valid alternatives exist (see for example mpy's answer). -
suspectus about 10 yearshmm... despite
ls
specifically requested by OP. Thanks for link. -
user about 10 yearsYou're right, the question title says
ls
, but the question itself is much broader and only asks how to list files with numeric file names from the command line.ls
is one way to do that (and it's one way to list files), but far from the only one. Try e.g.echo *
some time. -
Victoria Stuart over 6 yearsI think this is a really excellent answer; thank you for the recursive / mon-recursive versions!
-
phuclv over 6 years
-
phuclv over 6 yearsso what makes it different from tijko's answer?
-
MOHRE over 6 yearstijko's answer does not work correctly. It would match a2g.test but my answer just match numbers.
-
MOHRE over 6 yearsAs @techfun mentioned "only numeric(only names with 0 to 9) file names" tijko's answer doesn't fit.