Quick take: The ls command lists the contents of a directory. Use -l for a detailed long listing, -a to include hidden files, -h for human-readable sizes, and -t to sort by modification time.
Introduction
The ls command (short for list) is the command you will run more than any other in Linux. It shows the files and directories in a location, and with the right options it reveals permissions, ownership, size, and timestamps — everything you need to understand what is in a directory.
This guide covers the most useful ls options, how to combine them, and the practical listings you will reach for daily as a Linux user or administrator.
Syntax
The basic syntax of the ls command is:
ls [OPTIONS] [FILE/DIRECTORY...]Common Options and Parameters
The most useful options and parameters for the ls command:
| Option | Description |
|---|---|
| -l | Long format — show permissions, owner, group, size, and modification date. |
| -a | Show all files, including hidden ones that start with a dot. |
| -A | Like -a but omit the . and .. entries. |
| -h | Human-readable sizes (KB, MB, GB) — combine with -l. |
| -t | Sort by modification time, newest first. |
| -r | Reverse the sort order. |
| -S | Sort by file size, largest first. |
| -R | Recursive — list subdirectories and their contents. |
| -d | List the directory itself, not its contents. |
| -i | Show each file's inode number. |
| -1 | List one file per line. |
| -F | Append an indicator (/ for directories, * for executables). |
| --color=auto | Colourise output by file type. |
Practical Examples
Real ls commands you can run today:
# Detailed listing with human-readable sizes
ls -lh
# Show hidden files too
ls -la
# Sort by newest first
ls -lt
# Sort by largest file first
ls -lhS
# List a specific directory
ls -l /var/log
# Recursively list everything under a path
ls -R /etc/nginx
# Show only directories in the current folder
ls -d */Combining ls with Other Commands
ls becomes far more powerful when its output is piped into other tools. Because the long format is line-based, you can filter, count, and slice it with the standard pipeline commands.
# Count the files in a directory
ls -1 | wc -l
# Show only the 5 most recently modified files
ls -lt | head -n 6
# List only directories using grep
ls -l | grep '^d'
# Find the largest files in a folder
ls -lhS | headThese one-liners answer everyday questions — “how many files are here?”, “what changed last?”, “what is taking space?” — without leaving the shell.
Real-World Use Cases
Administrators lean on ls constantly. When auditing a web server, ls -l /var/www reveals whether files are owned by the right user and carry safe permissions. When chasing a problem, ls -ltr /var/log puts the most recently written log at the bottom of the screen so you can spot the active one instantly. And when a script fails to run, ls -l script.sh quickly shows whether the execute bit is missing.
Because ls reports the metadata that governs access, it is usually the first command you run when something does not behave as expected — before you even open a file.
Tips and Best Practices
- Combine flags freely:
ls -lhrtshows a detailed, human-readable listing sorted oldest-to-newest — perfect for finding the latest log file. - Add
alias ll='ls -lh'to your~/.bashrcfor a shortcut you will use constantly. - Use
ls -d */to list only subdirectories without recursing into them.
Final Thoughts
The ls command looks simple but rewards mastery. Memorise the core flags — -l, -a, -h, -t — and learn to stack them, and you will read any directory at a glance. Pair it with cd to move around and find to search deeper.
FAQ: ls Command in Linux
What is the difference between ls -a and ls -A?+
ls -a shows every entry including the current directory (.) and parent directory (..). ls -A shows hidden files but hides those two special entries, which keeps the output cleaner.
How do I show file sizes in a readable format?+
Combine -l and -h: ls -lh prints sizes as 4.0K, 12M, or 1.2G instead of raw byte counts.
How do I sort files by date with ls?+
Use ls -lt to sort newest first, or add -r to reverse it (ls -ltr) so the most recent file appears last — handy for logs.
How do I list only directories?+
Run ls -d */ to list directories in the current location, or use ls -l | grep '^d' to filter the long listing for directory entries.
Why are some files coloured in ls output?+
Most distributions alias ls to ls --color=auto, which colours files by type — blue for directories, green for executables, and so on. It is purely visual and does not change the files.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support