Vicky
Vicky

Reputation: 1328

Listing files with numeric names

How do I list files that have only numeric names e.g

1111 
2342 
763
71

I have tried ls -l [0-9]* but this seems to bring all file names that start with a digit and can have anything in their name after a digit.

Upvotes: 2

Views: 3502

Answers (4)

nagendra547
nagendra547

Reputation: 6302

This command will be safest option always!

ls | grep -E '^[0-9]+$'

Adding few sample test cases for audiences to make it clear.

$ls
1       12222e      2       B.class     Test.java   file.sh     y.csv
1.txt       1a      A.class     Test.class  db_2017-08-15   file.txt
$ ls  | grep -E '^[0-9]+$'
1
2

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With opposite operation : ignoring filenames with non-digits [^0-9]:

ls -1 --ignore=*[^0-9]*

--ignore=PATTERN
do not list implied entries matching shell PATTERN

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385580

First, turn on Bash's extglob option:

shopt -s extglob

Then use this pattern:

ls -l +([0-9])

Read more about extended pattern matching in the Bash Reference Manual.

Upvotes: 7

Kusalananda
Kusalananda

Reputation: 15603

Using GNU find:

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

This uses a regular expression rather than a filename globbing pattern. The expression ./[0-9]*$ matches ./ (the current directory) followed by only digits until the end ($).

If you need to stop find from going into subdirectories, add -maxdepth 1.

If you want ls -l -like behaviour, add -ls at the very end.

Upvotes: 2

Related Questions