Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
ls - List Directory Contents
The ls command is one of the most fundamental and frequently used utilities in Linux/Unix systems for listing directory contents and displaying file information. It provides extensive options for customizing output format, sorting methods, filtering criteria, and display styles. Beyond simple directory listing, ls can reveal detailed file metadata, permissions, ownership, timestamps, and file types, making it an essential tool for system administration, file management, and directory navigation.
Basic Syntax
ls [OPTIONS] [FILE/DIRECTORY...]
Complete Options Reference
Display and Formatting Options
Basic Display Control
-a, --all- Show all files, including hidden files starting with.(except.and..)-A, --almost-all- Show all files except.and..-b, --escape- Print non-graphic characters in octal backslash notation-B, --ignore-backups- Don't list files ending with~-c- Sort files by change time (last status modification)-d, --directory- List directories themselves, not their contents--full-time- List times in full ISO format-g- Like-lbut don't show owner-G, --no-group- Don't show group in long listing-h, --human-readable- Show sizes in human-readable format (K, M, G, T)-H, --si- Like-hbut use powers of 1000 instead of 1024-i, --inode- Print inode number of each file-l- Use long listing format-L, --dereference- When showing file info for symbolic links, show info for the referenced file-m, --commas- Separate entries with commas-n, --numeric-uid-gid- Show numeric UID and GID instead of names-N, --literal- Print raw entry names without quoting-o- Like-lbut don't show group information-p, --indicator-style=slash- Append/indicator to directories-Q, --quote-name- Enclose entry names in double quotes-q, --hide-control-chars- Replace non-graphic characters with?-r, --reverse- Reverse order while sorting-R, --recursive- List subdirectories recursively-s, --size- Print size of each file in blocks--show-control-chars- Show non-graphic characters as-is (default)--color=WHEN- Colorize output (never, always, or auto)--indicator-style=WORD- Append indicator (none, slash, file-type, classify)--quoting-style=WORD- Use quoting style (literal, locale, shell, shell-always, c, escape)
Sorting Options
-f- Don't sort, enable-aU, disable-ls --color-F, --classify- Append indicator (one of*/=>@|) to entries--group-directories-first- Group directories before files-r, --reverse- Reverse order of sorting-S- Sort by file size, largest first--sort=WORD- Sort by word (none, size, time, version, extension)-t- Sort by modification time, newest first-U- Do not sort; list entries in directory order-v- Natural sort of version numbers within text-X- Sort alphabetically by entry extension
Time-Based Options
-c- Sort by change time (last status change)-u- Sort by access time-t- Sort by modification time (default for time-based sorting)--time=WORD- Show time as WORD (atime, access, use, ctime, status)--time-style=STYLE- Show times using STYLE (full-iso, long-iso, iso, locale, +FORMAT)
File Type and Style Indicators
File Type Indicators (with -F or --classify)
*- Executable file/- Directory@- Symbolic link|- Named pipe (FIFO)=- Socket>- Door
Color Coding (when --color=auto is enabled)
- Blue - Directories
- Green - Executable files
- Cyan - Symbolic links
- Yellow - Device files (block/character)
- Magenta - Socket files
- Red - Archives and compressed files
- Bright red - Broken symbolic links
Usage Examples
Basic Directory Operations
Simple Listings
# Basic directory listing
ls
# List specific directory
ls /home/user/documents
# List multiple directories
ls /var/log /tmp /etc
# List current directory's absolute path
ls $PWD
File Type Listing
# List directories only
ls -d */
# List files only (not directories)
ls -p | grep -v '/$'
# List executable files
ls -l | grep -E '^[^d].*x'
# List symbolic links
ls -l | grep '^l'
Advanced Display Formats
Long Format Variations
# Standard long format
ls -l
# Long format with human-readable sizes
ls -lh
# Long format with all files including hidden
ls -la
# Long format with inode numbers
ls -li
# Long format without group information
ls -lo
# Long format without owner information
ls -lg
# Long format with numeric IDs
ls -ln
Human-Readable Output
# Human-readable sizes (power of 1024)
ls -lh
# Human-readable with SI units (power of 1000)
ls -lH
# Sizes in blocks
ls -ls
# Show block sizes in KB
ls --block-size=K
Hidden Files and System Files
Complete File Listing
# Show all files including hidden
ls -a
# Show all files except . and ..
ls -A
# Show only hidden files
ls -d .*
# Show hidden files with details
ls -la .*
# List hidden directories only
ls -ld .*/
Configuration Files Discovery
# Find all configuration files
ls -la /etc/ | grep -E '\.(conf|cfg|ini)$'
# List user configuration files
ls -la ~/.config/
# Show system startup files
ls -la /etc/rc*.d/
# List cron files
ls -la /etc/cron.*
Sorting and Ordering
Time-Based Sorting
# Sort by modification time (newest first)
ls -lt
# Sort by modification time (oldest first)
ls -ltr
# Sort by access time
ls -lut
# Sort by change time
ls -lct
# Sort by creation time (if filesystem supports)
ls -l --time=birth
Size-Based Sorting
# Sort by file size (largest first)
ls -lS
# Sort by file size (smallest first)
ls -lSr
# Sort by size with human-readable format
ls -lhS
# Show largest files first with details
ls -lSh | head -20
Extension and Version Sorting
# Sort by file extension
ls -lX
# Natural version sorting
ls -lv
# Sort by extension and reverse order
ls -lXr
# Sort directories first
ls --group-directories-first -l
Recursive Directory Operations
Complete Directory Tree
# Recursive listing
ls -R
# Recursive with details
ls -lR
# Recursive with human-readable sizes
ls -lhR
# Recursive only specific directories
ls -R /etc/ | grep -E '^/etc/'
# Recursive with depth limit (using find)
find . -maxdepth 2 -type d -exec ls -l {} \;
Directory Structure Analysis
# Show directory structure with sizes
ls -lR | grep '^d'
# Count files in subdirectories
for dir in */; do echo "$dir: $(ls -1 "$dir" | wc -l) files"; done
# Find largest directories
du -h --max-depth=1 | sort -hr
Special File Listings
Symbolic Links Management
# List symbolic links with details
ls -l | grep '^l'
# List broken symbolic links
find . -type l ! -exec test -e {} \; -ls
# Follow symbolic links in listing
ls -Ll
# Show link targets
ls -lL
Device Files and Special Files
# List block devices
ls -l /dev | grep '^b'
# List character devices
ls -l /dev | grep '^c'
# List all special files
ls -l /dev | grep -E '^[bcdps]'
# Show device major/minor numbers
ls -l /dev/sd*
Filtering and Pattern Matching
File Extension Filtering
# List specific file types
ls *.txt *.md *.doc*
# List multiple file extensions
ls *.{jpg,jpeg,png,gif}
# List files by pattern
ls file[0-9]*.log
# Exclude certain file types
ls -I "*.tmp"
Complex Pattern Matching
# List files with specific naming patterns
ls config_*.conf
# List files containing specific text in name
ls *backup*
# List files with specific size patterns
ls -lS | grep -E '^[^-].*[0-9]+M'
# Find files modified within timeframe
find . -mtime -7 -exec ls -lt {} + | head -20
Practical Examples
System Administration
Log File Analysis
# List log files by size
ls -lhS /var/log/*.log
# Find recent log files
ls -lt /var/log/ | head -10
# Show system log directory structure
ls -lR /var/log/ | grep -E '^.*/.*:'
# Check log file permissions
ls -l /var/log/ | grep -v '^total'
# Find empty log files
find /var/log -name "*.log" -size 0 -exec ls -l {} \;
User and File Management
# List user home directories
ls -l /home/
# Show files with specific ownership
ls -l /home/user/ | grep username
# Find files with problematic permissions
find /data -type f ! -perm 644 -exec ls -l {} \;
# Check disk usage by directory
ls -lS /tmp/ | head -10
# List recently modified files
ls -lt /home/user/documents/ | head -20
Configuration Management
# Show system configuration files
ls -la /etc/sysconfig/ 2>/dev/null || ls -la /etc/default/
# List network configuration files
ls -la /etc/network/
# Show startup scripts
ls -l /etc/init.d/
# List cron jobs
ls -la /etc/cron.*/
# Check service configuration files
ls -la /etc/systemd/system/
Development Workflow
Project File Management
# List source files by type
ls -la src/ | grep -E '\.(c|cpp|h|hpp)$'
# Show recently modified source files
ls -lt src/ | head -10
# List build artifacts
ls -la build/ dist/
# Check file permissions for deployment
ls -la /var/www/html/
# List configuration files
ls -la config/ *.conf
Version Control Integration
# List untracked files (excluding .git)
ls -la --exclude='.git*'
# Show project structure excluding Git files
ls -R -I '.git*' -I 'node_modules*' -I '.vscode*'
# List recently changed files
find . -type f -mtime -1 -exec ls -lt {} + | head -20
# Compare directory contents
diff <(ls -1 dir1/) <(ls -1 dir2/)
Data Analysis and Reporting
File Size Analysis
# Find largest files in directory
ls -lSh | head -20
# Analyze file size distribution
ls -l | awk '{print $5}' | sort -n | uniq -c
# Show disk usage by file type
ls -la | awk '{print $1}' | sort | uniq -c | sort -nr
# Find files larger than specific size
find . -type f -size +100M -exec ls -lh {} \;
# Calculate total directory size
ls -la | awk '{total += $5} END {print "Total size:", total, "bytes"}'
File Age Analysis
# List files by modification date
ls -lt | awk '{print $6, $7, $8, $9}'
# Find files modified in last 24 hours
find . -mtime -1 -exec ls -lt {} +
# Group files by modification year
ls -la | awk '{print $8, $6}' | sort | uniq -f 1
# Show file age distribution
ls -la | awk '{print $6, $7}' | sort | uniq -c
Advanced Techniques
Custom Output Formatting
Creating Custom Views
# Custom format showing permissions, size, and name
ls -l | awk '{print $1, $5, $9}'
# List with file type indicators
ls -F | column -t
# Show files in columns with specific width
ls -C --width=80
# Create CSV output of directory contents
ls -la | awk 'NR>1{print $1","$2","$3","$4","$5","$6","$7","$8","$9}'
Integration with Other Tools
# Pipe to grep for filtering
ls -la | grep '^d.*user'
# Sort files externally
ls -la | sort -k9
# Count file types
ls -la | awk '{print $1}' | sort | uniq -c
# Generate file statistics
ls -la | awk '{sum+=$5; count++} END {print "Average size:", sum/count, "bytes"}'
Performance and Optimization
Handling Large Directories
# Limit output for large directories
ls -la | head -100
# Process large directories efficiently
ls -f | head
# Show directory size without listing all files
du -sh /path/to/large/directory/
# Use find for very large directories
find /large/path -maxdepth 1 -type f -exec ls -l {} + | head -50
Remote and Network Operations
# List remote directory via SSH
ssh user@server 'ls -la /remote/path/'
# Compare local and remote directories
diff <(ls -1 /local/path/) <(ssh user@server 'ls -1 /remote/path/')
# List files from FTP server (with lftp)
lftp -c 'open ftp.server.com && ls -la /remote/path/'
# Create file manifest for transfer
ls -laR > file_manifest.txt
Aliases and Customization
Useful Aliases
# Enhanced directory listing
alias ll='ls -alhF'
alias la='ls -Ah'
alias l='ls -CF'
alias ls.='ls -d .*'
# Time-based listings
alias lt='ls -ltrh'
alias lat='ls -alth'
# Size-based listings
alias lsize='ls -lhS'
alias lsmall='ls -lhSr'
# Directory-specific listings
alias ldir='ls -lhd */'
alias llink='ls -l | grep "^l"'
# Custom color settings
alias lsc='ls --color=always'
Environment Variables
# Set default block size
export BLOCKSIZE=1K
# Custom time format
export TIME_STYLE=long-iso
# Color settings
export LS_COLORS='di=1;34:fi=0:ln=35:pi=40;33:so=35:do=35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.zip=01;31'
Shell Functions
# Enhanced directory browsing
function ll() {
ls -alhF "$@" | less -F
}
# List files by size with limit
function lsize() {
local limit=${1:-20}
ls -lhS "$@" | head -n "$limit"
}
# List recently modified files
function lrecent() {
local days=${1:-7}
find . -type f -mtime -"$days" -exec ls -lt {} + | head -20
}
# Compare two directories
function lcompare() {
echo "=== $1 ==="
ls -la "$1" | awk '{print $9}' | sort > /tmp/dir1.txt
echo "=== $2 ==="
ls -la "$2" | awk '{print $9}' | sort > /tmp/dir2.txt
diff /tmp/dir1.txt /tmp/dir2.txt
rm /tmp/dir1.txt /tmp/dir2.txt
}
Troubleshooting
Common Issues and Solutions
Permission Problems
# Check directory permissions
ls -ld /path/to/directory
# Fix permission issues
chmod 755 /path/to/directory
# List files with permission issues
find . -type f ! -perm -u+r -exec ls -l {} \;
Symbolic Link Issues
# Find broken symbolic links
find . -type l ! -exec test -e {} \; -exec ls -l {} \;
# List symbolic links and their targets
ls -l | grep '^l'
# Check for circular symbolic links
find . -type l -exec readlink {} \;
Display Issues
# Fix terminal display issues
reset
# Handle non-ASCII characters
export LANG=en_US.UTF-8
ls --show-control-chars
# Deal with very long filenames
ls -1 | wc -l # Count files without displaying names
Related Commands
dir- Directory listing alternativevdir- Verbose directory listingfind- Find files and directoriestree- Display directory tree structurestat- Display file status informationdu- Disk usage summarywc- Word, line, character countpwd- Print working directorycd- Change directory
Best Practices
- Use meaningful aliases to create frequently used listing formats
- Combine with pipes for advanced filtering and analysis
- Use human-readable formats (-h) for better readability
- Leverage color coding to quickly identify file types
- Be careful with recursive operations on large directory trees
- Use appropriate sorting based on your current needs (time, size, extension)
- Check permissions when access denied errors occur
- Consider performance when listing very large directories
- Use time-based sorting to find recently modified files quickly
- Combine with other tools like grep, awk, and find for advanced analysis
Performance Tips
- Use
-1instead of-Cfor faster output on very large directories - Avoid
-Runless absolutely necessary for deep directory structures - Use specific patterns instead of listing everything and filtering
- Consider using
findfor complex file searches across large filesystems - Limit output with
headortailwhen dealing with large result sets - Use
-ffor unsorted listings when order doesn't matter - Disable color output (
--color=never) for faster processing - Use
-bwhen dealing with filenames containing control characters
The ls command is an essential tool that forms the foundation of Linux command-line file management. Its extensive options provide powerful capabilities for examining, sorting, and analyzing file system contents, making it indispensable for daily system administration and file navigation tasks.