Fundamentals 6 min read

Master Linux Sorting: 14 Real-World Examples to Master the sort Command

This tutorial explains the Linux sort utility, its behavior, and walks through fourteen practical examples—from creating files and basic sorting to reverse ordering, column‑based sorting, merging, and deduplication—helping readers confidently manipulate text data on the command line.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Sorting: 14 Real-World Examples to Master the sort Command

What is sort

Sort is a Linux program that orders the contents of one or more text files. It uses spaces as field delimiters and outputs the sorted result without modifying the original files unless redirected.

Goal

The article demonstrates 14 practical examples to deepen understanding of the sort command.

Examples

Create target file – create tecmint.txt in /home/$USER/Desktop/tecmint.

View file – display its contents.

Basic sorting – sort the file and output to screen.

Sort and output to file – sort into sorted.txt and view with cat.

Reverse order – use -r to sort descending and save to reversesorted.txt.

Create new file from ls – generate lsl.txt containing the output of ls -l.

Sort by column (numeric) – sort lsl.txt by the second column using -n.

Sort by column (name) – sort lsl.txt by the ninth column (file name).

Redirect output – pipe other command output into sort.

Sort and remove duplicates – sort tecmint.txt with -u to eliminate repeated lines.

Output content – create lsla.txt with ls -lA output.

Sort two files together – sort the contents of lsla.txt and lsl.txt.

Merge and deduplicate – sort and merge the two files, removing duplicate lines.

Multi‑column sorting – sort by columns 2,5 (numeric) and 9 (non‑numeric) of the ls -l output.

Sorting priority follows: numeric‑starting lines first, then lowercase letters, and finally dictionary order. The -n option treats fields as numbers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Bashcommand-linesort
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.