Linux ls Command - How to List Files and Directories

October 10, 2024

Introduction

The ls (list) command in Linux lists directory contents, displaying files and subdirectories. It provides detailed information such as file names, permissions, sizes, and modification dates.

Knowing how to use ls is crucial for navigating and managing files efficiently in a Linux environment.

The following text elaborates on how to use the Linux ls command.

21 Linux ls Command Examples

Prerequisites

  • Linux system.
  • Access to the terminal.

Linux ls Command Syntax

The ls command syntax is:

ls [options]

White ls prints output on its own, options let you add additional instructions to the command and modify its output.

Linux ls Command Options

The ls command has several options that modify how it works. The following table lists the most important ls options:

OptionDescription
-lLong format. Displays detailed file information, including permissions, ownership, size, and modification time.
-aShows all files, including hidden ones.
-hDisplays file sizes in human-readable formats (e.g., KB, MB) when combined with -l.
-RLists contents of directories recursively, including subdirectories.
-tSorts files by modification time, with the newest first.
-rReverses the order of the output.
-1Lists one file per line instead of displaying them in columns.
-d
Lists directories themselves, not their contents.
-FAppends a character to filenames indicating their type (e.g., / for directories, * for executables).
-LFollows symbolic links and lists the actual file or directory they point to.
-XSorts files alphabetically by their extension.

Linux ls Command Examples

The ls command is essential for navigating the file system and viewing detailed file information in Linux. The following text presents common ls use case examples.

List Files and Directories

Running the ls command without options prints out files and directories in their bare format. With this command, you cannot see file types, dates, and permissions:

ls
ls terminal output

The output shows the current directory's directories (blue) and files (white).

Distinguish File Types

Type ls -F to add a / after directory names, helping you to easily identify them.

ls -F
ls -F terminal output

This option also marks other file types: * for executable files, @ for symbolic links, and = for sockets.

Separate by Comma

To print directories and files separated by a comma, run the following:

ls -m
ls -m terminal output

Add Quotation Marks

Run ls with the -Q option to add quotation marks to all directories and files:

ls -Q
ls -Q terminal output

Get the Inode Number

To get the Inode (index node) number of all directories and files, type:

ls -i
ls -i terminal output

Sort Files and Directories in Reverse Order

To sort directories and files in the reverse order, type the following:

ls -r
ls -r terminal output

Sort Files and Directories Modification Time

Use the following to sort directories and files by modification time, with the most recently modified items displayed first:

ls -t
ls -t terminal output

Sort Directories and Files Alphabetically by Their Extension

To sort directories and files alphabetically by the entry extension type, run:

ls -X
ls -X terminal output

Files with the same extension are grouped, making it easier to view similar file types together,

Note: If you want to learn how to sort file contents, refer to the Linux sort command.

View Hidden Files

When using the basic ls command, you can't see hidden files and files starting with ".". To display them, run:

ls -a
ls -a terminal output

Note: To learn how to hide and see hidden files in Linux, refer to our Show Hidden Files in Linux article.

Access Directory Trees

To access long listing directory trees, type:

ls -R
ls -R command terminal output

This option recursively lists all files and subdirectories, meaning it shows the contents of each directory and its nested directories in sequence.

To display additional information on the directory tree, such as the file owner, size, and date and time of the last modification, type:

ls -lR

View Long Listing Format

You can also print out a long listing format of files and directories. The output displays a file or directory name, file owner, its permission, size, and modified date and time.

To get this output, run:

ls -l
ls -l terminal output

List Files and Directories with Details

Print a comprehensive, long-format listing of all files and directories in the current directory, including hidden ones, along with detailed file information. To accomplish this, run:

ls -la
ls -la terminal output

Print Specific File Types

To print specific file types using the ls command, use wildcards (*) to filter the output. For example, to list all .txt files, run:

ls *.txt
ls *.txt terminal output

List UID and GID of Files

To display the unique identifier (UID) and group identifier (GID) assigned to all files and directories, run:

ls -n
ls -n terminal output

Display Files in Human-Readable Format

You can also check the size of files and directories in a human-readable format. A human-readable format presents file sizes in units that are easier to understand, such as kilobytes (KB), megabytes (MB), or gigabytes (GB), instead of displaying sizes in bytes.

To accomplish this, type:

ls -lh
ls -lh terminal output

View Reverse Output Order by Date

To print a long listing format of files and directories with the latest modification date, run:

ls -ltr
ls-ltr terminal output

The output displays detailed information about each file or directory in reverse order based on their last modification time.

List Files by Size

To receive a long listing format of files and directories sorted by file size, from largest to smallest, run:

ls -lS
ls -lS terminal output

Display Files Under Specific Directory

If you want to access files in the specific directory, add the directory path to the command. For example, to display files under the /tmp directory, type:

ls -l /tmp
ls -l /tmp terminal output

Hide File Owner

You can also print a detailed list of files and directories, but without showing the owner of each file. To accomplish that, run:

ls -g
ls -g terminal output

View All ls Commands

To print additional ls info and list all command options, type:

ls --help
ls --help terminal output

Print ls Version

To print the ls command version, use the following command:

ls --version
ls --version terminal output

This command displays the version number and some additional information about the ls command.

Conclusion

This article explained how to use the ls command and options in Linux to list files and directories. The command allows users to manage files by providing various options to filter, sort, and display data.

Next, learn how to create files in Linux.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to List Users in Linux, List all Users Command
August 4, 2022

Linux OS is unique because of its multiuser characteristic. It allows multiple...
Read more
How to Create a File in Linux Using Terminal/Command Line
July 11, 2024

Creating a file in Linux might seem straightforward, but there are some...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command...
Read more
How to Use Linux Cat Command
June 3, 2024

The cat command, short for concatenate, is used to display the contents...
Read more