Skip to main content

Linux Commands Series

ls Command in Linux

This guide explains the ls command in Linux with syntax, examples, and options to help you navigate and manage files effectively.

The ls command (short for β€œlist”) is used to list or display the contents of a directory.

This could either be your current directory or any other directory on the system. The command displays both files and subdirectories, and in most cases distinguishes between different file types using color codes.

Without any command-line options, the ls command will simply list all the directory contents. However, it provides an array of handy command-line options to manipulate the output and display the desired output.

Let’s discuss the basics of ls command examples with all the available various command options that it provides in Linux.

ls Command Options in Linux

The ls command takes the following syntax:

ls [ options ] /path/to/directory

The options section represents the command-line arguments that can be passed to manipulate the output of the command.

1. List Files and Directories in Linux

Running the ls command without passing any command-line options or arguments, the ls command simply lists the directory contents in alphabetical order.

Here we won’t be able to view details like file types, size, modified date and time, permission, and links, etc.

ls
List Directory Contents in Linux

2. Long Listing of Files in Linux

The β€œ-l” command option lets you print out detailed information about the directory contents in a columnar format that includes size, modified date and time, file or directory name and owner of the file, and its permission.

ls -l
Long Listing of Files in Linux

Starting from the far left, we have:

  • 1st column – File/directory permissions.
  • 2nd column – Number of links.
  • 3rd column – Name of the owner.
  • 4th column – Name of the group that the file belongs to.
  • 5th column – File size in bytes.
  • 6th column to 8th column – Last modification date.
  • 9th column – File / Directory name.

3. List Hidden Files and Directories

Hidden files are special files that store user settings and configuration files, which are used by running programs and services for reading and storing information.

For example, the .bashrc file is a script that contains user settings and configurations of the currently logged-in user, which include command aliases, shell history, the coloring of the terminal font, etc.

The .bash_logout file is executed when you log out of your bash sessions. It’s mainly used for cleanup purposes i.e carrying out any operations that need to be performed once you exit the bash shell.

To list hidden files, pass the β€œ-a” option as shown, which displays both hidden files and directories.

ls -a
List Hidden Files on Linux

4. List All Files in Linux

As you have noticed, the "-a" option not only lists hidden files but also all the files and directories. For better viewing, you can use the β€œ-la” option.

 ls -la
List All Files in Linux

5. Display File Size in a Human-readable Format

To present the output in a better format, add the β€œ-h” flag to print the file size in a human-readable format.

From the output, the file size is displayed in Kilobytes, Megabytes, and Gigabytes. By all means, this looks more presentable.

ls -lh
Linux ls Command File Size Formatting

6. Distinguish Directories and Files in Linux

When running the ls command, it’s not always easy to make a clear distinction between files and directories. The β€œ-F” option adds a forward slash (/) to directories, making it easier for them to stand out from the rest of the files.

ls -F
Distinguish Directories and Files in Linux

7. Sorting Files in Reverse Order

By default, the ls command sorts files and directories alphabetically (From A – Z). You can opt to sort the directory contents in reverse order using the β€œ-r” option.

ls -lr 
Sort Files in Reverse Order in Linux

In addition, you can sort the file extensions alphabetically using the β€œ-X” flag.

ls -X