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.

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

Other options to use with ls

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

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

Combining Options

To combine options, simply places the two together:

$ ls -la

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad