sort — Sort lines of text across all 5 shells
Equivalents in every shell
Worked examples
Sort numerically
Bash
sort -n filePowerShell
Get-Content file | Sort-Object { [int]$_ }Reverse sort
Bash
sort -r filePowerShell
Get-Content file | Sort-Object -Descendingcmd.exe
sort /r fileSort unique lines
Bash
sort -u filePowerShell
Get-Content file | Sort-Object -UniqueGotchas
- GNU `sort -n` sorts numerically; PowerShell `Sort-Object` sorts as strings unless you cast (`{ [int]$_ }`).
- cmd `sort` is alphabetical only and case-insensitive by default — pass `/c` to make it case-sensitive.
- Large file sorts: GNU sort uses temporary files on disk; `Sort-Object` is fully in-memory and can OOM on huge inputs.
WSL & PowerShell Core notes
pwsh`Sort-Object` is fully in-memory on every pwsh platform — sorting multi-GB log lines blows the .NET heap, while GNU `sort` happily spills to `$TMPDIR`. For very large inputs on Linux/macOS pwsh hosts, shell out to the system binary (`Get-Content huge.log | sort`) rather than relying on the cmdlet. Locale also matters: GNU `sort` honours `LC_COLLATE` (so `a A b B` interleaves under `en_US.UTF-8`) while `Sort-Object` is ordinal by default — pass `-Culture invariant` for predictable cross-platform results.
WSLSorting a `/mnt/c/...` file from WSL works but the initial read is bottlenecked by DrvFs / 9P bandwidth (~150–250 MB/s typical, vs 1 GB/s+ for native ext4 inside `~/`). GNU `sort`'s external-memory spill files land in `$TMPDIR` which defaults to `/tmp` on the WSL ext4 — that side stays fast. For one-off cleanup of a huge Windows-side CSV, copy to `~/` first (`cp /mnt/c/data/huge.csv ~/`), sort there, then move the result back; you pay the DrvFs cost once instead of once per external-sort pass.
Common tasks using sort
- Dedupe lines while preserving order
Remove duplicate lines from input but KEEP the first occurrence in its original position — for unique-but-sorted-by-recency lists, `$PATH` cleanup, and history dedup.
- Find processes using the most CPU
List the top CPU-consuming processes — useful for performance triage, runaway-script detection, and capacity planning.
- Find the largest files in a directory
Identify the top N largest files under a directory tree, sorted by size.
- Remove duplicate lines from a file
Strip duplicate lines from a file, optionally preserving original order.
- Shuffle lines in a file
Randomize the order of lines in a file — useful for sampling, A/B-bucketing, and test fixtures.
- Sort a file by a specific column
Sort lines of a file by the value in a chosen column (numeric or alphabetic).
- Use process substitution
Treat a command's output as a file argument to another command — e.g. `diff <(sort a) <(sort b)` — without manual tempfile management.