Skip to content

What is sort Linux command?

The Linux sort command is a handy tool for arranging lines of text in a file based on alphabetical, numerical, or positional order. It also allows for custom sorting options and can be used in combination with other commands to manipulate and organize text data efficiently.

sort Syntax:

Terminal window
sort [option] [file]

sort Options:

OptionDescription
-rReverse the result of comparison
-nCompare according to numerical value
-fIgnore case while sorting
-kSort by key field
-uOutput only unique lines
-tSpecify the field delimiter

Parameters:

ParameterDescription
fileThe file to be sorted

sort Command Usage Examples:

Sort a text file in ascending order

Terminal window
sort file.txt

This command will sort the lines in the file.txt in ascending order.

Sort a text file in descending order

Terminal window
sort -r file.txt

This command will sort the lines in the file.txt in descending order.

Sort a text file based on a specific column

Terminal window
sort -k2 file.txt

This command will sort the lines in the file.txt based on the second column.

Sort a numeric text file

Terminal window
sort -n numbers.txt

This command will sort the lines in numbers.txt numerically.

Sort a text file ignoring case

Terminal window
sort -f file.txt

This command will sort the lines in the file.txt ignoring the case.

How do I use sort in Linux?

To use the sort command in bash, execute the following command:

Terminal window
sort file.txt

How can I sort numerically with sort in Linux?

To sort numerically using the sort command in bash, use the following command:

Terminal window
sort -n numbers.txt

How do I sort in reverse order with sort in Linux?

To sort in reverse order using the sort command in bash, utilize the following command:

Terminal window
sort -r file.txt

How can I ignore leading blanks when sorting with sort in Linux?

To ignore leading blanks when sorting using the sort command in bash, employ the following command:

Terminal window
sort -b file.txt

How do I sort in reverse numerical order with sort in Linux?

To sort in reverse numerical order using the sort command in bash, use the following command:

Terminal window
sort -nr numbers.txt

How can I sort based on a specific column with sort in Linux?

To sort based on a specific column using the sort command in bash, use the following command:

Terminal window
sort -k2 data.txt

How do I perform a case-insensitive sort with sort in Linux?

To perform a case-insensitive sort using the sort command in bash, execute the following command:

Terminal window
sort -f names.txt

How can I sort files in a directory with sort in Linux?

To sort files in a directory using the sort command in bash, you can combine it with other commands like “ls”:

Terminal window
ls | sort

Applications of the sort command

  • Sorting lines of text files
  • Merging and sorting multiple files
  • Removing duplicate lines from a file
  • Sorting data in a specific order (numerically or alphabetically)
  • Preparing data for further processing or analysis