Home Beginner's Guide 13 ways to use the ls command to list files on Linux

13 ways to use the ls command to list files on Linux

by Arun Kumar
Published: Updated:
ls command linux examples

The LS command is one of the first commands that any Linux newbie will learn after installing their first Linux operating system. Not only does it come in handy with Linux Desktop systems, but it’s also an excellent utility for server management.

LS command lists files and directories in Linux and Unix systems. Other than listing the files and directories, you can use this command to get more information about a file or directory. This data includes who owns the file, size of the data, permissions given to the file, and even the time of the creation of the file.

ls command uses in Linux with examples

The basic syntax of the LS command is as follows:

ls [option] [path_to_file/directory]

It is not a surprise that we use the LS command daily, but have we exhausted all the options that come with this great utility? In this article, we will look at 13 LS commands that may be of help in your everyday interaction with Linux systems.

1. List files and directory without additional options

ls

It is one of the basic LS commands that every Linux user executes daily. It only lists files in a directory without any further information like permissions and file owner. Take a look at the image below.

LS command Image

LS command Image

From the above image, we ran the LS command on the Desktop, and it listed all the files present.

2. List files and Directories using the LS -L

ls -l

The LS -L command lists files and directories together with additional information like permissions, owner, date of creation, etc. Let’s look at the image below before getting to every column and data presented.

LS -L Command Image

LS -L Command Image

From the above image, we see the information listed under seven columns. We will look at every single column and what it represents.

  • Column One: It shows the file type and the user permissions assigned to the file.
  • Column two: Indicates the number of blocks of memory taken by the file.
  • Column three: It represents the owner or user who created the file.
  • Column four: In Linux systems, users are associated with different groups. This column represents the group to which the owner belongs.
  • Column five: It represents the size of the directory or file. It is calculated in bytes.
  • Column six: This is the column that deals with time stamps on the file—the date of creation and modifications.
  • Column seven: It represents the original name of the file or directory as given by the user.

3. List files with LS -A

ls -a

In every operating system, we have hidden files and folders that are not displayed even with the LS -L command. Unless you have enabled the “show hidden files feature.” In Linux systems, hidden files are those that start with a dot(.) letter. The LS -A command lists all data, including hidden files (dot-files).

LS -A Command Image

LS -A Command Image

From the above image, we first ran the LS command, which listed only a few files. When we ran the LS -A command, on the same directory, it listed all files, including the hidden files.

4. List files with the LS -LH

ls -lh

The LS -LH command is commonly known to list files in a human-readable format. It is mainly useful when it comes to the size of the data. Unlike the LS -L command alone, which lists the size of files in bytes only, the LS -LH command will show the exact size in bytes(b), kilobytes(kb), megabytes(MB), etc. See the image below.

LS -LH Command Image

LS -LH Command Image

From the image above, the LS -L command file size in bytes, which is not so human-friendly. The LS -LH command output the files with the file sizes in a straightforward format.

5. List files with the LS -F

ls -F

Suppose you want to know directories in an output of an LS command, then we will use the LS -F, which adds a forward slash at the end of every directory name. See the image below.

LS -F Command Image

LS -F Command Image

Note, unlike other previous commands, the F option is in capital letters.

6. Use LS -R to list files in reverse order

ls -r

If you wish to list files in reverse order with the last file in the directory listed first, use the LS -R command. See the image below.

LS -R Command Image

LS -R Command Image

From the above image, we first executed the LS command, and the first file was 1wolf14.zip, while the last was prince-of-persia.zip. With LS -R command, the files were reversed. prince-of-persia.zip became the first while 1wolf14.zip was at the last index.

7. List files and directory contents recursively with LS -R

ls -R

Suppose you want to list files together with the contents of directories present in the path, then we will use the recursive (R) feature that comes with Linux systems. We will use the LS -R command.

LS -R (recursive) Command Image

LS -R (recursive) Command Image

From the image above, we first executed the LS command, which listed the files and directories present. In the second command, we ran LS -R command, which not only listed files and directories present, but also contents inside those directories.

8. Show the latest modified file with LS -LTR

ls -ltr

You can use LS -LTR command to show the latest modified files or directories in order. The latest modified is listed as the last while the oldest outputted as the first. Take a look at the image below.

LS -LTR Command image

LS -LTR Command image

From the image, we see the oldest modified file is the ‘DSC_0627.JPG’ edited on March 13th, while the newest is ‘youtubeDownloader’ modified on April 10th.

9. List files and directories by size using LS -LAS

ls -laS

You can also sort your files by size using the ls -las command. The most extensive file will be displayed first while the smallest last. See the image below.

LS -laS command Image

LS -laS command Image

10. List the inode number of files and directories using LS -I

ls -i

Inode numbers, also referred to index numbers, are unique identifiers assigned to every file and directory upon creation. To list files and directories together with their inode numbers, use the LS -I command. See the image below.

LS -I command image

LS -I command image

11. List UID and GID values with the LS -N

ls -n

To list files and directories together with their User Identifier(UID) and Group Identifier(GID) values, use the LS -N command. See the image below.

LS -N command linux

LS -N command Linux

In the above image, column three and four represent the UID and GID values, respectively.

12. List information of a directory using the LS -LD

ls -ld

When we run the LS -L command on a directory, it will output all the contents and information about the file. If you wish to list only the information about the directory/folder, then we will use the LS -LD command. See the image below.

LS -LD Command image

LS -LD Command image

From the above image, we first executed the LS command on the lib folder. It outputted the contents of the lib directory. The second command, we ran the LS -LD command that returned information about the lib directory.

13. List all usages of LS command using LS -HELP, and MAN

ls --help
man help

Assuming you are a Linux newbie who wants to learn about the LS command or a user who wants to exhaust all the options that come with the LS command, there are two ways you can achieve that. Use the LS –HELP command.

LS --Help Command

LS –Help Command

Else, use the MAN LS command.

MAN LS command image

MAN LS command image

Conclusion

Those are some of the LS commands you can use in your everyday activity with Linux systems. The command-line way of listing files is quite useful for managing servers without a graphical user interface or in combination with other commands like grep to filter results. Do you have a command that you want to share with our readers? Feel free to post in the comment section below. If you find this article useful, feel free to share the link with friends.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.