Sagar.BlogArticle
All posts
All posts
Linux

ls — List and Inspect Directory Contents

Learn ls inside out: show hidden files, read the long format output, sort by size and time, and combine options for powerful directory insights.

January 8, 20257 min read
LinuxFilesystemCommands

ls (list) is probably the command you'll run more than any other in your Linux career. It shows you what's in a directory — but with the right options, it reveals permissions, sizes, ownership, modification times, and file types, giving you a complete picture of any directory at a glance.

Basic Usage

ls                           # List current directory
ls /path/to/dir              # List a specific directory
ls /etc /var                 # List multiple directories
ls file1.txt                 # Info about a specific file

Most Important Options

ls Options

OptionLong FormDescription
-a--allShow all files including hidden (. files)
-A--almost-allShow hidden but NOT . and ..
-lLong format: permissions, size, date
-h--human-readableHuman-readable sizes (K, M, G) — use with -l
-tSort by modification time (newest first)
-SSort by file size (largest first)
-r--reverseReverse the sort order
-R--recursiveList subdirectories recursively
-d--directoryList the directory itself, not its contents
-F--classifyAdd / after dirs, * after executables
-1One file per line

Showing Hidden Files

Files whose names start with . are hidden by default — the OS doesn't show them in a normal listing. They're not protected or secret, just visually hidden to reduce clutter. Configuration files (dotfiles) use this convention: .bashrc, .gitignore, .ssh/.

ls              # Normal listing — hidden files not shown
ls -a           # Show ALL files (includes . and ..)
ls -A           # Show hidden files EXCEPT . and ..

# In your home directory:
ls -a ~
# . .. .bashrc .bash_history .gitconfig .ssh Documents Downloads Projects

The Long Format (-l)

The -l flag unlocks the richest output. Each line tells you seven things about a file:

ls -l

# Example output:
-rw-r--r--  1  sagar  staff   4096  Jan 28 10:30  notes.txt
drwxr-xr-x  3  sagar  staff    96   Jan 27 09:15  Projects/
lrwxrwxrwx  1  root   root     11   Sep 12 08:00  log -> /var/log
#│          │   │      │         │       │            │
#│          │   │      │         │       │            └── filename
#│          │   │      │         │       └── last modified
#│          │   │      │         └── size in bytes
#│          │   │      └── group owner
#│          │   └── user owner
#│          └── number of hard links
#└── permissions (10 characters)

The First Character: File Type

CharType
-Regular file
dDirectory
lSymbolic link
bBlock device (disk)
cCharacter device (terminal, serial)
pNamed pipe
sSocket
# Long format combinations you'll actually use:
ls -l           # Long format
ls -lh          # Long + human-readable sizes (shows "4.0K" not "4096")
ls -la          # Long + show hidden files
ls -lah         # Long + hidden + human-readable (most complete)
ls -lS          # Long + sorted by size (largest first)
ls -lt          # Long + sorted by time (newest first)
ls -ltr         # Long + sorted by time, reversed (oldest first — great for logs!)

Sorting

# Sort by modification time — newest files at the top
ls -lt /var/log

# Sort by time, reversed — oldest first (see most recent changes at bottom)
ls -ltr /var/log

# Sort by file size — largest first
ls -lS ~/Downloads

# Sort by size, reversed — find smallest files
ls -lSr ~/Downloads

Useful Patterns

# ─── Filtering ───────────────────────────────────────────────
ls *.txt                     # Only .txt files
ls *.{jpg,png,gif}           # Multiple extensions
ls -d */                     # Directories only (trailing /)
ls -p | grep -v /            # Files only (exclude dirs)

# ─── Counting ────────────────────────────────────────────────
ls -1 | wc -l               # Count files in current dir
ls -1 *.log | wc -l         # Count .log files

# ─── Recursive ───────────────────────────────────────────────
ls -R ~/Projects             # List everything recursively
ls -R | grep ".ts$"         # Find all .ts files recursively

# ─── Full paths ──────────────────────────────────────────────
ls -d "$PWD"/*               # Absolute paths of all files here

# ─── Indicators ──────────────────────────────────────────────
ls -F                        # dirs/ executables* links@

Quick Check

You want to find the 5 largest files in `~/Downloads`. Which command is most direct?

Quick Check

In `ls -l` output, what does the first character `d` mean?

Exercise

Practice with ls:

  1. List your home directory showing hidden files in long human-readable format
  2. Sort /var/log by modification time (newest first) and identify the most recently changed log file
  3. Count how many files are in /usr/bin
  4. List only directories inside /etc