Menu

ls command in Linux – Mastering the “ls” command in Linux

Written by Jagdeesh | 3 min read

Introduction

The ls command is one of the most fundamental commands in the Linux operating system. As a Linux user, you’ll find yourself using it frequently to list files and directories within the file system. This powerful command offers various options that enable you to customize the output to suit your needs. In this blog post, we will delve into the ls command, explore its various options, and provide clear code examples to help you master this essential tool.

1. The Basic “ls” Command

The simplest form of the ls command is used to list the contents of the current directory. To use it, simply type ‘ls’ and press Enter:

python
$ ls

2. Listing Files in a Specific Directory

To list the files in a specific directory, provide the directory path as an argument to the ls command:

python
$ ls /path/to/directory

For example, to list the contents of the /etc directory:

python
$ ls /etc

3. Displaying Detailed Information with the -l Option

To display detailed information about files and directories, use the -l (long listing) option:

python
$ ls -l

This will display the output in columns, including file permissions, owner, group, file size, modification date, and file name.

4. Showing Hidden Files with the -a Option

In Linux, files and directories starting with a dot (.) are considered hidden. To display these hidden files, use the -a (all) option:

python
$ ls -a

5. Sorting Files by Modification Time with the -t Option

To sort the files by their modification time, use the -t option:

python
$ ls -lt

By default, the files will be sorted in descending order, with the most recently modified files appearing first.

6. Displaying File Sizes in a Human-Readable Format with the -h Option

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

python
$ ls -lh

7. Displaying Directory Contents Recursively with the -R Option

To list the contents of a directory and its subdirectories recursively, use the -R (recursive) option:

python
$ ls -R /path/to/directory

8. Sorting Files by File Size with the -S Option

To sort files by their size, use the -S option:

python
$ ls -lS

By default, the files will be sorted in descending order, with the largest files appearing first.

9. Filtering Files by File Extension

You can filter the files listed by the ls command using a wildcard (*) followed by the desired file extension:

python
$ ls *.txt

This will display only the files with a .txt extension in the specified directory.

Conclusion

The ls command is a powerful and versatile tool for managing and exploring the Linux file system. By familiarizing yourself with its various options and modifiers, you can quickly and efficiently navigate through directories, view file details, and filter file listings according to your needs. Keep practicing with the examples provided in this post, and you’ll soon become a master of the ls command in Linux.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master Linux — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science