06. Listing files ls, -a, -l

Listing directory files with ls

When you start up a terminal session, you are placed within your home directory (denoted by a ~). To list contents of your current working directory, simply use the ls command (short for list).

$ ls
file1.txt file2.txt file3.txt file4.txt

Here, we can see that four text files reside in our current working directory.

Additionally, you can list files within other directories by specifying the pathname after the ls command.

$ ls ~/Dropbox
Books Library Movies School Work

Listing hidden files with the -a option

Hidden files are usually those that have to do with the configuration of certain programs. They are kept hidden from the user, and have a . preceding its name.

For example, the .bash_profile file, located in your home directory, is run every time you start a new terminal to configure your environment. Furthermore the .vimrc file configures your Vim settings. However, you can't see these files with a normal ls command. To list the hidden files use the option -a.

$ ls -a
. Applications Library .. Desktop Movies .bash_history Documents Music .bash_profile Downloads Public .vimrc Dropbox

Can you see your secret files now?

Listing file details with the -l option

To see more details of the files, we can pass the -l option.

You should see a list of files with the following format (don't worry if your date format isn't exactly the same):

$ ls -l
total 12 drwx------ 6 JohnDoe staff 204 Jan 7 10:30 Applications drwx------+ 37 JohnDoe staff 1258 Mar 20 17:58 Desktop drwx------+ 6 JohnDoe staff 204 Mar 13 08:57 Documents drwx------+ 29 JohnDoe staff 986 Mar 22 00:06 Downloads drwx---rwx@ 25 JohnDoe staff 850 Mar 20 15:38 Dropbox drwx------@ 51 JohnDoe staff 1734 Mar 17 17:42 Library drwx------+ 3 JohnDoe staff 102 Dec 29 00:00 Movies drwx------+ 3 JohnDoe staff 102 Dec 29 00:00 Music drwxr-xr-x 4 JohnDoe staff 136 Mar 13 11:15 NetBeansProjects drwx------+ 4 JohnDoe staff 136 Dec 29 20:39 Pictures drwxr-xr-x+ 5 JohnDoe staff 170 Dec 29 00:00 Public drwx------ 4 JohnDoe staff 136 Mar 20 11:31 VirtualBox VMs

Let's break this down per attribute.

Access file rights. We will talk about this when we go over permissions.
The file's number of hard links. We will talk about links in our links lesson.
The file's owner.
The file's group owner.
Size of file in bytes (kb).
Dec 21 16:02
Date and time of file's last modification.
Name of file.

Other options to use with ls

There are other options you can use, but -l and -a are the most common.

List all entries except for implied . and ..
Indicator samp to see what type of file it is.
/ for directory, @ for symbolic link, = for socket, | for pipe.
Colorized output. Can also use --color on some distros.
Recursively list subdirectories.
Lists and sorts results by file size.
Sorts by modification time.
Sorts in reverse-alphebetical order.

Combining Options

To combine options, simply places the two together:

$ ls -la

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources