LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to ls files that start with numbers (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-ls-files-that-start-with-numbers-807671/)

Drigo 05-13-2010 04:15 PM

How to ls files that start with numbers
 
Hello All,
I want wondering if I can ls files that start with numbers only in a folder.


Suppose:
001.txt
302.txt
dsadf.txt
defa.txt
648.txt
wqre.txt

I want to execuste: ls "something"

and get...

001.txt
302.txt
648.txt

Thanks!

catkin 05-13-2010 04:16 PM

ls [0-9]*

pixellany 05-13-2010 04:43 PM

ls | grep '^[0-9]'

This and catkin's both screen for 1 digit at the beginning of the line. You can modify them to specify other patterns / criteria

SharpyWarpy 05-13-2010 08:10 PM

Okay I've got to say/ask something here. The OP said files that start with numbers only. So I'm wondering if he got the right answer. Let's assume I want to list files with only numbers, none with letters too. How would I do that? Pixellany, I can't get your command to work with Fedora.

kurwongbah 05-13-2010 08:56 PM

ls | grep -E '^[0-9]+\.' # find files starting with at least 1 digit and have only digits before the first decimal point (.)
ls | grep -E '^[0-9]+' # find files starting with at least 1 digit

SharpyWarpy 05-13-2010 09:42 PM

Pixellany, I apologize. I had forgotten to negate the alias I have set for ls - ls --color=always - with the backslash.
\ls | grep '^[0-9]' works. Thanks. I have been researching the same sort of information so I want to say thanks also to the OP for starting this thread.

catkin 05-14-2010 12:28 AM

A list of files with names comprising only numbers may be generated by bash itself using bash' Filename Expansion, Pattern Matching if extended globbing is enabled. The list can be used as arguments to any command including echo and ls:
Code:

shopt -s extglob
echo +([0-9])
/bin/ls +([0-9])


Drigo 11-14-2016 10:10 AM

Thank you all. To continue the conversation, I have a list:

pbs_1
pbs_2
pbs_3
.
.
.
pbs_20
pbs_21
pbs_23
.
.
.
pbs_51
.
.
.


How do I ls (probably using globbing) only those files that start at pbs_job_20 and go all the way to pbs_job_29 ?

If I do ls pbs_job_2* then I'll also get pbs_job_2.XX which is not needed.
Thanks in advance,
Rodrigo

Drigo 11-14-2016 10:13 AM

lol...I will reply to myself (for future readers)...


The answer should be: ls pbs_[2-9*]*[2-9]* (so two [] [] in the ls argument)

grail 11-14-2016 11:35 AM

Firstly, reopening such an old thread is not really the right way to go about this, you should simply open a new question.

As to your solution, you might want to check it again, currently it will also find the following file:
Code:

pbs_222555888
Which is clearly not between 20 - 29


All times are GMT -5. The time now is 01:53 AM.