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 bay manifest (supplies.txt) is a mess. The quartermaster wants a clean unloading order for the crew.
- Sort
supplies.txtin reverse alphabetical order and display the result on screen. - Save that reverse-sorted list into a new file called
unload-order.txt. - Create a folder called
cargo-bayand moveunload-order.txtinto it. - Show just the first item on the list. That is what the crew unloads first.
Expected result
cargo-bay/unload-order.txt holds the six items from water filters down to ammo crates.