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.
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:
Option | Description |
---|---|
-l | Long format. Displays detailed file information, including permissions, ownership, size, and modification time. |
-a | Shows all files, including hidden ones. |
-h | Displays file sizes in human-readable formats (e.g., KB, MB) when combined with -l . |
-R | Lists contents of directories recursively, including subdirectories. |
-t | Sorts files by modification time, with the newest first. |
-r | Reverses the order of the output. |
-1 | Lists one file per line instead of displaying them in columns. |
-d | Lists directories themselves, not their contents. |
-F | Appends a character to filenames indicating their type (e.g., / for directories, * for executables). |
-L | Follows symbolic links and lists the actual file or directory they point to. |
-X | Sorts 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
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
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
Add Quotation Marks
Run ls
with the -Q
option to add quotation marks to all directories and files:
ls -Q
Get the Inode Number
To get the Inode (index node) number of all directories and files, type:
ls -i
Sort Files and Directories in Reverse Order
To sort directories and files in the reverse order, type the following:
ls -r
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
Sort Directories and Files Alphabetically by Their Extension
To sort directories and files alphabetically by the entry extension type, run:
ls -X
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
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
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
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
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
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
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
View Reverse Output Order by Date
To print a long listing format of files and directories with the latest modification date, run:
ls -ltr
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
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
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
View All ls Commands
To print additional ls
info and list all command options, type:
ls --help
Print ls Version
To print the ls
command version, use the following command:
ls --version
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.