Example Uses Of The Linux ls Command

List Files And Folders Using Linux
List Files And Folders Using Linux.


The ls command is one of the most important command line tools you should learn in order to navigate the file system.

Click here for a complete list of essential command for navigating your file system using the command line.

The ls command is used to list the names of the files and folders within the file system. This guide will show you all of the switches that are available for the ls command along with their meaning and how to use them.

List The Files In A Folder

To list all the files in a folder open a terminal window and navigate to the folder you wish to see the contents for using the cd command and then simply type the following command:


You don't actually have to navigate to the folder to list the files within it. You can simply specify the path as part of the ls command as shown below.

ls /path/to/file

By default the files and folders will be listed in columns across the screen and all you will see is the filename.

Hidden files (files which begin with a full stop) are not shown automatically by running the ls command. You need to use the following command instead.

ls -a
ls --all

This minus a (-a) switch used above stands for list all. This lists absolutely every file and folder within the directory which the command is run or indeed against the path supplied to it.

The upshot of this is that you see a file called . and another called ..

. The single full stop stands for current folder and the double full stop stands for one level up.

If you want to omit these from the list of files you can use a capital A instead of the lowercase a as follows:

ls -A
ls --almost-all

Certain commands such as the mv command and cp command are used for moving and copying files around and there are switches that can be used with these commands that creates a backup of the original file.

These backup files generally end with a tilde (~).

To omit backup files (files ending with a tilde) run the following command:

ls -B
ls --ignore-backups

In most cases the returned list will show the folders in one colour and the files as another. For example in my terminal folders are blue and files are white.

If you don't want to show different colours you can use the following command:

ls --color=never

If you want a more detailed output you can use the following switch:

ls -l

This provides a list showing the permissions, number of inodes, the owner and the group, the file size, the last accessed date and time and file name.

If you don't want to see the owner use the following command instead.

ls -g

You can also omit the group details by specifying the following switch:

ls -o

The long format listing can be used with other switches to show even more information. For example you can find the author of the file by running the following command.

ls -l --author

You can change the output for the long listing to show human readable file sizes as follows:

ls -l -h
ls -l --human-readable
ls -l -s

Instead of showing the user and group names in a list command you can get the ls command to show the physical user id and group ids as follows:

ls -l -n

The ls command can be used to show all of the files and folders from the specified path downwards.

For example:

ls -R /home

The above command will show all of the files and folders below the home directory such as Pictures, Music, Videos, Downloads and Documents.

Change The Output Format

By default the output for the file listing is across the screen in columns.

You can however specify a format as shown below:

ls -X
ls --format=across

Show the list in columns across the screen.

ls -m
ls --format=commas

Show the list in a comma separated format.

ls -x
ls --format=horizontal

Show the list in a horizontal format

ls -l
ls --format=long

As mentioned in the previous section this shows the list in a long format.

ls -1
ls --format=single-column
ls --format=verbose

Shows all the files and folders, 1 on each row.

ls -c
ls --format=vertical

Shows the list vertically.

How To Sort The Output From The ls Command

To sort the output from the ls command you can use the --sort switch as follows:

ls --sort=none
ls --sort=size
ls --sort=time
ls --sort=version

The default is set to none which means the files are sorted by name. When you sort by size the file with the largest size is shown first and the smallest is shown last.

Sorting by time shows the file which has been accessed last first and the least accessed file last.

Incidentally all of the above sorts can be achieved with the following commands instead:

ls -U
ls -S
ls -t
ls -v

If you want the results in the reverse sort order use the following command.

ls -r --sort=size
ls --reverse --sort=size


There are a number of other switches available to do with time formatting. You can read about all the other switches by reading the ls Linux Manual Page.

man ls

More From Us