Ad space (banner)
🖥️Linux Lessons
Lesson 23 / 61

Sorting a File's Lines with sort

sort is for when "I want to sort a file's lines alphabetically or numerically." sort rearranges each line of the given file according to a specified order and displays the result.

Use it in the form sort filename. Combine it with -n to sort numerically, or -r to reverse the order.

Running sort fruits.txt rearranges each line of fruits.txt into character-code order (alphabetical, and so on) and displays it.

A common early mistake is forgetting -n when sorting lines that contain numbers, so "10" ends up sorted before "2" — treated as plain text order instead of numeric order.

In real work, this is used constantly as a data-preparation step — sorting a huge log by date and time, sorting CSV data by a specific column, and more.

Linux
Try it (type this into the pseudo terminal)
sort fruits.txt

🧑‍💻 You can type this command into the Linux pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)