Linux

Get more out of the ls command


The ls command is a well-known and often used command-line program that is used to list directory contents by name; in fact, it could be argued that it is the most used Linux command-line program.

$ ls
MD5SUMS  annvix-netinstall-i586-2.0-RELEASE.iso  bin  mc-vdanen

In addition to its most basic use, the ls command has a number of options that will provide additional information. To get a long listing of every file in a directory, including the hidden files, use:

$ ls -la
total 363604
drwx------  3 vdanen vdanen        91 Jun  2 16:02 .
drwx--x--x 18 vdanen vdanen      4096 Jun  2 15:07 ..
-rw-r--r--  1 vdanen vdanen        73 Feb  4 21:53 MD5SUMS
-rw-r--r--  1 vdanen vdanen 372318208 Feb  4 20:19 annvix-netinstall-i586-2.0-RELEASE.iso
lrwxrwxrwx  1 vdanen vdanen         6 Jun  2 16:02 bin -> ../bin
drwx------  2 vdanen vdanen         6 Mar  2  2004 mc-vdanen
...

To display the long listing in a human readable format that nicely summarizes the file sizes, use:

$ ls -lah
total 356M
drwx------  3 vdanen vdanen   91 Jun  2 16:02 .
drwx--x--x 18 vdanen vdanen 4.0K Jun  2 15:07 ..
-rw-r--r--  1 vdanen vdanen   73 Feb  4 21:53 MD5SUMS
-rw-r--r--  1 vdanen vdanen 356M Feb  4 20:19 annvix-netinstall-i586-2.0-RELEASE.iso
lrwxrwxrwx  1 vdanen vdanen    6 Jun  2 16:02 bin -> ../bin
drwx------  2 vdanen vdanen    6 Mar  2  2004 mc-vdanen

To include visual identifiers of different file types, use the -F option. This will show directories by suffixing their name with a backslash (/) character, symbolic links by suffixing the file name with @, and so on:

$ ls -F
MD5SUMS  annvix-netinstall-i586-2.0-RELEASE.iso  bin@  mc-vdanen/

There are other options for ls worth exploring. To do a long list without the permissions and size information, use the -1 option. To view the listings in color, use --color=auto. To list the files in reverse, use -r. To show a colored long list, in reverse, of just filenames, use ls -1 -r --color=auto. As you can see, you can use many options in conjunction with one another.

If you find a listing format that you like, create an alias for it so when you invoke ls on the command-line, you'll always use your preferred format, such as by adding the following to ~/.bashrc:

alias ls="/bin/ls -Fh --color=auto

Use ls --help or man ls to get more information on the many different options you can use with ls to customize its output.

>>Related tip: Customizing colors with the ls utility

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

13 comments
DanLM
DanLM

[i] The listing of a directory's contents is preceded by a labeled total num- ber of blocks used in the file system by the files which are listed as the directory's contents (which may or may not include . and .. and other files which start with a dot, depending on other options). [/i] Thats from the man pages. Dan

mostafa815
mostafa815

what does "total" refer to? in the output of "ls" command?

papawdalton
papawdalton

Where can I get a book (ebook,.pdf)on commands and how to use them, I read a lot about commands, but nobody explaining in novice terms how to use them and where to find resources.

Jason Norris
Jason Norris

I was working with a new developer yesterday, trying to give him enough knowledge of Linux to continue exploring on his own. The central problem, I think, is that he does not "love the command line". If you don't love working from the command line, you'll never be a great Linux user. You can always add --help to any Linux command and usually get a pretty extensive list of options to use. If I'm in a directory with a lot of files, I like to do 'ls -l | grep myfile' to find the file I'm looking for.

tpernas
tpernas

ls -ltr to sort by last modified, in reverse order. That way if there is streaming list, the modified is at the bottom. I also like ls -lut. It does a sort but by last access time. That one is nice to see if anything has accessed your files since your last access :)

CharlieSpencer
CharlieSpencer

My newbie experience with the man pages is they're written assuming a certain level of experience and skill. They're good references for a command you may already know something about, but most of them aren't good beginner tools. I often wonder when someone is going to market a "Page-a-Day Linux Commands Calendar".

IT cowgirl
IT cowgirl

Signup for a free Linux class on HP.com! he have Linux 101 and 201 which get you started with using linux commands and you learn how Linux basically works. By the time you get to Linux 301 you will start learning administration on a Linux. Have some fun!!

dawgit
dawgit

realy good ideas. (Now if I just had some time :_| ) I'm glad to see that you're still the kick'n around the Penguin. ls = day 1 (?) anyway, it's good to see you're still at it.-d

lastchip
lastchip

I agree with you. "Man" for novices might as well be a foreign language.