Linux ls Command: Content Listing

LabEx
3 min readJun 5, 2024

--

Linux ls Command: Content Listing

This tutorial provides an in-depth understanding of the ls command in Linux, including its purpose and usage. The ls command is a fundamental tool for listing files and directories in a given directory.

1. ls Command

The ls command is used to list files and directories in a specified location.

Command Usage

To use the ls command, open the terminal.

terminal

And first, by using cd /home/labex/project, we change to the specified path /home/labex/project.

Input:

cd /home/labex/project
ls

Output:

data_file.txt  test  test_file.txt  # test is a directory
ls

This command will list the files and directories in the current directory.

2. Parameters and Usage Examples

The ls command offers a variety of parameters to customize its behavior. We will introduce some of the most commonly used ones and provide usage examples in increasing order of complexity.

Option Parameter

ls [OPTION]

  • -l: In addition to the file name, more detailed information is provided, like file permissions, owner, file size, etc.
  • -a: -all List all files in the directory, including hidden files beginning with the symbol "." (full stop, punctuation mark)
  • -h: –human-readable List file sizes in an easy-to-understand format (eg 1K 234M 2G)

Example Usage

  1. List files in long format (-l)

The -l option displays file and directory details in a long format. It provides information such as file permissions, owner, group, file size, modification date, and filename. Here’s an example:

Input:

ls -l

Output:

total 12
-rw-rw-r-- 1 labex labex 12 Dec 1 14:45 data_file.txt
drwxrwxr-x 2 labex labex 4096 Dec 1 14:45 test
-rw-rw-r-- 1 labex labex 27 Dec 1 14:45 test_file.txt

2. Display Hidden Files (-a)

The -a option shows hidden files and directories, which are those that start with a dot (.). Hidden files are typically configuration files or directories. To list hidden items, use:

Input:

ls -a

Output:

.  ..  data_file.txt  test  test_file.txt

3. Display File Sizes in Human-Readable Format (-h)

The -h option makes file sizes more human-readable by using units like KB, MB, or GB. This is convenient for understanding the size of files: Input:

ls -lh

Output:

total 12K
-rw-rw-r-- 1 labex labex 12 Dec 1 14:35 data_file.txt
drwxrwxr-x 2 labex labex 4.0K Dec 1 14:35 test
-rw-rw-r-- 1 labex labex 27 Dec 1 14:35 test_file.txt

3. Summary

In this tutorial, we have covered the basics of the ls command, introduced common parameters, and provided usage examples. The ls command is a versatile tool for listing files and directories in a Linux system, and understanding its various options allows you to tailor your listings to specific needs.

🙌 Thanks for reading this far — we genuinely hope that we can help you in the programming road by making the overall experience fun, fun and fun. You can practice this tutorial with auto-validation on LabEx 🚀**

Want to contribute?

Here are few ways you can help.

  • Becoming an instructor;
  • Submitting your own tutorial;
  • Or you can simply make your own proposal;

Please do reach out if you’re interested. We don’t want to miss a talent like you!

Join our Discord or tweet us @WeAreLabEx !

--

--

LabEx

LabEx is an AI-assisted, hands-on learning platform for tech enthusiasts, covering Programming, Data Science, Linux and other areas.