ls [man page] - list directory contents


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

This prompt gives you a 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 you personal settings ( environment).

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

well it seems that something went wrong because no output was produced on the screen. 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 and any given time, at the moment is is home directory. Default behaviour of the ls command is to show list of files and directories of current directory. Command pwd will show your current location:$ pwd

Default behaviour of ls command can be overridden by entering some options and arguments to ls command itself. So, for instance if we want to list all files and directories in / ( root directory ) we would enter / as a 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 ls command which is a directory location / . / ( root directory ) is a parent directory of all other directories within a filesystem. The output therefore shows all directories and files located under / directory. Please note that our /home directory is also located there.

Let us create some files 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 had been created:$ ls

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

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

Using a -l option combined with ls command will give us more detailed information about any given file or directory. Consider a 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 ls command, the ls command displays detailed information about the foo file. This information includes file permission, owner, group, size, creation date +time and lastly file name.

Another useful option to modify default behaviour of ls command is -h. This option will display size of the 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 two options -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 a ls command without any arguments will display list of files and directories in the current directory. There may be some case that we need to list files a 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 mkdir command: dir1 and dir2 where dir1 will be a parent directory of dir2. Then we will create a file called foobar inside a dir2 and use ls -R to list recursively current directory:$ mkdir -p dir1/dir2$ touch dir1/foobar$ ls -R
dir1 foo

dir2 foobar


Output analysis will reveal the following findings:

  • current directory contains directory dir1 and file foo
  • directory dir1 contains directory dir2
  • directory dir2 contains file foobar

Video Examples


ls [OPTION]... [FILE]... 

Frequently used options

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


  1. Can you list hidden and non-hidden files and directories in /etc/?
  2. Answer: ls -a /etc
  3. How would you retrieve permissions of a /etc/passwd file?
  4. Answer: ls -l /etc/passwd
  5. Can you list all hidden files inside your current working directory?
  6. Answer: ls -a
  7. What is the size os /bin/bash file in human readable format?
  8. Answer: ls -lh /bin/bash
  9. How would you produce a list of files of /etc/ directory from largest to the smallest and store it into /tmp/myfiles.txt file?
  10. Answer: ls -lS /etc > /tmp/myfiles.txt

Related commands

cd, cat, pwd

Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.