---
title: " How to Use the ls Command in Linux"
url: https://docs.vultr.com/how-to-use-the-ls-command-in-linux
description: Learn how to use the ls command in Linux to list files and directories. Discover essential options, flags, and practical examples for effective file management.
publish_date: 2024-08-16T15:48:25.780981Z
last_updated: 2024-08-16T18:30:00Z
author: Vishwas Agrawal
header_image: https://docs.vultr.com/public/doc-assets/1903/da011a3c-39d6-44f1-880b-e05306f0512e.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## 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](https://docs.vultr.com/public/doc-assets/1903/4574120b-b476-4ffc-b8f1-bff0dfffaf7b.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/dd2425eb-c221-4e46-8c58-a0d770b8b128.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/9434de90-06ba-4e4d-b0b4-9fe79ef4e7bb.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/ea8f4d65-c96b-45fc-81ae-92c1fe8543fc.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/a9c893e3-55ba-46f7-940d-962e2b6e5d80.gif)

1. List files and directories recursively.

    ```console
    $ ls -R
    ```

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

    Output:

    ![ls -R command](https://docs.vultr.com/public/doc-assets/1903/5ecc6720-6584-4144-b6ac-1c94a8917076.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/586fbaf5-05c1-4d70-8f30-8da0a316b915.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/e64ead2a-c8cc-46d7-8fc0-ac7f9af2f293.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/fa1455f3-74fc-422f-8d64-c23d7bf72b15.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/565c3de9-9565-4a37-976a-369cdabbd886.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/ba9dd392-f954-4b96-915b-e5ac664255d5.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/d77c85fd-2819-4030-97a9-176a28722d83.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/4abb7457-cf96-4fa7-b632-a0380dbb73a4.gif)

## 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](https://docs.vultr.com/public/doc-assets/1903/ec3e1185-6cf1-43c8-acc5-d86ac4926246.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/4cb78635-31f2-4f89-8bb4-71bd3e37b815.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/d11cebb0-8a18-4c90-a88d-cf02dc08b416.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/4c9b46eb-b902-415e-a76f-af6101e5c67a.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/afa9b00b-b67c-4117-b77c-36c07ce506ba.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/daf9a6d6-634d-4c93-b669-d016b3f5216f.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/121af5ec-e596-4796-906e-d13cb3c36c40.gif)

1. 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](https://docs.vultr.com/public/doc-assets/1903/6581bc59-9c95-403b-8373-aab1377e9ea7.gif)

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