Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux 'ls' command

May 13, 2024

Using the Linux ls Command: A Comprehensive Guide

The ls command is one of the most commonly used commands in Linux. It is used to list the files and directories in a specified location. In this blog post, we will explore the various options and examples of using the ls command to efficiently navigate and manage your files and directories.

Basic Usage

The basic syntax for the ls command is as follows:

1
ls [OPTIONS] [FILES/DIRECTORIES]

By executing ls without any options or parameters, it will list the files and directories in the current working directory.

Let’s look at some of the most frequently used options for the ls command:

List Files and Folders

To list files and directories in a specific location, simply provide the path of the directory as an argument:

1
ls /path/to/directory

Long Listing Format

To obtain a detailed view of files and directories, you can use the -l option:

1
ls -l /path/to/directory

The long listing format provides additional information such as file permissions, ownership, size, and modification timestamp.

Reverse Order

To reverse the order of the listing, you can use the -r option:

1
ls -r /path/to/directory

Sorting Files by Modification Time

By default, files and directories are sorted alphabetically. However, you can sort them by modification time (most recent first) using the -t option:

1
ls -lt /path/to/directory

Show Hidden Files

By default, the ls command does not display hidden files and directories. To show hidden files, use the -a option:

1
ls -a /path/to/directory

Hidden files and directories in Linux start with a dot ('.').

Human-Readable File Sizes

To display file sizes in a human-readable format (e.g., KB, MB, GB), use the -h option:

1
ls -lh /path/to/directory

Recursive Listing

To list files and directories recursively within a directory and its subdirectories, utilize the -R option:

1
ls -R /path/to/directory

This option is particularly useful when you want to see the contents of all subdirectories.

Multiple Directories

You can also provide multiple directories as arguments to the ls command, and it will list the contents of each directory:

1
ls /path/to/directory1 /path/to/directory2

Conclusion

The ls command is a versatile tool for listing files and directories in Linux. With its numerous options, you can customize the output to suit your specific needs. Whether it’s displaying hidden files, sorting by modification time, or listing files recursively, ls has got you covered.

Remember, for more detailed information about each option and additional features, refer to the man page of ls by running man ls in your terminal.

Happy listing!


➡️ Real-time Operating Systems for ESP32: Choosing the Right One for Your Project


⬅️ Introductory Guide to Working with SPI and I2C on ESP32


Go back to Posts.