<< back to Guides
<< back to Guides
ls Command: A Complete Guide
The ls
command lists the contents of a directory. It’s one of the most basic and frequently used Linux commands.
Basic Syntax
ls [OPTIONS] [FILES or DIRECTORIES]
1. Basic Usage
- List files in the current directory:
ls
- List files in a specific directory:
ls /path/to/directory
2. Commonly Used Options
Option | Description |
---|---|
-a |
Show all files, including hidden ones |
-l |
Long listing format |
-h |
Human-readable sizes (with -l ) |
-t |
Sort by modification time |
-r |
Reverse the order |
-S |
Sort by size |
-R |
Recursively list subdirectories |
-d |
List directories themselves, not contents |
3. Combining Options
- List all files (including hidden ones) in long format:
ls -al
- List files sorted by modification time:
ls -lt
- List files with human-readable sizes:
ls -lh
- Recursively list files in all subdirectories:
ls -R
4. Special Listings
- List only hidden files:
ls -d .*
- List files sorted by size in descending order:
ls -lS
- List directories without their contents:
ls -d */
5. Using Wildcards
- List files with a specific extension:
ls *.txt
- List files that start with a specific letter:
ls a*
- List files that end with a number:
ls *[0-9]
6. Using ls with Color Output
The ls command often color-codes its output for easier readability:
Blue: Directories
- Green: Executable files
- Red: Compressed files
- Yellow: Devices or special files
To enable color (if not by default):
ls --color=auto
7. Advanced Examples
- List files in size order with human-readable sizes:
ls -lhS
- List files modified within the last 24 hours:
ls -lt --time-style="+%Y-%m-%d %H:%M" | head
- List files with details and directory sizes (using du):
ls -l | grep '^d' | awk '{print $9}' | xargs -I {} du -sh {}
8. Helpful Aliases
You can make ls easier to use by setting up aliases in your ~/.bashrc or ~/.zshrc:
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
Apply the changes:
source ~/.bashrc
Commonly Used ls Aliases
Alias | Command | Description |
---|---|---|
ll |
alias ll='ls -alF' |
Long listing format with file type symbols (/ , * , etc.) |
la |
alias la='ls -A' |
List all files, excluding . and .. |
l |
alias l='ls -CF' |
List with color and classifying symbols (/ for directories, * for executables, etc.) |
lsa |
alias lsa='ls -al' |
List all files in long format (includes . and .. ) |
lst |
alias lst='ls -lt' |
List files sorted by modification time, newest first |
lS |
alias lS='ls -lS' |
List files sorted by size, largest first |
ld |
alias ld='ls -d */' |
List directories only |
lx |
alias lx='ls -lX' |
List files sorted by extension |
lq |
alias lq='ls -lQ' |
List files with filenames enclosed in double quotes |
Cheat Sheet
Common Options
Option | Description | Example Usage |
---|---|---|
-a |
Show all files, including hidden files (those starting with . ) |
ls -a |
-A |
Show all files, excluding . and .. |
ls -A |
-l |
List files in long format (detailed view) | ls -l |
-h |
Human-readable file sizes (works with -l ) |
ls -lh |
-t |
Sort by modification time (newest first) | ls -lt |
-r |
Reverse the sorting order | ls -lr |
-S |
Sort by file size (largest first) | ls -lS |
-R |
Recursively list subdirectories | ls -R |
-d |
List directories themselves, not their contents | ls -d */ |
-1 |
List one file per line (useful for scripting) | ls -1 |
Sorting Options
Option | Description | Example Usage |
---|---|---|
-t |
Sort by modification time (most recent first) | ls -lt |
-S |
Sort by file size (largest first) | ls -lS |
-X |
Sort by extension | ls -lX |
-v |
Sort by version (numerical sorting for version numbers) | ls -lv |
Filtering Options
Option | Description | Example Usage |
---|---|---|
--color=auto |
Enable colored output (auto-detects terminal) | ls --color=auto |
-I |
Ignore files that match the given pattern | ls -I "*.bak" |
-f |
Do not sort the output (immediate listing) | ls -f |
-Q |
Enclose filenames in double quotes | ls -Q |
Advanced Usage
Option | Description | Example Usage |
---|---|---|
-L |
Follow symbolic links and list the file they point to | ls -lL |
-p |
Append a / to directory names |
ls -p |
-F |
Classify files with a symbol (/ for directories, * for executables, etc.) |
ls -F |
-B |
Exclude backup files (those ending with ~ ) |
ls -B |
-X |
Sort files by extension | ls -X |
--block-size=SIZE |
Set the block size used for file sizes (e.g., KB, MB) | ls -lh --block-size=M |
Using Wildcards with ls
Wildcard | Description | Example Usage |
---|---|---|
* |
Matches any string of characters | ls *.txt |
? |
Matches exactly one character | ls file?.txt |
[] |
Matches any single character inside the brackets | ls file[1-5].txt |
! |
Matches anything except the following pattern | `ls !(file1 |