Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ls Command Guide for Listing Directory Contents

This guide helps you understand and use the ls (list) command, the primary way to see what files and folders exist in a directory.


What is ls, and Why Use It?

The ls command lists the contents of a directory — the files, folders, and other entries it contains. On its own it shows names only, in columns, with no details.

Why Learn ls?

  • See what’s there: Check a folder’s contents before copying, moving, or deleting anything.
  • Inspect details: With flags you can see sizes, permissions, owners, and modification times.
  • Find things fast: Combined with sorting and filtering, ls is a quick search tool.
  • Confirm your work: After a cp or mv, ls is how you verify the result.

ls is the command you run most often of all. Getting comfortable with its flags pays off immediately.


1. List the Current Directory

ls
  • Shows the names of files and folders in your current working directory.

Example:

ls
Documents   Pictures   notes.txt   scripts

2. List a Specific Directory

ls /etc/nginx/
  • Works with any absolute path.
ls ../logs
  • Works with any relative path.
ls ~/Downloads
  • ~ expands to your home directory, so this lists your Downloads folder.

3. List Multiple Directories at Once

ls dir1 dir2 dir3
  • Each directory is shown under its own heading.
ls *
  • The shell expands * to every entry in the current directory, so this behaves like several arguments to ls.

4. Show Hidden Files

ls -a
  • -a (all) includes entries beginning with a dot, such as .bashrc and .git.
ls -A
  • -A (almost all) also hides . and .., which is usually what you want in scripts.

Hidden files are normal configuration files. Forgetting -a is the most common reason a folder “looks empty”.


5. Show Detailed Information

ls -l
  • Long format. One entry per line with permissions, link count, owner, group, size, modification time, and name.

Example:

ls -l notes.txt
-rw-r--r--  1 alice  staff  2048  4 Dec 10:32 notes.txt

Reading that line:

FieldValueMeaning
Permissions-rw-r--r--Who can read and write the file
Link count1Number of hard links
OwneraliceUser who owns the file
GroupstaffGroup that owns the file
Size2048Size in bytes
Modified4 Dec 10:32Last modification timestamp
Namenotes.txtThe file itself

6. Combine the Common Flags

ls -la
  • Long format and hidden files — the everyday “show me everything” command.
ls -alh
  • Adds -h (human-readable), so sizes appear as 2K, 1.5M, or 3.2G instead of raw bytes.
ls -lt
  • Sorts by modification time, newest first.
ls -ltr
  • -r (reverse) flips the sort, so the oldest entries come last — handy for tailing a log directory.

7. Sort and Filter Output

ls -lS
  • Sort by file size, largest first.
ls -lU
  • Sort by directory order rather than alphabetically (faster, but unsorted).
ls *.txt
  • Only entries matching the pattern. Combine with -l for details.
ls -d */
  • Lists directory names only, not their contents. -d means “describe the directory itself”.

8. Classify Entries

ls -F
  • Appends a character to each name: / for directories, * for executables, @ for symlinks, | for pipes.
ls -G
  • Colourises the output by file type. On many systems this is on by default; if colours are missing, add the flag explicitly.
ls --color=never
  • Forces colour off, which is useful when piping output into other tools.

9. Show Files in a Recursively Structured Way

ls -R
  • Recurses into subdirectories and lists each one.

For a proper visual tree, prefer tree, which draws the hierarchy with box-drawing characters. ls -R is hard to read past a couple of levels.


10. Sort Human Readably by Version

ls -v
  • Natural sort order, so file2.txt comes before file10.txt instead of after it.

Common Pitfalls

  • ls with no flags hides dotfiles. Use -a when looking for configuration files.
  • ls -l directory/ lists the contents, not the directory. Use ls -ld directory/ to see the directory’s own line.
  • Aliases can mislead. Some systems alias ls to ls --color=auto; ls -l | cat shows the unadorned output.
  • Sorting flags override each other. The last one on the command line wins: ls -ltS sorts by size, not time.

CommandDescription
cdMove into a directory
pwdPrint the current directory path
treeDraw a directory tree
fileIdentify a file’s type
statShow detailed file metadata
duReport directory disk usage
findSearch for files by name, size, or time
locateFind files quickly using a prebuilt database

ls Cheatsheet

CommandDescription
lsList current directory
ls path/List a specific directory
ls -aInclude hidden (dot) files
ls -lLong format with permissions and sizes
ls -hHuman-readable sizes
ls -laEverything, with details
ls -lhDetailed with human-readable sizes
ls -ltSort by modification time, newest first
ls -lSSort by size, largest first
ls -rReverse the sort order
ls -d */List directory names only
ls -FMark directories and executables
ls -RRecurse into subdirectories
ls -vNatural (version) sort order
ls --color=neverDisable colour output