In Linux, the ls command is one of the fundamental tools. It lists files and directories with or without various additional information. The ls command is a part of the GNU core utility package. It should be available on any Linux distro.

This guide will showcase how to use the ls command, specifically “ls -l” in combination with other options.

Linux ls command

The ls command takes the location of a directory and prints all the files and directories within the location. It can also print additional file information like file permissions, file ownership, file size, etc.

Command structure

This is the command structure that all ls commands must follow.

$ ls <options> <directory>

If no directory is specified, then ls performs its action on the current directory. For example, the following command will list all the files and directories (name only).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image1-4.png" data-lazy- height="146" src="data:image/svg xml,” width=”1237″>

Listing files and directories in long list format

By default, the ls command will print the name of all the files and directories only. To get the additional information and a cleaner view, use the flag “-l”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image3-2.png" data-lazy- height="370" src="data:image/svg xml,” width=”1241″>

In long list format, ls shows the following info about each file.

Additional options

There are numerous options available to modify the output of the ls command. All the following options are combined with “ls -l” to use the long list format by default.

Showing backslash after directories

By default, the ls command uses coloring to distinguish between files and directories. However, it may not be reliable in various situations; for example, you’re accessing through a console that doesn’t support coloring.

In such a situation, use the flag “-F” to show a backslash “/” after the name of a directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image2-3.png" data-lazy- height="391" src="data:image/svg xml,” width=”1236″>

Sorting

The ls command will show the list in a specific order (generally by file name in alphabetic order). However, it also supports sorting based on other values.

To sort the output in reverse order, use the flag “-r”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image5-3.png" data-lazy- height="387" src="data:image/svg xml,” width=”1239″>

To sort the files and directories by time and date of creation/modification, use the flag “-t” instead.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image4-3.png" data-lazy- height="394" src="data:image/svg xml,” width=”1239″>

We can also sort the output alphabetically by entry extension. To do so, use the flag “-X”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image7-2.png" data-lazy- height="396" src="data:image/svg xml,” width=”1241″>

Showing hidden files

By default, the ls command won’t show hidden files. These are the files that start with “.” at the beginning of the file name.

To show the hidden files, use the flag “-a”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image6-3.png" data-lazy- height="467" src="data:image/svg xml,” width=”1241″>

Directory tree

A directory tree shows the hierarchy of the target directory and its sub-directories and files. Generally, we use the tree command to check the directory tree.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image9-1.png" data-lazy- height="569" src="data:image/svg xml,” width=”1240″>

However, the ls command can also showcase the directory tree (although not-so-good-looking). To print the tree view, use the flag “-R”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image8-2.png" data-lazy- height="518" src="data:image/svg xml,” width=”1236″>

Showing inode number

In Linux, each file has its unique inode value. Inode stores metadata for every single file on the partition. These data are stored at the start of each partition. It stores all the file info except the file name and the data itself.

To show the inode value of each file and directories, use the flag “-i”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image12.png" data-lazy- height="383" src="data:image/svg xml,” width=”1237″>

Showing UID and GID

In Linux, each file has its UID (unique identifier) and GID (group ID). To show the UID and GID of the files, use the flag “-n”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image10.png" data-lazy- height="393" src="data:image/svg xml,” width=”1240″>

Human-readable format

By default, the ls command prints file size in bytes. We can tell to print all the values in human-readable format for easier understanding of them.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image11.png" data-lazy- height="391" src="data:image/svg xml,” width=”1238″>

More options

These are only a handful of options that the ls command supports. For the full list of supported options as well as detailed explanations, check out the man page.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/image13.png" data-lazy- height="670" src="data:image/svg xml,” width=”1238″>

Final thoughts

In this guide, we learned about the ls command in Linux. It’s one of the fundamental tools available on all Linux distros. This guide showcases many common methods of using the ls command. Once mastered, it can be more convenient to use it than a GUI file browser.

Happy computing!

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/profile-photo-1-150×150.jpg615bbdb5cad2b.jpg" height="112" src="data:image/svg xml,” width=”112″>

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.