LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2010, 04:15 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
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!
 
Old 05-13-2010, 04:16 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
ls [0-9]*
 
1 members found this post helpful.
Old 05-13-2010, 04:43 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
1 members found this post helpful.
Old 05-13-2010, 08:10 PM   #4
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 90
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.
 
Old 05-13-2010, 08:56 PM   #5
kurwongbah
Member
 
Registered: Apr 2010
Posts: 82

Rep: Reputation: 23
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
 
Old 05-13-2010, 09:42 PM   #6
SharpyWarpy
Member
 
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862

Rep: Reputation: 90
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.
 
Old 05-14-2010, 12:28 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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])
 
1 members found this post helpful.
Old 11-14-2016, 10:10 AM   #8
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Cool

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
 
Old 11-14-2016, 10:13 AM   #9
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
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)
 
Old 11-14-2016, 11:35 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do inode numbers start from 1 and not 0 ? manavendra Programming 10 01-07-2010 06:33 AM
subtract total numbers of files from 2 directories Michael235 Linux - Newbie 2 03-28-2009 05:49 PM
Copying large numbers of files dman65 Linux - General 8 02-26-2009 07:32 PM
Best way to organize large numbers of files. dman65 Linux - General 4 03-12-2008 07:42 PM
sending files using isp numbers dr_zayus69 Linux - Networking 5 10-27-2004 08:18 PM


All times are GMT -5. The time now is 05:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration