Sunday, January 13, 2008

Finding file names with only digits, no text

A recursive way to list files whose filename contain only digits would be:

$ find . -type f -name '[0-9]*'


Oh!, its matching files whose name contain digits as well as digits with text.
Some help from "awk" to filter only those files whose name contain only digits and no text. And xargs to do a "ls -l" of them.

$ find . -type f -name '[0-9]*' | awk '/\/[0-9]+$/{print}' | xargs ls -l
-rw-r--r-- 1 jaduks staff 32 Jan 13 23:11 ./323321
-rw-r--r-- 1 jaduks staff 20 Jan 13 23:11 ./323332
-rw-r--r-- 1 jaduks staff 50 Jan 13 23:19 ./newdir/123

A non-recursive way is this (only to list file names with only digits in present dir)
$ ls [0-9]* | awk '/^[0-9]+$/{print}'


Erol said...

You could for non-recursive search also use find with 'maxdepth' switch.
find . -maxdepth 1 -type f -name '[0-9]*'

Jadu Saikia said...

thanks a lot.
ya, maxdepth is there; the time I wrote the post I was not aware of maxdepth in find. Thanks a lot for posting.

// Jadu

Anirudh said...

> find . -type f -name '[0-9]*'

> ls [0-9]* | ...

You are confusing wildcards with regular expressions.
[0-9]* -> is a wildcard(or, a glob)
which just ensures that the first char. is a digit and there can be any kind & number (even none) after this first digit.

Here's one way to deal with this issue, all within find,

find . -type f ! -name '*[!0-9]*'

This would print all the files (recursively) that dont have even one nondigit, IOW, they have all digits.

© Jadu Saikia