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]*'

./323321
./3233221.txt
./323332
./newdir/123
./newdir/1234.out

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}'

2 comments:

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...

@Erol,
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

© Jadu Saikia www.UNIXCL.com