How can I get files with numeric names using ls command?

40,803

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.

Share:
40,803

Related videos on Youtube

techfun
Author by

techfun

Updated on September 18, 2022

Comments

  • techfun
    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
    ckujau about 10 years
    Note: this will list files starting with a digit. Add another "*" at the end to match also files with digits inbetween.
  • suspectus
    suspectus about 10 years
    These patterns will only match files which end with a numeric character.
  • techfun
    techfun about 10 years
    Hello, 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
    suspectus about 10 years
    why downvote please?
  • user
    user about 10 years
    @techfun Add the -d switch.
  • user
    user about 10 years
    Not 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
    suspectus about 10 years
    hmm... despite ls specifically requested by OP. Thanks for link.
  • user
    user about 10 years
    You'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
    Victoria Stuart over 6 years
    I think this is a really excellent answer; thank you for the recursive / mon-recursive versions!
  • phuclv
    phuclv over 6 years
  • phuclv
    phuclv over 6 years
    so what makes it different from tijko's answer?
  • MOHRE
    MOHRE over 6 years
    tijko's answer does not work correctly. It would match a2g.test but my answer just match numbers.
  • MOHRE
    MOHRE over 6 years
    As @techfun mentioned "only numeric(only names with 0 to 9) file names" tijko's answer doesn't fit.