In linux command line, how can we 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 we get the list of process ids on the system in linux command prompt?, where we’ve run ls on /proc/. now trying to exclude everything except the process ID directories.
- Using ls piped to grep -E (extended grep with additional regexp capabilities) to search for all filenames with only numeric characters:
- 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
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:
- However, this is not very optimal, as find searches recursively also in this case and only filters out the desired results afterwards.
- If you’re wanting to use ls, you could use:
- The * splat operator will match any [[:digit:]]. You could also use:
- 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.