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

directory listing in linux

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

ls

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 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

This -a switch lists every file and folder within 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, you can 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 will show 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.

ls -X
ls --format=across

Shows the list in columns across the screen.

ls -m
ls --format=commas

Shows the list in a comma separated format.

ls -x
ls --format=horizontal 

Shows 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 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 

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

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

Shake the Tree

tree utility

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.