How to Use the ls Command in Linux
Introduction
The ls
command is an important utility used to list directories and to display information about the included files, and attributes. It's also known as list (ls
) and enables you to efficiently navigate through a file system.
This article explains how to use the ls
command in Linux to perform directory listing tasks.
ls
Command Syntax
Below is the basic ls
command syntax:
$ ls [options] [file/directory]
Within the above command, [options]
includes optional flags that modify the command's behavior, and [file/directory]
specifies the file or directory to list. If no file or directory is specified, ls
displays the working directory contents.
ls
Command Options
Option | Description |
---|---|
-l |
Long list. Displays detailed information about files and directories. |
-a |
All files. List all files including hidden files and directories. |
-t |
Sort files and directories using the last modification time. |
-r |
Reverse order. Reverses the default order of listing. |
-S |
Sorts files and directories by their sizes. |
-R |
Recursive listing. Lists files and directories recursively, including sub-directories. |
-i |
Inode. Displays the index number (inode) of each file and directory. |
-g |
Group. Displays the group ownership of files and directories instead of the owner. |
-h |
Human-readable format. Prints file sizes in a human-readable format (example, 1K, 234M, 2G). |
-d |
Directory. Lists directories themselves, rather than their contents. |
--color |
Color. Adds color coding to the output based on file types. |
Practical Examples of the ls
Command
List files in your working directory.
console$ ls
The above command lists all files in your working directory in alphabetical order.
Output:
Long list the working directory to view detailed information about files and directories.
console$ ls -l
The above command lists all directory contents with detailed information about each file and sub-directory including the permissions, owner, group, size, and modification time.
Output:
List all files in the working directory including hidden files.
console$ ls -a
The above command lists all files and directories in your working directory including hidden files and directories.
Output:
Sort files by last modification time.
console$ ls -lt
The above command sorts files and directories using the last modification time in descending order.
Output:
Display all files with the respective sizes in a human-readable format.
console$ ls -lh
The above command lists all directory file sizes in a human-readable format for easier interpretation.
Output:
List files and directories recursively.
console$ ls -R
The above command recursively lists all files and directories, including sub-directories.
Output:
Display inode numbers of all files and directories.
console$ ls -i
The above command displays the inode numbers for all files in your working directory.
Output:
Display the group ownership of files and directories.
console$ ls -g
The above command displays the group ownership of files and directories in your working directory.
Output:
List all directories and exclude contents.
console$ ls -d */
The above command lists directories without include the available contents and appends a slash
/
to each directory result.Output:
List files with combined options to view more details with a human-readable output.
console$ ls -lhS
The above command combines long listing
-l
, human-readable-h
, and sorting-S
options to display detailed information.Output:
List files with color-coding.
console$ ls --color
The above command adds color-coding to the output based on the file types to differentiate file types and directories.
Output:
List files with a specific pattern.
console$ ls *.txt
The above command lists all files with the
.txt
extension in your working directory.Output:
Display directory sizes.
console$ du -sh */
The above command combines
ls
withdu
(disk usage) to list directories and their respective sizes in a human-readable format.Output:
Advanced Examples and Combined Uses
List all files with the respective permissions and timestamps.
console$ ls -alt
The above command lists all directory files and sorts the output using the last modification time.
Output:
List files with inodes and human-readable sizes.
console$ ls -lih
The above command combines the
-l
,-i
, and-h
options to show detailed information, inode numbers, and human-readable sizes.Output:
List files and pipe the output to
grep
to filtering specific files.console$ ls -l | grep 'filename'
The above command lists files in a long format and pipes the output to
grep
results to display files that match thefilename
string.Output:
List directories that match a specific pattern.
console$ ls -d */ | grep 'pattern'
The above command lists directories matching a specific pattern.
Output:
List and count files and directories in the working directory.
console$ ls | wc -l
The above command lists the directory contents and pipes the output to
wc -l
to count the number of files in the directory.Output:
List and sort files by size in descending order.
console$ ls -lS
The above command lists files in long format and sorts them by size in descending order.
Output:
List only files (not directories).
console$ ls -p | grep -v /
The above command lists all files and directories, and pipes the output to
grep -v /
to exclude directories.Output:
Display files modified in the last 7 days.
console$ find . -type f -mtime -7 | xargs ls -l
The above command finds all files modified in the last 7 days and lists them in long format.
Output:
Conclusion
You have used the ls
command in Linux to list directory contents, navigate the file systems, and manage files and directories. For more command options, run the man ls
command to view the ls
manual page.