Unix command to list all the files and directories in the current directory whose second character is a digit

4.1k Views Asked by At

My code for this is ls | grep .[0-9]*

And the output is showing as

d2
d4
di3
dir1
f1
f2
fil4
file3
g2t
g3t

The expected output is

d2
d4
f1
f2
g2t
g3t

I know i can directly use ls ?[0-9] but then my output order is different

f1  f2  g2t  g3t

d2:

d4:
3

There are 3 best solutions below

0
On BEST ANSWER

Okay this worked ls -d ?[0-9]*

0
On

This find command lists the files and directories in the current directory whose names have a second character that is a digit

find . -maxdepth 1 -name '?[0-9]*'
1
On

Try this , It will work

ls -1 | grep '^.[0-9][.]*'