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.txtThe output will be:
apple
banana
cherry
mangoUseful flags:
-r — sort in reverse (descending) order:
sort -r fruits.txt-n — sort numerically instead of alphabetically:
sort -n numbers.txtWithout -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 columnNote: sort does not modify the original file — it only prints the sorted output to the screen.
Challenge
BeginnerUse the sort command on fruits.txt:
- Sort the file in alphabetical order using
sort - Sort the file in reverse alphabetical order using
sort -r
Hint: Run both commands one after the other. The original file is not changed —
sortonly prints the result.
Cheat sheet
The sort command arranges the lines of a file in alphabetical or numerical order:
sort fruits.txtUseful 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 columnNote: sort does not modify the original file — it only prints the sorted output to the screen.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline