ls

Overview

The ls command lists directory contents. It’s one of the most frequently used commands in Linux, providing information about files and directories.

Syntax

ls [options] [file/directory...]

Common Options

Option Description
-l Long listing format
-a Show all files (including hidden)
-h Human-readable sizes
-R Recursive listing
-t Sort by modification time
-S Sort by file size
-r Reverse sort order
-d List directories themselves
-i Show inode numbers
--color Colorize output

Key Use Cases

  1. List directory contents
  2. View file permissions
  3. Check file sizes
  4. Find recently modified files
  5. View hidden files

Examples with Explanations

Example 1: Basic Listing

ls -l

Shows detailed listing of current directory

Example 2: All Files with Human Readable Sizes

ls -lah

Shows all files including hidden ones with readable sizes

Example 3: Sort by Time

ls -lt

Lists files sorted by modification time

Understanding Output

Long format (-l) columns: - Permissions (drwxrwxrwx) - Number of links - Owner name - Group name - File size - Last modification time - File/directory name

Common Usage Patterns

  1. List recent files:

    ls -lt | head
  2. Find large files:

    ls -lSh
  3. List only directories:

    ls -ld */

Performance Analysis

  • Use with grep for filtering
  • Avoid -R on deep directories
  • Consider using find for complex searches
  • Use -1 for single column output
  • Limit directory depth when needed

Additional Resources