How to Use the ls Command in Linux

Updated on November 21, 2023
 How to Use the ls Command in Linux header image

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:

console
$ 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

  1. List files in your working directory.

    console
    $ ls
    

    The above command lists all files in your working directory in alphabetical order.

    Output:

    ls command

  2. 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:

    ls -l command

  3. 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:

    ls -a command

  4. 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:

    ls -lt command

  5. 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:

    ls -lh command

  6. List files and directories recursively.

    console
    $ ls -R
    

    The above command recursively lists all files and directories, including sub-directories.

    Output:

    ls -R command

  7. 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:

    ls -i command

  8. 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:

    ls -g command

  9. 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:

    ls -d */ command

  10. 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:

    ls -lhS command

  11. 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:

    ls --color command

  12. List files with a specific pattern.

    console
    $ ls *.txt
    

    The above command lists all files with the .txt extension in your working directory.

    Output:

    ls *.txt command

  13. Display directory sizes.

    console
    $ du -sh */
    

    The above command combines ls with du (disk usage) to list directories and their respective sizes in a human-readable format.

    Output:

    du -sh */ command

Advanced Examples and Combined Uses

  1. 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:

    ls -alt command

  2. 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:

    ls -lih command

  3. 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 the filename string.

    Output:

    ls -l | grep 'filename' command

  4. List directories that match a specific pattern.

    console
    $ ls -d */ | grep 'pattern'
    

    The above command lists directories matching a specific pattern.

    Output:

    ls -d */ | grep 'pattern' command

  5. 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:

    ls | wc -l command

  6. 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:

    ls -lS command

  7. 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:

    ls -p | grep -v / command

  8. 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:

    find . -type f -mtime -7 | xargs ls -l command

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.