Menu
Coddy logo textTech

Sort Command

Part of the Fundamentals section of Coddy's Terminal journey — lesson 32 of 82.

The sort command arranges the lines of a file in alphabetical or numerical order.

We have fruits.txt:

banana
apple
cherry
mango

Simply type sort followed by the filename:

sort fruits.txt

The output will be:

apple
banana
cherry
mango

Useful flags:

-r — sort in reverse (descending) order:

sort -r fruits.txt

-n — sort numerically instead of alphabetically:

sort -n numbers.txt

Without -n, numbers are sorted as text, so 10 would come before 2. With -n, they sort correctly by value.

-u — sort and remove duplicate lines at the same time:

sort -u fruits.txt

-k — sort by a specific column (useful for files with multiple columns):

sort -k2 data.txt   # sort by the second column

Note: sort does not modify the original file — it only prints the sorted output to the screen.

challenge icon

Challenge

Beginner

Use the sort command on fruits.txt:

  1. Sort the file in alphabetical order using sort
  2. Sort the file in reverse alphabetical order using sort -r

Hint: Run both commands one after the other. The original file is not changed — sort only prints the result.

Cheat sheet

The sort command arranges the lines of a file in alphabetical or numerical order:

sort fruits.txt

Useful flags:

-r — sort in reverse (descending) order:

sort -r fruits.txt

-n — sort numerically instead of alphabetically:

sort -n numbers.txt

-u — sort and remove duplicate lines:

sort -u fruits.txt

-k — sort by a specific column:

sort -k2 data.txt   # sort by the second column

Note: sort does not modify the original file — it only prints the sorted output to the screen.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals