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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
![Reply](https://www.linuxquestions.org/questions/images/buttons/reply.gif) |
05-13-2010, 05:15 PM
|
#1
|
Member
Registered: Jun 2009
Posts: 111
Rep:
|
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!
|
|
|
05-13-2010, 05:16 PM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
ls [0-9]*
|
|
1 members found this post helpful.
|
05-13-2010, 05:43 PM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
05-13-2010, 09:10 PM
|
#4
|
Member
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862
Rep:
|
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.
|
|
|
05-13-2010, 09:56 PM
|
#5
|
Member
Registered: Apr 2010
Posts: 82
Rep:
|
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
|
|
|
05-13-2010, 10:42 PM
|
#6
|
Member
Registered: Feb 2003
Location: Florida
Distribution: Fedora 18
Posts: 862
Rep:
|
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.
|
|
|
05-14-2010, 01:28 AM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
11-14-2016, 11:10 AM
|
#8
|
Member
Registered: Jun 2009
Posts: 111
Original Poster
Rep:
|
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
|
|
|
11-14-2016, 11:13 AM
|
#9
|
Member
Registered: Jun 2009
Posts: 111
Original Poster
Rep:
|
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)
|
|
|
11-14-2016, 12:35 PM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,030
|
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:
Which is clearly not between 20 - 29
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 02:27 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|