Using the ls Command to List Files in Linux

List the files in a directory with this versatile command

Data files, illustration
VICTOR HABBICK VISIONS / Getty Images

The ls command lists the names of the files and folders within the file system. It's one of the most basic, essential commands of the Linux shell.

List the Files in a Folder

To list all the files in a folder, open a terminal window, select the relevant folder using the cd command, then type:

ls

By default, the files and folders are listed in columns across the screen and only display the filename.

directory listing in linux

Hidden files (files with names beginning with a period) are not shown automatically by running the ls command unless you specify the -a or -all switches:

ls -a
ls --all

The -a switch lists every file and folder in the directory.

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

ls -B
ls --ignore-backups

In most cases, the returned list shows the folders in one color and the files as another. If you don't want to show different colors, use the following command:

ls --color=never

To display more detailed output, 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 works with other switches to show even more information. For example, show 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 shows all of the files and folders below the home directory such as Pictures, Music, Videos, Downloads, and Documents.

Combine switches, too. The command ls -aBlh presents a listing as displayed in the screenshot at the top of this section.

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.

To show the list in columns across the screen:

ls -X
ls --format=across

To show the list in a comma-separated format:

ls -m
ls --format=commas

To show the list in a horizontal format:

ls -x
ls --format=horizontal 

To show the list in a long format:

ls -l
ls --format=long

To show all the files and folders, one on each row:

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

To show the list vertically:

ls -c
ls --format=vertical

How to Sort the Output From the ls Command

To sort the output from the ls command 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 at the top of the list and the least accessed file is at the bottom of the list.

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

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

To show the results in the reverse sort order, use the following command:

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

Shake the Tree

An alternative approach to viewing files involves the tree utility, which ships by default with some distributions but not others. When you execute it, you'll see printed to standard output a hierarchy of files and folders.

tree utility