LinuxQuestions.org
Visit the LQ Articles and Editorials section
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-17-2007, 09:55 PM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
ls command question...


i've been reading man pages for the ls command, but i was not able to find the option that would perfectly suite what i mean...maybe i just missed something or was not able to interpret that certain option...here's the question, how would you view all the files in the folder with the specific file extension...like for example, view all files with .txt extension...thanks...peace \m/
 
Old 05-17-2007, 10:02 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
try 'ls -a *.txt' or 'dir -a *.txt'

you could also do 'ls -a --file-type txt' (or '.txt' I'm not sure if it needs the period. )

Last edited by GrapefruiTgirl; 05-17-2007 at 10:03 PM.
 
Old 05-17-2007, 10:08 PM   #3
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by GrapefruiTgirl
try 'ls -a *.txt' or 'dir -a *.txt'

you could also do 'ls -a --file-type txt' (or '.txt' I'm not sure if it needs the period. )
thank you very much...
 
Old 05-17-2007, 10:10 PM   #4
vtel57
Member
 
Registered: Jul 2006
Location: Tampa, FL, USA
Distribution: Slackware64
Posts: 852

Rep: Reputation: 88
Use a wildcard...

If you wanted to list all the .conf files in say the /etc directory, you would:

Code:
 $ ls /etc/*.conf
The output would be something like:

Quote:
adduser.conf fdmount.conf libao.conf resolv.conf
adduser.local.conf ffserver.conf logrotate.conf sbackup.conf
ca-certificates.conf hdparm.conf mke2fs.conf scrollkeeper.conf
chkrootkit.conf host.conf mplayerplug-in.conf sensors.conf
debconf.conf inetd.conf nsswitch.conf sysctl.conf
deluser.conf kernel-img.conf pam.conf syslog.conf
discover.conf kernel-pkg.conf pnm2ppa.conf ucf.conf
evms.conf ld.so.conf prelink.conf updatedb.conf
Or say you wanted to list all the files with Robert Smith's last name in the file name, you would:

Code:
 $ ls *smith*
From RUTE - Users Tutorial and Exposition:

Quote:
Wildcards, names, extensions and glob expressions
ls can produce a lot of output if there are a large number of files in a directory. Now say that we are only interested in files that ended with the letters tter. To list only these files you can use ls *tter. The * matches any number of any other characters. So, for example, the files Tina.letter, Mary_Jones.letter and the file splatter, would all be listed if they were present. While a file Harlette would not be listed. While the * matches any length of characters, then ? matches only one character. For example the command ls ?ar* would list the files Mary_Jones.letter and Harlette.
Hope that helped you out.

Have FUN!

EDIT: Grapefruttgirl answered while I was creating this post. Well, extra information never hurts.

Last edited by vtel57; 05-18-2007 at 02:12 PM.
 
Old 05-17-2007, 10:17 PM   #5
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
The reason you did not find this in the ls man page is because ls is not what is handling the wildcard(s). The resolution of wildcards (called globbing) in this case is done by your shell (probably bash), not by the ls command. (This is different than some other OSes such as MS DOS/Windows.) The shell will do this with any command, which is frequently convenient, but can get you into trouble if that is not what you want. If you don't want the shell handling the wildcard(s), you must enclose the expression with the wildcard in either double or single quotes. But quoting is a whole other (involved) story ...

I know this is a little more than you asked, but I hope it will be helpful as you continue exploring. Enjoy.

EDIT: On reviewing, I think I could have been a bit more clear. If you type

ls *.txt

and your working directory has three files that match (file1.txt file2.txt file3.txt) the shell will replace the the wildcard expression with the actual filesnames that match, so as far as the ls command is concerned, it was as if you typed:

ls file1.txt file2.txt file3.txt

But all of this goes on behind the scenes, so the output of what you actually typed will ouput:

file1.txt file2.txt file3.txt

I hope that helps and doesn't totally confuse you.

Last edited by blackhole54; 05-17-2007 at 10:25 PM.
 
Old 05-17-2007, 10:28 PM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Actually, vtel57 and blackhole both provided useful info too -- thanks for that, from me.
Quote:
Originally Posted by vtel57
EDIT: GrapefruiTgirl answered while I was creating this post. Well, extra information never hurts.
.. it sure doesn't hurt thanks again, I didn't know a lot of the details provided there.
 
Old 05-17-2007, 10:48 PM   #7
vtel57
Member
 
Registered: Jul 2006
Location: Tampa, FL, USA
Distribution: Slackware64
Posts: 852

Rep: Reputation: 88
Yes, indeed! Blackhole's additional posting was enlightening to me also.

Thanks!

~Eric
 
Old 05-18-2007, 01:51 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by vtel57
Use a wildcard...

If you wanted to list all the .conf files in say the /etc directory, you would:

Code:
 $ ls *.conf /etc
Which version of ls would do this?

All the ones I know will display all conf files in the
current dir, and *ALL* files in /etc ....

The desired result would be achieved with
Code:
ls /etc/*conf


Cheers,
Tink
 
Old 05-18-2007, 02:38 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Sorry, I misread the quote above as a question.

If you wanted to show .conf files in both /etc/ and the current directory you could use:
ls *.conf /etc/*.conf

Last edited by jschiwal; 05-18-2007 at 02:42 AM.
 
Old 05-18-2007, 11:01 AM   #10
vtel57
Member
 
Registered: Jul 2006
Location: Tampa, FL, USA
Distribution: Slackware64
Posts: 852

Rep: Reputation: 88
I'm using ls ver. 5.97.

Code:
$ ls /etc/*.conf
Gives me output only showing the .conf files within /etc.

*Using Konsole in Slackware.

Last night, when I posted the original answer above, I was using Konsole in Mepis. I don't know the ls ver in that distro, but its output was similar... .conf files only.

Regards,

~Eric
 
Old 05-18-2007, 02:06 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by vtel57
I'm using ls ver. 5.97.

Code:
$ ls /etc/*.conf
Gives me output only showing the .conf files within /etc.

*Using Konsole in Slackware.

Last night, when I posted the original answer above, I was using Konsole in Mepis. I don't know the ls ver in that distro, but its output was similar... .conf files only.

Regards,

~Eric
But that is what you initially said yours does?

Quote:
If you wanted to list all the .conf files in say the /etc directory, you would:

Code:
$ ls *.conf /etc


Cheers,
Tink
 
Old 05-18-2007, 02:15 PM   #12
vtel57
Member
 
Registered: Jul 2006
Location: Tampa, FL, USA
Distribution: Slackware64
Posts: 852

Rep: Reputation: 88
Ooooops! Tink... you are correct. I typed in incorrectly in my first post here. I've edited to correct that. The proper way to use the wildcard term with ls to list all .conf files in /etc would be:

Code:
 $ ls /etc/*.conf
 
Old 05-19-2007, 01:51 AM   #13
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by GrapefruiTgirl
try 'ls -a *.txt' or 'dir -a *.txt'

you could also do 'ls -a --file-type txt' (or '.txt' I'm not sure if it needs the period. )
I wasn't at a Linux machine on my first post, and I wanted to have a chance to double check some things before I posted the following . But I thought I should clarify some things about the above quote. Specifically, the -a option in the above examples doesn't do anything, and the --file-type option doesn't do what is suggested. Let me explain.

Any file or directory whose name starts with a period is "hidden" in the sense that it is not displayed when you do a directory listing unless you include a -a or -A parameter. The difference between these is that -a will display everything while -A does not display the special directory entries . and .. that are present in each directory. However, if you explicitly list the files you want ls to list, with or w/o wildcards, then neither of these options does anything. Therefore

Code:
ls -a *.txt
will just list the non-hidden files that end with .txt even though you specified -a. If you want both hidden and non-hidden files ending with .txt, then you must use

Code:
ls  *.txt .*.txt
where, you will note, I didn't even bother with the -a.

Since, as I mentioned before, the shell is the one actually doing the wildcard expansion, the same is true for other commands such as cp, mv, and rm. So that

Code:
cp *.txt other_dir/
will only copy the non-hidden files ending in .txt.

The option --file-type has to do with listing format, not with selection of files to list. For example, as illustrated below, directories will have a slash appended to their name. Type info ls and go to General output formatting for a full explanation. (If you are unfamiliar with info, type info info to learn about it.)

These points are illustrated below. For ease of reading (I hope!) I have made the commands I typed bold faced and I have added some blank lines.

Code:
[blackhole@machine backhole]$ mkdir testdir
[blackhole@machine backhole]$ cd testdir
[blackhole@machine testdir]$ touch file1.txt file2.txt .file3.txt .file4.txt
[blackhole@machine testdir]$ mkdir dir1 dir2 .dir3 .dir4
[blackhole@machine testdir]$ ls  
dir1  dir2  file1.txt  file2.txt

[blackhole@machine testdir]$ ls -a
.   .dir3  .file3.txt  dir1  file1.txt
..  .dir4  .file4.txt  dir2  file2.txt

[blackhole@machine testdir]$ ls -A
.dir3  .dir4  .file3.txt  .file4.txt  dir1  dir2  file1.txt  file2.txt

[blackhole@machine testdir]$ ls --file-type
dir1/  dir2/  file1.txt  file2.txt

[blackhole@machine testdir]$ ls *.txt
file1.txt  file2.txt

[blackhole@machine testdir]$ ls -a *.txt
file1.txt  file2.txt

[blackhole@machine testdir]$ ls .*.txt
.file3.txt  .file4.txt

[blackhole@machine testdir]$ ls *.txt .*.txt
.file3.txt  .file4.txt  file1.txt  file2.txt
EDIT: I just discovered that by default, Ubuntu doesn't have info pages installed so typing info ls just displays the man page for ls which is a little less explicit than the info page. Bummer! I imagine you can install some package to get it. I don't know what other distros (probably at least Debian) do the same thing.

Last edited by blackhole54; 05-19-2007 at 02:15 AM.
 
Old 05-19-2007, 06:44 AM   #14
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
ls: to '-a', or not to '-a' ----- that is the question;

@ Blackhole -- your post makes this so clear, it should almost be stickied -- thanks for this bit of experimentation.
I had based my answer to the OP (regarding ls) solely on the ls man page, but personally I use the 'dir' command nine times out of ten, and it does what I expect when using the -a flag; ie, it lists 'hidden' files as well as 'non-hidden' ones. For the record, I don't often pay any attention to file extensions since switching to Linux , and also, as with so many things, "the devil is in the details" which unfortunately, the man page seems to have neglected to include this time.
Based on your findings here, I think I can get by from here on without the -a flag!
Cheers, & thanx again.
~ Sasha
 
Old 05-19-2007, 10:16 AM   #15
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by GrapefruiTgirl
but personally I use the 'dir' command nine times out of ten, and it does what I expect when using the -a flag; ie, it lists 'hidden' files as well as 'non-hidden' ones.
TMK, the ls command and dir command work the same. In fact, when I first investigated such things, I figured dir was a symlink to ls. But in fact, on that system at any rate they were two distinct binaries. From multiple OSes (not just MS!) I was used to using dir to list directories. But when I started using Linux, I decided "when in Linuxland, do as the Linuxlanders do!" (I hope that none of my old English teachers ever find out I used the word Linuxlanders )

In any case the issue with -a seems to be whether or not you name any files on the command line. And my real concern was that somebody would think that -a with a wildcard would list the corresponding hidden files.

Quote:
For the record, I don't often pay any attention to file extensions since switching to Linux , ...
I hear ya. In fact, in traditional *nix there really isn't such a thing as an extension since the period is just another character. My impression (which could be wrong) is that extensions are now starting to play a bit more of a role in the desktop metaphor. But I have been using old software and just recently got a new computer where I can run current software. So hopefully I will slowly start to learn about such things.

Thanks for the kind words.
 
  


Reply

Tags
globbing, ls


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
command question satimis Debian 2 07-27-2006 03:41 PM
mv command question skilletlicker Linux - General 2 02-03-2006 01:22 PM
Command question satimis Fedora 3 04-04-2005 12:24 AM
who command question Malique Linux - Newbie 2 03-22-2004 02:18 AM
ls -al command question Stingreen Linux - Networking 5 02-14-2002 11:32 AM


All times are GMT -5. The time now is 03:28 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration