By far the most common command used in Linux — or Unix, for that matter — is the ls command, which lists the contents of a directory (files and sub-directories)1. In its simplest form, you can simply enter the command by itself, to see a list of the files and sub-directories in the current directory, but there are many, many options you can use, to filter results, or get extra information about the files.
The Basics
Let's start with a simple example. From the root directory, if I simply type ls, I'll get something like the following:
$ cd /
$ ls
bin etc initrd.img.old mnt root tmp vmlinuz.old
boot home lib opt sbin usr
cdrom initrd lost+found proc srv var
dev initrd.img media Recycled sys vmlinuz
$
You can see that the ls command has listed out all of the files and directories under the root folder. It hasn't given me any details about the files, just their names. It has also formatted them into columns, so that more file/directory names can fit onto one screen. (Also, you can't see it on this page, but the results will also be colour-coded; directories would be in one colour, compressed files in another, image files in another, etc.)
Listing Files From Other Paths
I'm not limited to just getting a listing of the current directory. I could also enter another directory as a parameter, such as this:
$ cd /
$ ls /etc/X11/
app-defaults gdm xinit xorg.conf.old Xsession.d
cursors imwheel xkb Xresources Xsession.options
default-display-manager rgb.txt xorg.conf xserver XvMCConfig
fonts X xorg.conf~ Xsession Xwrapper.config
$
In this case, even though my current working directory was the root directory, I passed the ls command a different path as a parameter. So it didn't list me the contents of /, it listed the contents of the /etc/X11 directory.
Formatting Results
There are a lot of options that you can use with ls, to format your results. The following options are some of the more common ones, but this is by no means an exhaustive list.
Remember that these parameters are case-sensitive, so -c is different than -C.
Parameter | Result |
---|---|
-l | Display results in “long” format. That is, display information about each file. See below for an example. |
-s | Displays the size of each file, in “blocks.” Using “blocks” as a measurement probably isn't that handy, which is why you'll probably want to combine -s with -h. |
-h | When used in conjunction with -l or -s, prints file sizes in “human readable” format. e.g. 4.0K instead of 4096 (where 4096 would be 4096 bytes). See below for an example. |
-a | Display hidden files/directories |
-R | Recursively list sub-directories; be careful, when using this one because there could be a lot of subdirectories. |
-C | Display the results in columns. If you just type ls by itself, with no parameters, this is the default, but if you're using some of the other options, you might want to specify this one to get results in columns. |
-m | Display the results in a comma-separated manner; a simple comma and space is placed between each entry. You can fit more results on a screen with -m than with -C, but you may find the results more readable in columns with -C. |
-1 | Display the results one file/directory per line. |
-Q | Enclose each file/directory in quotation marks. This isn't usually particularly useful, but it can be helpful when there are a lot of file/directory names that have spaces in them. |
For example, let's take another look at the files in the root directory, in the “long” format:
$ cd /
$ ls -l
total 88
drwxr-xr-x 2 root root 4096 2007-09-02 21:45 bin
drwxr-xr-x 3 root root 4096 2007-09-02 21:46 boot
lrwxrwxrwx 1 root root 11 2007-06-29 18:26 cdrom -> media/cdrom
drwxr-xr-x 13 root root 13960 2007-09-17 19:17 dev
drwxr-xr-x 120 root root 12288 2007-09-17 19:24 etc
drwxr-xr-x 3 root root 4096 2007-06-29 18:47 home
drwxr-xr-x 2 root root 4096 2007-06-29 18:28 initrd
lrwxrwxrwx 1 root root 33 2007-06-29 22:57 initrd.img -> boot/initrd.img-2.6.20-16-generic
lrwxrwxrwx 1 root root 33 2007-06-29 18:30 initrd.img.old -> boot/initrd.img-2.6.20-15-generic
drwxr-xr-x 17 root root 4096 2007-09-02 21:45 lib
drwx------ 2 root root 16384 2007-06-29 18:25 lost+found
drwxr-xr-x 5 root root 4096 2007-09-17 19:17 media
drwxr-xr-x 2 root root 4096 2007-04-12 05:11 mnt
drwxr-xr-x 2 root root 4096 2007-06-29 18:28 opt
dr-xr-xr-x 124 root root 0 2007-09-17 15:16 proc
drwxr-xr-x 2 root root 4096 2007-07-20 13:21 Recycled
drwxr-xr-x 10 root root 4096 2007-09-15 22:01 root
drwxr-xr-x 2 root root 4096 2007-08-21 14:17 sbin
drwxr-xr-x 2 root root 4096 2007-06-29 18:28 srv
drwxr-xr-x 11 root root 0 2007-09-17 15:16 sys
drwxrwxrwt 12 root root 4096 2007-09-17 20:03 tmp
drwxr-xr-x 11 root root 4096 2007-06-29 18:31 usr
drwxr-xr-x 15 root root 4096 2007-06-29 18:43 var
lrwxrwxrwx 1 root root 30 2007-06-29 22:57 vmlinuz -> boot/vmlinuz-2.6.20-16-generic
lrwxrwxrwx 1 root root 30 2007-06-29 18:30 vmlinuz.old -> boot/vmlinuz-2.6.20-15-generic
$
As you can see, ls provides a lot of information about each file, including permissions, owner, creation date, size, etc.
Here's another example of the long format, but this time, using a human-readable version of the file sizes:
$ ls -l -h
total 88K
drwxr-xr-x 2 root root 4.0K 2007-09-02 21:45 bin
drwxr-xr-x 3 root root 4.0K 2007-09-02 21:46 boot
lrwxrwxrwx 1 root root 11 2007-06-29 18:26 cdrom -> media/cdrom
drwxr-xr-x 13 root root 14K 2007-09-17 19:17 dev
drwxr-xr-x 120 root root 12K 2007-09-17 19:24 etc
drwxr-xr-x 3 root root 4.0K 2007-06-29 18:47 home
drwxr-xr-x 2 root root 4.0K 2007-06-29 18:28 initrd
lrwxrwxrwx 1 root root 33 2007-06-29 22:57 initrd.img -> boot/initrd.img-2.6.20-16-generic
lrwxrwxrwx 1 root root 33 2007-06-29 18:30 initrd.img.old -> boot/initrd.img-2.6.20-15-generic
drwxr-xr-x 17 root root 4.0K 2007-09-02 21:45 lib
drwx------ 2 root root 16K 2007-06-29 18:25 lost+found
drwxr-xr-x 5 root root 4.0K 2007-09-17 19:17 media
drwxr-xr-x 2 root root 4.0K 2007-04-12 05:11 mnt
drwxr-xr-x 2 root root 4.0K 2007-06-29 18:28 opt
dr-xr-xr-x 124 root root 0 2007-09-17 15:16 proc
drwxr-xr-x 2 root root 4.0K 2007-07-20 13:21 Recycled
drwxr-xr-x 10 root root 4.0K 2007-09-15 22:01 root
drwxr-xr-x 2 root root 4.0K 2007-08-21 14:17 sbin
drwxr-xr-x 2 root root 4.0K 2007-06-29 18:28 srv
drwxr-xr-x 11 root root 0 2007-09-17 15:16 sys
drwxrwxrwt 12 root root 4.0K 2007-09-17 20:03 tmp
drwxr-xr-x 11 root root 4.0K 2007-06-29 18:31 usr
drwxr-xr-x 15 root root 4.0K 2007-06-29 18:43 var
lrwxrwxrwx 1 root root 30 2007-06-29 22:57 vmlinuz -> boot/vmlinuz-2.6.20-16-generic
lrwxrwxrwx 1 root root 30 2007-06-29 18:30 vmlinuz.old -> boot/vmlinuz-2.6.20-15-generic
$
In many cases, you may not need all of this information. If you just needed the file sizes, you could use the -s parameter — but you'll still probably want to use -h with it:
$ ls -s -h
total 88K
4.0K bin 0 initrd.img 0 proc 4.0K usr
4.0K boot 0 initrd.img.old 4.0K Recycled 4.0K var
0 cdrom 4.0K lib 4.0K root 0 vmlinuz
0 dev 16K lost+found 4.0K sbin 0 vmlinuz.old
12K etc 4.0K media 4.0K srv
4.0K home 4.0K mnt 0 sys
4.0K initrd 4.0K opt 4.0K tmp
$