The ls command on Linux is one of the most essential command line tools that all users should familiarize themselves with. When navigating directories on the command line, the ls command is used to list the contents of a directory. Without this command, we can't know what files are on our system. Once you learn how to use this command, the knowledge will carry over to any Linux distribution, since ls is a longtime staple on all of them.

ls becomes even more handy once you learn some of its options. Newcomers to Linux may intuitively think that browsing files in the GUI would be infinitely easier than fiddling with the command line. But this couldn't be further from the truth. Mastering the ls command will allow you to list directory contents and find files a lot more efficiently than any GUI tools. It can also be utilized in Bash scripting to help other tools manipulate files.

In this guide, we'll introduce you to the ls command through various examples and scenarios on a Linux system. By the end, you'll have a good understanding of how it works, and be able to use it for all of your file listing needs. At the end of the guide, we have an exercises section to help make sure you've learned some of the most essential aspects of the command.

In this tutorial you will learn:
  • How to use the ls command with examples
  • Frequently used options with the ls command
  • ls command exercises
ls command examples on Linux
ls command examples on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ls
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

ls introduction and command examples


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


The very first time you login into a Linux system, a shell prompt will be shown on your terminal. The prompt you will see will be similar to the one below:

linuxconfig.org:~$

This prompt gives you the following information:

  • $ means you have logged in as a non-privileged user
  • ~ means that you are located in your home directory

The information displayed by the shell prompt can vary. This depends on your Linux distribution and your personal environment settings.

Now, it is time to start exploring what is inside your home directory and see where exactly your home directory is located. For this exploration we need some tools. One such tool will be the ls command which has an ability to display a list of files and directories.

linuxconfig.org:~$ ls
linuxconfig.org:~$

Well, it seems that something went wrong because no output was produced on the screen. However, this is perfectly normal output if there are no files or directories in your current directory. Current directory means a directory where you are located at any given time, and at the moment this would be the home directory. Default behaviour of the ls command is to show a list of files and directories of the current directory. The pwd command will show your current location:

$ pwd
/home/linuxconfig

The default behaviour of the ls command can be overridden by entering some options and arguments to the ls command itself. So, for instance if we want to list all files and directories in / ( root directory ) we would enter / as an argument to ls.

$ ls /
bin   cdrom  etc   initrd      lib         media  opt   root  srv  tmp  var
boot  dev    home  initrd.img  lost+found  mnt    proc  sbin  sys  usr  vmlinuz


In the command above, we have supplied an argument to the ls command, which is a directory location /. The root directory is a parent directory of all other directories within a filesystem. The output therefore shows all directories and files located under the / directory. Please note that our /home directory is also located there.

Let us create some files so that we have more data to play with. The next command will create 2 files: foo and .bar. The difference between those two files is that file .bar is a hidden file and the file foo is not. Any directory and file which has a dot in front of its name is hidden. To create these two files we use a touch command.

$ touch foo .bar

Now we can use our ls command again to confirm that files have been created:

$ ls
foo

Just because the .bar file is hidden it will be not shown when the ls command is executed. The ls command does not show hidden files by default and therefore we need to override its behaviour with the -a option.

$ ls -a
.  ..  .bar  .bash_profile  .bashrc  foo

Option -a causes the ls command to list all files, including hidden files. Note that we have a couple other hidden files in our home directory. What is important to notice at this point is that the previously created file .bar is listed in the ls output.

Using the -l option combined with ls command will give us more detailed information about any given file or directory. Consider the following example:

$ ls -l foo
-rw-r--r-- 1 linuxconfig linuxconfig 0 2008-01-13 00:52 foo


When -l options ( long listing format ) and foo argument is supplied to the ls command, the command displays detailed information about the foo file. This information includes file permissions, owner, group, size, creation date and time, and lastly file name.

Another useful option to modify default behaviour of the ls command is -h. This option will display the size of the files in a human readable format. By default ls -l command will display file size in bytes:

$ ls -l /etc/services
-rw-r--r-- 1 root root 18274 2007-02-02 13:09 /etc/services

We can combine those two options into -lh to get the previous output in KB:

$ ls -lh /etc/services
-rw-r--r-- 1 root root 18K 2007-02-02 13:09 /etc/services

As it was already mentioned above, the ls command without any arguments will display a list of files and directories in the current directory. There may be some case where we need to list files and directories in all subdirectories. The following ls command with an option -R will recursively list all files. First we need to create two directories with the mkdir command: dir1 and dir2, where dir1 will be a parent directory of dir2. Then we will create a file called foobar inside of dir2 and use ls -R to list recursively the current directory:

$ mkdir -p dir1/dir2
$ touch dir1/foobar

$ ls -R
.:
dir1  foo

./dir1:
dir2  foobar

./dir1/dir2:

Output analysis will reveal the following findings:

  • The current directory contains directory dir1 and file foo.
  • Directory dir1 contains directory dir2.
  • Directory dir2 contains file foobar

Frequently used options

Here's a quick rundown of some of the most frequently used options with ls. These are the ones that would be most beneficial to remember, since you'll likely find yourself using them often.

  • -l - use a long listing format
  • -a - do not ignore entries starting with dot
  • -t - sort by modification time
  • -r - reverse order while sorting
  • -h - with -l, will print sizes in human readable format (e.g., 1K 234M 2G)

ls exercises

Use the following exercises to test your knowledge of the ls command. You can reveal an answer by highlighting it.

1. How can you list hidden and non-hidden files and directories in /etc/?
Answer: ls -a /etc

2. How would you retrieve permissions of the /etc/passwd file?
Answer: ls -l /etc/passwd

3. How can you list all hidden files inside your current working directory?
Answer: ls -a

4. What is the size of the /bin/bash file in human readable format?
Answer: ls -lh /bin/bash

5. How would you produce a list of files in /etc/ directory from largest to the smallest and store it into /tmp/myfiles.txt file?
Answer: ls -lS /etc > /tmp/myfiles.txt


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Closing Thoughts

In this guide, you were introduced to the ls command on Linux. You also saw some of the most frequently used options with the ls command, which should accommodate most of your file listing needs in terminal. You can also use the exercises section of our guide to test your knowledge. If you are able to answer the questions correctly, you have mastered all of the most essential aspects of the ls command.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.