SyntaxStudy
Sign Up
Linux / Bash Listing and Inspecting Files with ls and tree
Linux / Bash Beginner 1 min read

Listing and Inspecting Files with ls and tree

The `ls` command is one of the most frequently used commands in Linux. It lists directory contents and accepts a rich set of options to control what information is shown and how it is sorted. By default, `ls` hides files whose names begin with a dot — these "hidden" files typically contain configuration data. The long-format option `-l` shows file permissions, link count, owner, group, file size, modification time, and name. Combining `-l` with `-h` produces human-readable file sizes. The `-a` flag includes hidden files. Sorting options include `-t` for modification time, `-S` for size, and `-r` to reverse any sort order. The `tree` command provides a visual recursive view of directory structures, which is very useful for understanding project layouts. It must usually be installed separately (`apt install tree`). For large trees, use `-L` to limit the depth and `-I` to ignore patterns such as `node_modules` or `.git`.
Example
# Basic listing
ls
ls /usr/local/bin

# Long format — permissions, size, date
ls -l
# -rw-r--r-- 1 alice alice 4096 Apr 10 09:22 notes.txt

# Long format + human-readable sizes + hidden files
ls -lah

# Sort by modification time (newest first)
ls -lt

# Sort by size (largest first)
ls -lS

# Reverse sort (oldest or smallest first)
ls -ltr

# List only directories
ls -d */

# Colorised output (usually default on modern distros)
ls --color=auto

# ---- tree ----
# Install if needed: sudo apt install tree

# Show full tree from current directory
tree

# Limit to 2 levels deep
tree -L 2

# Show hidden files
tree -a

# Show sizes
tree -sh

# Ignore specific directories
tree -I 'node_modules|.git|__pycache__'

# Print only directories
tree -d