Linux Curriculum
File and Directory Basics
File Viewing and Editing
File Searching
Text Processing
File Compression and Archiving
File Links
On this page
Subtopic•File and Directory Basics

Listing Files

The ls (list) command is one of the foundational utilities in Linux and Unix-like operating systems. It queries the filesystem directory structure and displays the contents—files, directories, symbolic links, sockets, named pipes, and device nodes—along with their associated metadata (permissions, ownership, file size, timestamps).

In DevOps and cloud engineering, navigating directories and inspecting file attributes via ls is a daily necessity: verifying application deployments, validating configuration permissions in /etc, inspecting log rotations in /var/log, or checking mounted container volumes.

Command Syntax and Anatomy

ls [OPTIONS] [FILE/DIRECTORY...]

When run without arguments, ls lists files in the current working directory in alphabetical order, omitting hidden files (files beginning with a dot .).


Essential ls Command Flags

FlagLong OptionDescriptionDevOps Practical Use Case
-l--format=longUses the long listing format (permissions, links, owner, group, size, date, name).Auditing security permissions and ownership of critical config files.
-a--allShows all files including hidden files starting with . (e.g., .bashrc, .env, .git).Inspecting dotfiles, git repositories, and container environment configs.
-A--almost-allShows all hidden files except . (current dir) and .. (parent dir).Clean inspection of directory contents without recursive navigation anchors.
-h--human-readableFormats file sizes with readable units (K, M, G) instead of raw byte counts.Checking sizes of database dumps, container logs, and backup archives.
-t--sort=timeSorts files chronologically by modification time, newest first.Pinpointing recently crashed services or newly generated log files.
-r--reverseReverses the current sorting order (e.g., oldest first with -t).Finding the oldest backup file to delete during disk space cleanup.
-R--recursiveRecursively lists all subdirectories and their contents.Inspecting nested project structures or deeply nested web assets.
-S--sort=sizeSorts files by file size, largest first.Troubleshooting disk capacity issues ("disk full" alert investigation).
-d--directoryLists directory entries themselves rather than their contents.Inspecting permissions of a folder (e.g., ls -ld /var/www/html).
-i--inodePrints the filesystem index number (inode) of each file.Debugging hard links, filesystem corruption, or unremovable files.
-F--classifyAppends indicators (/ for dir, * for executable, @ for symlink).Quick terminal visual triage without color support.
-1(number one)Displays one file per line.Shell script pipelines and loop iterations (for f in $(ls -1)).

Anatomy of the Long Listing Output (ls -l)

Running ls -la /var/log/nginx produces output similar to this:

$ ls -lh /var/log/nginx
total 2.4M
-rw-r--r-- 1 www-data adm  1.2M Oct 14 09:15 access.log
-rw-r----- 1 www-data adm  842K Oct 13 23:59 access.log.1.gz
-rw-r--r-- 1 www-data adm   14K Oct 14 08:30 error.log
lrwxrwxrwx 1 root     root   21 Oct 10 12:00 current -> /var/log/nginx/access.log
drwxr-xr-x 2 root     root 4.0K Oct 14 00:00 archives/

Breaking Down Each Column

-rw-r--r--   1   www-data   adm    1.2M   Oct 14 09:15   access.log
  [ 1 ]    [2]     [3]     [4]    [5]        [6]           [7]
  1. File Type & Permissions (10 characters):
    • 1st character: File type indicator:
      • - : Regular file
      • d : Directory
      • l : Symbolic link
      • c : Character device (e.g., /dev/urandom)
      • b : Block device (e.g., /dev/sda1)
      • s : Unix domain socket (e.g., /var/run/docker.sock)
      • p : Named pipe (FIFO)
    • Characters 2–4: User (owner) permissions (rwx = read, write, execute).
    • Characters 5–7: Group permissions (r-x).
    • Characters 8–10: Others/World permissions (r--).
  2. Hard Link Count: The number of hard links pointing to this inode. For directories, this reflects subdirectories (due to internal . and .. links).
  3. Owner Username: The user account that owns the file (www-data).
  4. Group Name: The primary group associated with the file (adm).
  5. File Size: Size formatted in human-readable units (1.2M) when -h is supplied.
  6. Last Modification Timestamp: Date and time when file content was last written.
  7. File Name: Name of the resource, plus target arrow -> if it is a symbolic link.

High-Frequency DevOps Recipes

1. Identify the 10 Largest Files in a Directory

When troubleshooting disk space exhaustion on a server:

ls -lhS /var/log | head -n 11

Note: head -n 11 accounts for the total header line plus the top 10 files.

2. View Recently Modified Files

Find what changed on the server within the last few minutes:

ls -lt /etc/nginx/conf.d | head -n 5

Or view in reverse order so the newest file appears at the bottom of your terminal screen:

ls -ltr /var/log

3. Check Directory Permissions (Not Contents)

Common mistake: running ls -l /var/www/html displays the contents of /var/www/html. To inspect permissions of the directory itself:

$ ls -ld /var/www/html
drwxr-xr-x 3 www-data www-data 4096 Oct 12 14:20 /var/www/html

4. Display Hidden Files and Dotfiles

Inspect application secrets and version control manifests:

ls -la /app
drwxr-xr-x  5 appuser appgroup 4096 Oct 14 09:00 .
drwxr-xr-x 12 root    root     4096 Oct 10 10:00 ..
-rw-------  1 appuser appgroup  420 Oct 14 08:45 .env
drwxr-xr-x  8 appuser appgroup 4096 Oct 14 09:10 .git
-rw-r--r--  1 appuser appgroup  210 Oct 14 08:30 .gitignore
-rw-r--r--  1 appuser appgroup 1890 Oct 14 09:00 Dockerfile

5. Inspect File Inodes

ls -i /etc/hosts
# 131074 /etc/hosts

Modern Linux Alternatives: exa / eza

On modern DevOps developer machines and cloud instances, modern replacements like eza (formerly exa) provide color-coded output, Git integration status, and tree views:

# Eza command equivalent with git status
eza -la --git --header

However, standard POSIX ls is guaranteed to exist on every minimal Docker image, Alpine container, enterprise RHEL server, and embedded appliance. Mastering standard ls flags is essential for any production engineer.


Common Pitfalls and Best Practices

[!WARNING] Do not parse ls in shell scripts. Avoid writing: for file in $(ls *.log); do ... done. If filenames contain spaces, newlines, or special characters, shell word-splitting will break your script. Instead, use native globbing:

for file in *.log; do
    [ -e "$file" ] || continue
    echo "Processing $file..."
done

[!TIP] Add handy aliases to your ~/.bashrc or ~/.zshrc:

alias ll='ls -lah --color=auto'
alias llt='ls -lahtr --color=auto'

Summary

  • ls lists directory contents and queries filesystem metadata via POSIX system calls.
  • The standard daily triage command is ls -lah (long, all files, human-readable).
  • To sort chronologically, use -t (newest first) or -ltr (oldest first, newest at the bottom).
  • Use ls -ld <directory> to view permissions of the directory itself rather than its inner contents.
  • Never parse ls output in automated bash scripts; rely on shell globbing or find.
←Previous ModuleLinux Fundamentals