Linux - NewbieThis 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!
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.
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/
Or say you wanted to list all the files with Robert Smith's last name in the file name, you would:
$ ls *smith*
From RUTE - Users Tutorial and Exposition:
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.
EDIT: Grapefruttgirl answered while I was creating this post. Well, extra information never hurts.
The reason you did not find this in the lsman 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
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.
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
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
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
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.
[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
[blackhole@machine testdir]$ ls -a *.txt
[blackhole@machine testdir]$ ls .*.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.
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.
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.
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.