Linux Terminal Command: ls (List Directory Contents)

The ls command is one of the very first commands you learn when entering the Linux terminal. It lists directory contents, displaying files, folders, symlinks, and sockets.

While typing ls seems simple, under the hood lies a powerful directory inspector capable of custom sorting, displaying file permissions, and revealing hidden system files.


Concept & Explanation

When you execute ls, it queries the operating system’s filesystem directory index and returns the names of the files located within that directory.

By default, the output is sorted alphabetically and formatted into columns. However, by appending flags, you can instruct ls to print detailed file metadata (permissions, owner, size, timestamps) which are read from the file’s inode (index node).


Anatomy of ls -l Output

When you run ls -l (long format), each line displays detailed metadata about a single file. Here is how to read it:

drwxr-xr-x  5  jehb  staff  160B  Jul  2 21:00  projects/
┬        ┬  ┬  ┬     ┬      ┬     ┬             ┬
│        │  │  │     │      │     │             └─ File Name
│        │  │  │     │      │     └─────────────── Modification Time
│        │  │  │     │      └───────────────────── File Size
│        │  │  │     └──────────────────────────── Group Owner
│        │  │  └────────────────────────────────── User Owner
│        │  └───────────────────────────────────── Number of Hard Links
│        └──────────────────────────────────────── File Permissions (rwx)
└───────────────────────────────────────────────── File Type (d = directory)

1. File Type Indicator (The first character)

2. Permissions (Next 9 characters)

Divided into three blocks of three: User (Owner), Group, and Others.

For example, rwxr-xr-x means the owner has read, write, and execute permissions, while group members and others can only read and execute.


1. Interactive Examples (Everyday Commands)

A. View Hidden Files (Dotfiles)

Files starting with a dot (like .bashrc or .git) are hidden by default to keep directories clean. To show them:

ls -a

B. Sorting by File Size

To find the largest files in a directory immediately:

ls -lS
ls -lhS

C. Sorting by Modification Date

To see the most recently modified files at the top of the list:

ls -lt
ls -ltr

2. Power-User Examples (Advanced Techniques)

A. Recursive Directory Tree Listing

To list all files in the current folder, all files in subfolders, and so on:

ls -R

B. Appending Format Indicators

To quickly differentiate files, directories, and symlinks without long listing, use the -F flag:

ls -F

C. Customizing Colors (LS_COLORS)

The terminal colorizes outputs based on the LS_COLORS environment variable. You can verify your settings using:

echo $LS_COLORS

To enable color-coding automatically, make sure your alias is defined with:

alias ls='ls --color=auto'

⚙️ Warning & Common Pitfalls

[!CAUTION] Do not parse the output of ls in shell scripts.

A common beginner mistake is using ls in loops to process files:

# BAD PATTERN - DO NOT USE
for file in $(ls *.txt); do
    mv "$file" "${file%.txt}.bak"
done

If any file contains spaces or newlines (e.g. My Report.txt), the shell will split the file name into two separate arguments (My and Report.txt), causing your script to fail or delete the wrong files.

The Safe Alternative: Use shell glob expansion:

# GOOD PATTERN - Safe for spaces and special characters
for file in *.txt; do
    [ -e "$file" ] || continue # Handle empty matches
    mv "$file" "${file%.txt}.bak"
done

Here are some related posts on cli_tty1 you might want to check out: