Sorting Lines: sort
The sort command rearranges lines of text into order. By default it sorts alphabetically (A before B, lowercase after uppercase).
sort crew.txt
Chen
Okafor
Tanaka
Voss
The original file is unchanged: sort prints the sorted result to stdout. You can
save it with > sorted.txt if needed.
Reverse order with -r
sort -r crew.txt
Voss
Tanaka
Okafor
Chen
Sorting is case-sensitive by default
Uppercase letters sort before lowercase. If you want true alphabetical order regardless
of case, add -f (fold case):
sort -f mixed.txt
Sort in a pipeline
sort is a perfect pipeline citizen: it reads stdin if no file is given:
grep "error" log.txt | sort
This filters errors first, then sorts them alphabetically.
Mission: Cargo Priority List
A supply shuttle just docked, and the cargo manifest supplies.txt is a mess. The quartermaster's rule: unload in reverse alphabetical order. In a new cargo-bay folder, save the whole list in that order as unload-order.txt, and the item the crew unloads first, alone, as first.txt.
Expected result
cargo-bay/unload-order.txt holds every item from last to first in the alphabet, and cargo-bay/first.txt holds only the one at its top.