Ubuntu dir Command Guide: Practical Tips & Differences from ls

Overview

Inspecting directory contents is one of the most fundamental tasks on any Linux workstation or server. While administrators universally rely on standard utilities, the GNU coreutils dir command provides a dependable alternative for listing files and subdirectories. This hands-on tutorial walks through practical everyday sorting flags, compares the runtime behavior of dir against the ubiquitous ls, and demonstrates how to tailor output for efficient terminal workflows.

Setting Up a Test Workspace and Running Basic Directory Inquiries

To begin experimenting in an isolated environment, establish an empty testing directory and navigate straight into it by invoking mkdir -p ~/dir_practice && cd ~/dir_practice.

Initialize practice environment with mkdir and cd

Populate the workspace with diverse document formats by launching touch report1.txt report2.txt data.csv README.md.

Create multi-extension mock files

Next, introduce a realistic directory tree with distinct subfolders by issuing mkdir -p docs backups logs.

Create sample subdirectories

When executed without arguments, dir scans the current working location and prints all non-hidden objects horizontally across the terminal grid in alphabetical order.
⚙️ [Essential CLI Flags]

-a : Exposes every single directory item, including dotfiles and dot-directories (. and ..).
-l : Generates a detailed vertical table showing file permissions, owner accounts, group ownership, sizes, and timestamp records.
-h : Expresses raw file capacities in easily digestible human-friendly metrics (e.g., KB, MB, GB).
-t : Reorders listed items dynamically by prioritizing the most recently modified records.
-S : Arranges the returned entries according to disk usage, descending from biggest to smallest.
-r : Inverts whatever ordering scheme is currently applied across the queried items.

Default dir execution in practice directory

Uncovering Hidden Items and Inspecting Metadata

Linux systems routinely conceal dotfiles to prevent clutter. To demonstrate hidden file handling, construct a few configuration placeholders using touch .config_hidden .env_sample.

Generate sample hidden dotfiles

Supplying the -a switch with dir -a forces the utility to drop its default filtering rule.
Reviewing the generated output reveals not only regular documents and folders, but also .config_hidden, .env_sample, alongside relative navigation anchors (. and ..).

Listing all entries including dotfiles with dir -a

For system audits requiring in-depth file attributes, dir -l produces a structured columnar breakdown.
Each row clarifies UNIX permissions, ownership credentials, byte counts, and timestamp markers, clearly identifying folders via a preceding d flag and standard files with a hyphen -.

Long-format listing with dir -l

Raw byte figures can quickly become cumbersome to evaluate mentally. Combining the human-readable switch into dir -lh converts raw byte metrics into intuitive scalar units, translating typical folder footprints into representations like 4.0K.

Display human-friendly sizes with dir -lh

Sorting Items by File Footprint and Modification Timestamps

To examine size-oriented sorting behaviors, allocate a 1MB payload file using truncate -s 1M large_file.bin.

Generate a 1MB test file

Introduce an intermediate threshold file sized at 500KB with truncate -s 500K medium_file.bin.

Generate a 500KB test file

Finally, create a lightweight 50K asset for comparison via truncate -s 50K small_file.bin.

Generate a 50KB test file

Executing dir -lS instructs the tool to rank items strictly by file size in descending sequence.
The terminal lists large_file.bin at the very top, followed down the hierarchy by medium_file.bin and small_file.bin.

Sort directory by file size descending using dir -lS

When troubleshooting newly modified files, dir -lt restructures the recordset chronologically based on the latest write activity.
Because small_file.bin was touched most recently, it claims the initial position while earlier records trail downward.

Chronological sorting with dir -lt

Should you need to trace older historical records first, appending the reverse flag via dir -ltr flips the chronological pipeline.
The oldest items are hoisted to the top of the terminal buffer, leaving the freshest modifications at the bottom.

Inverted timestamp ordering with dir -ltr

Contrasting dir with ls and Enabling Color Highlighting

By default, the dir command presents entries in monochrome plain text laid out across multiple columns.
Unlike everyday Ubuntu aliases where color profiles highlight distinct node categories automatically, standard dir operations avoid syntax tinting completely out of the box.

Monochrome output of native dir command

Running ls in typical Ubuntu shell sessions showcases colorized visual cues, immediately highlighting directories, executable binaries, and regular text files differently according to your environment’s profile.
Juxtaposing these two commands underscores their immediate difference in default visual rendering.

Standard color-coded directory listing with ls

If you appreciate the output format of dir but still desire visual cues, enable ANSI highlighting explicitly with dir --color=auto.
Passing this configuration flag brings dynamic coloring to dir, matching the visual legibility familiar from modern ls sessions.

Enabling terminal colors with dir –color=auto

When chaining commands into automated shell scripts, horizontal multi-column output can be tricky to parse. Using dir -1 forces the program to print entries on a strict single-column, one-item-per-line basis.
This linear layout simplifies downstream parsing operations via utilities such as awk, sed, or xargs.

Single-column stream output with dir -1

Once finished with the exercises, clean up the workspace and reset your terminal prompt by issuing cd ~ && rm -rf ~/dir_practice.

Cleaning up the test directory

While ls remains the standard daily driver across the Linux community, understanding the mechanics, sorting flags, and default display characteristics of dir expands your CLI toolkit. Choose whichever utility fits your immediate command-line or shell scripting requirements to keep your terminal workflows sharp and effective.