ls

Filesystem

List directory contents with precise filtering and formatting.

Overview

ls lists directory entries. It is the first step for filesystem discovery and should be paired with filters that keep output readable.

Syntax

bash
ls [options] [path...]

Arguments

  • path: One or more files or directories to list.

Options Table

OptionDescription
-lLong format with permissions and metadata.
-aInclude hidden entries.
-hHuman-readable sizes (use with -l).
-tSort by modification time.
-SSort by size.

Examples

bash
ls -la /var/log
ls -lt /etc | head -n 10

Real-world Usage

Use ls -lt when triaging logs to quickly spot the newest files.

Common Mistakes

  • Forgetting -a and missing hidden files.
  • Using ls -l without -h and misreading sizes.

Performance Notes

ls is fast, but avoid listing huge directories over remote connections without filters.

Security Considerations

Never rely on ls output for security decisions. Validate permissions with stat or namei when needed.

Related Commands

stat, find, tree, du

Practice Exercises

  1. List the 10 newest files in /var/log.
  2. Sort /usr/bin by size and show the top 5.