ls - Looking Around

ls lists directory contents. It's probably the command you'll run most often.

Basic Usage

Terminal
$ls
Documents Downloads projects notes.txt

By default, ls shows the contents of your current directory, names only, sorted alphabetically.

ls with a Path

Specify a directory to list its contents:

Terminal
$ls /etc
apt bash.bashrc crontab hosts passwd ssh ...
$ls ~/Documents
report.pdf notes.txt budget.xlsx

The Essential Flags

-l (Long Format)

This is the flag you'll use most:

Terminal
$ls -l
total 16 drwxr-xr-x 2 user user 4096 Jan 14 10:00 Documents drwxr-xr-x 2 user user 4096 Jan 14 10:00 Downloads drwxr-xr-x 3 user user 4096 Jan 14 09:30 projects -rw-r--r-- 1 user user 156 Jan 14 08:00 notes.txt

Each column means:

  1. Permissions (drwxr-xr-x)
  2. Link count (number of hard links)
  3. Owner (user)
  4. Group (user)
  5. Size (in bytes)
  6. Modified date
  7. Name

First Character

The first character tells you the type:

  • d = directory
  • - = regular file
  • l = symbolic link

-a (All Files)

Shows hidden files (those starting with .):

Terminal
$ls
Documents Downloads projects
$ls -a
. .. .bashrc .config Documents Downloads projects

Hidden files are everywhere in Linux - configs, caches, history files. You need -a to see them.

-h (Human Readable)

Makes file sizes readable:

Terminal
$ls -l
-rw-r--r-- 1 user user 1048576 Jan 14 10:00 video.mp4
$ls -lh
-rw-r--r-- 1 user user 1.0M Jan 14 10:00 video.mp4

Instead of 1048576, you see 1.0M. Much better.

Understanding Colors

Most terminals color-code ls output:

ColorMeaning
BlueDirectory
GreenExecutable file
CyanSymbolic link
RedBroken link or archive
YellowDevice file
MagentaImage/media file
White/DefaultRegular file

Color-Coded Context

When you see blue, you know you can cd into it. Green means you can run it with ./filename. This visual feedback helps you work faster.

Combining Flags

Flags can be combined:

Terminal
$ls -la
(all files in long format)
$ls -lah
(all files, long format, human readable)
$ls -lh
(long format, human readable, no hidden)

ls -lah is so common it's worth memorizing as a single unit: "ell-ay-aitch."

My Go-To

I use ls -lah probably 50 times a day. It shows everything: hidden files, permissions, human-readable sizes. Make it muscle memory.

Other Useful Flags

-t (Sort by Time)

Most recently modified first:

Terminal
$ls -lt
-rw-r--r-- 1 user user 156 Jan 14 15:30 newest.txt -rw-r--r-- 1 user user 892 Jan 14 10:00 older.txt -rw-r--r-- 1 user user 234 Jan 13 08:00 oldest.txt

-r (Reverse)

Reverse the sort order:

Terminal
$ls -ltr
(oldest first - useful for logs)

-S (Sort by Size)

Largest files first:

Terminal
$ls -lS
(biggest files at top)

-R (Recursive)

List subdirectories too:

Terminal
$ls -R projects/
projects/: website app projects/website: index.html style.css projects/app: main.py config.json

Quick Reference

CommandWhat It Shows
lsBasic list
ls -lLong format with details
ls -aInclude hidden files
ls -laAll files with details
ls -lahAll files, details, human sizes
ls -ltSort by modification time
ls -lSSort by size
ls -RRecursive listing

Listing Specific Files

Use wildcards to filter:

Terminal
$ls *.txt
notes.txt readme.txt log.txt
$ls -l *.pdf
-rw-r--r-- 1 user user 45K report.pdf -rw-r--r-- 1 user user 120K manual.pdf
Knowledge Check

Which command shows ALL files (including hidden) with detailed info and human-readable sizes?

Key Takeaways

  • ls shows directory contents
  • -l gives detailed (long) format
  • -a includes hidden files
  • -h makes sizes human-readable
  • ls -lah is your new best friend
  • -t sorts by time, -S sorts by size

Next: cd - how to actually move around the file system.