Linux File Listing: ls, tree, and Modern Alternatives
Listing files and inspecting directory contents is among the most frequent tasks in Linux systems administration and development. This guide covers the ubiquitous ls command, anatomy of file metadata, tree-structured visualizations, pattern matching, and modern listing utilities.
⚡ Quick Dive
Essential Listing Commands Cheat Sheet
| Command | Action | Use Case |
|---|---|---|
ls -la |
List all files in long format including hidden (.) |
Standard inspection of directory contents |
ls -lh |
Long format with human-readable file sizes (K, M, G) | Inspecting file sizes |
ls -lht |
Long format sorted by modification time (newest first) | Checking recent changes/deployments |
ls -lhS |
Long format sorted by file size (largest first) | Locating large files consuming disk space |
ls -d */ |
List only subdirectories in current location | Directory-only filtering |
ls -1 |
Single column output (one file per line) | Shell scripts and piping to xargs/loops |
tree -L 2 |
Display hierarchical directory tree 2 levels deep | Overview of project directory structure |
tree -d |
Display only directories in a tree view | Architecture layout visualization |
eza -la --git |
Modern colorful listing with Git status (if installed) | Modern interactive terminal workflow |
Common One-Liners
# Detailed view of all files with human-readable sizes and file type indicators
ls -lahF
# List 5 newest modified files in directory
ls -lt | head -n 6
# View directory tree up to 3 levels ignoring node_modules and .git
tree -L 3 -I "node_modules|.git"
📖 Extended Guide
1. ls Command Anatomy & Long Listing Format
Running ls -l outputs a detailed multi-column view of filesystem metadata:
-rw-r--r-- 1 ubuntu www-data 4.2K Aug 29 12:00 app.js
drwxr-xr-x 3 ubuntu www-data 4.0K Aug 29 11:30 config
lrwxrwxrwx 1 ubuntu www-data 14 Aug 29 11:00 current -> /var/www/v1.2
Field Breakdown:
- rw-r--r-- 1 ubuntu www-data 4.2K Aug 29 12:00 app.js
│ └──┬────┘ │ └──┬──┘ └───┬──┘ └─┬─┘ └─────┬────┘ └──┬─┘
│ │ │ │ │ │ │ └─ File/Dir Name
│ │ │ │ │ │ └─ Modification Date/Time
│ │ │ │ │ └─ File Size in Bytes (or Human Units with -h)
│ │ │ │ └─ Owning Group
│ │ │ └─ Owning User
│ │ └─ Number of Hard Links (Directories have at least 2: self and '.')
│ └─ 9-character Permission Triplet (User, Group, Others)
└─ File Type Indicator (- regular file, d directory, l symlink, c character device, b block device, s socket, p named pipe)
2. Comprehensive ls Options Reference
View & Formatting Options
-a(--all): Include hidden files and directories (names starting with.).-A(--almost-all): Include hidden files, but omit.(current) and..(parent).-l: Long listing format with file metadata.-h(--human-readable): Use unit suffixes (e.g., 1K, 234M, 2G) instead of raw bytes with-l.-F(--classify): Append indicator suffix:/for directory,*for executable,@for symlink,=for socket,|for FIFO.-p: Append/indicator to directories.-i(--inode): Print the inode number of each file.-1: List one file per line (useful when piping to tools likexargs).-d(--directory): List directory entries themselves, rather than listing their contents.
Sorting Options
-t: Sort by modification time, newest first.-S: Sort by file size, largest first.-X: Sort alphabetically by file extension.-v: Natural sort of version numbers (e.g.,v1.2,v1.9,v1.10).-r(--reverse): Reverse whatever sort order is active.-U: Do not sort; list entries in raw directory order (fastest for massive directories).
Advanced Date & Time Formatting
# ISO 8601 full timestamp
ls -l --time-style=full-iso
# Custom formatted timestamp
ls -l --time-style="+%Y-%m-%d %H:%M:%S"
3. Pattern Matching & Wildcards (Globbing)
Linux shells expand glob patterns before passing arguments to ls:
| Pattern | Description | Example |
|---|---|---|
* |
Matches zero or more characters | ls *.log |
? |
Matches exactly one character | ls config_?.yaml |
[abc] |
Matches any one character in the set | ls image[0-9].png |
[!abc] |
Matches any character NOT in the set | ls *.[!t][!x][!t] |
{a,b} |
Brace expansion (shell generates combinations) | ls *.{jpg,jpeg,png,gif} |
Practical Pattern Examples
# List all files starting with test, ending in .js or .ts
ls test*.{js,ts}
# List all hidden dotfiles in home directory
ls -d ~/.*
# List directories only in the current location
ls -d */
4. Hierarchical Visualizations with tree
The tree command recursively produces a depth-indented directory layout.
Essential tree Options
-L <depth>: Limit display to<depth>directory levels.-d: List directories only.-a: Include hidden files.-I "<pattern>": Ignore files matching pattern (e.g.,tree -I "vendor|cache").-h: Print human-readable sizes alongside files.--du: Calculate and print accumulated size for directories.
Examples
# View project structure 2 levels deep with directory sizes
tree -L 2 --du -h
# Tree view excluding build artifacts and git folders
tree -L 3 -I "node_modules|dist|.git"
5. Modern Alternatives: eza / exa
Many modern Linux servers and development environments replace standard ls with eza (a modern, maintained fork of exa written in Rust):
# High-information view with Git status, icons, and human dates
eza -la --git --time-style=relative
# Tree view integrated directly into eza
eza --tree --level=2 --git-ignore
6. Recommended Production Shell Aliases
Add these aliases to your ~/.bashrc or ~/.zshrc for faster day-to-day workflow:
# Long format with human sizes and file classifications
alias ll='ls -lahF --color=auto'
# Sort by modification time (newest first)
alias lt='ls -laht --color=auto'
# Sort by size (largest first)
alias lS='ls -lahS --color=auto'
# Directory-only listing
alias ld='ls -ld */'
# Quick tree alias
alias t='tree -L 2 -C'
Apply your changes immediately:
source ~/.bashrc