Linux ls command examples

Linux ls command FAQ: Can you share some examples of the Unix/Linux ls command?

The Linux ls command is used to list files and directories. While it has many options, I thought I'd list the most common ls command uses I'm aware of.

The ls command options I use most of the time are -a ("show all") and -l ("long listing"). Put together, like this:

$ ls -al

this means "show a long listing of all files in the current directory". In the home directory of my Mac OS X system this results in output like this:

drwxr-xr-x    60 al    al       2040 Aug  5 21:17 .
drwxrwxr-t     6 root  admin     204 Jul  2 18:13 ..
-rw-r--r--     1 al    al      15364 Aug  5 20:56 .DS_Store
drwx------    66 al    al       2244 Aug  5 21:22 .Trash
-rw-------     1 al    al          0 Aug  5 21:17 .Xauthority
-rw-------     1 al    al      10682 Aug  6 16:39 .bash_history
-rw-r--r--     1 al    al        696 Jan 14  2007 .bashrc
-rw-r--r--     1 al    al         59 Jul 22 11:57 .profile
drwx------     3 al    al        102 Jun 15  2006 .ssh
drwxr-xr-x     6 al    al        204 May 23  2006 .subversion
-rw-r--r--     1 al    al       2087 Jul 22 15:12 .vi_help
-rw-r--r--     1 al    al        218 Jul 22 14:53 .vimfoo
-rw-------     1 al    al       8417 Jul 25 21:32 .viminfo
-rw-r--r--     1 al    al        218 Jul 22 14:56 .vimrc
drwx------    46 al    al       1564 Jul 20 20:35 Desktop
drwx------    14 al    al        476 May 12 20:07 Documents
drwx------    34 al    al       1156 Mar 25 10:07 Library
drwx------     4 al    al        136 Dec 28  2006 Movies
drwx------     8 al    al        272 Dec 28  2006 Music
drwx------   109 al    al       3706 Jul 18 19:54 Pictures
drwxr-xr-x     5 al    al        170 Apr  1  2006 Public
drwxr-xr-x    16 al    al        544 Jul 20 15:52 Sites

I've trimmed a lot of files and directories from that listing, but you get the general idea of the output. Here are a few quick notes about that output:

  • The first column of 10 fields shows whether the current line represents a directory (if the first character is a "d") or a regular file (if the first character is a "-"), followed by nine characters that show the file/directory permissions.
  • The next column isn't important 99.999% of the time, and I haven't looked at it in the last 15 years. (I honestly can't even remember its exact meaning/purpose.)
  • The next column shows the user that owns this file (in this case the user "al").
  • The next column shows the group that owns this file (in this case the group named "al").
  • The next columns is the size of the file (or directory entry), in bytes.
  • The date field shows the last modification time of the file or directory.
  • The last column shows the name of the file or directory.

As a practical matter, directory listings can get long some time, and when I know a directory listing is going to be long I'll automatically pipe the output of this command into the Linux more command, like this:

$ ls -al | more

The more command lets me scroll through the ls command output, which is very cool.

Other Linux ls command options

As a practical matter the only other way I use the ls command is like this:

$ ls -F

or this:

$ ls -FG

or this:

$ ls -aFG

The -F option puts slashes after directories, "*" after executables, and other nice tags. The -G option adds in color coding, which is nice. And again the -a option includes all files, including those that begin with a decimal.

Output from the ls -F command looks like this:

AlsLife/                        GarageBandProjects/             Music/                          WakeUp.scpt
Apps/                           Library/                        Pictures/                       bin/
Desktop/                        MYSQL_README                    Reference/                      pg_foo/
Documents/                      MagicDrawFiles/                 Send Registration@              tmp/
EclipseStuff/                   Movies/                         Sites/                          tomcat-4.1.31/

This combination is nice because it shows a lot of information in a small area by filling the width of your screen with the data it shows. In this example, 20 files and directories are shown on five lines of screen real estate.

The ls command: Listing other directories

If you're in your home directory (like /Users/al) you don't have to move to the /tmp directory to get a listing of that directory, you can just type:

$ ls /tmp

or

$ ls -al /tmp

and so on. Only newbies do this:

cd /tmp
ls -al
cd -

so don't do that. :)

Many more Linux ls command options

There are many more options, but those are the ones I use the most. If I was forced to show two more options I would show these:

$ ls -alR

That shows a very long listing of every file beneath your current location in the filesystem. If you did that from the root directory it would show every file on the filesystem you have privilege to see.

$ ls -ld somedir

That command shows a listing of a directory named somedir. You use this when you want to see information about the directory itself, as opposed to what's in the directory. To be more clear, if you want to see a long listing of what's in a subdirectory named somedir you'd type this:

$ ls -l somedir

but if you want to see information about the somedir directory itself you'd type this:

$ ls -ld somedir

which displays something like this:

drwx------   45 al  al  1530 Aug  5 21:22 somedir

The -d option tells the ls command to just show the directory information, and not the directory contents.

Linux ls command aliases

With any of these options I encourage you to create aliases so you don't have to type much. My aliases look like this:

alias l="ls -al"
alias lm="ls -al | more"
alias lf="ls -aFG"

On my Mac OS X system I keep these in the .profile file in my home directory, so they're available each time I open a new Terminal window. I have more information on creating aliases here and here.

Even more ls command information

For more information on the ls command, type

$ man ls

on your Unix, Linux, or Mac OS X system, or click here for my online ls command man page.