Published: July 22, 2026
Read: 8 min
In: Technology & Development

Written by

Reviewed by

Last updated: July 2, 2023

Expert verified

SVG Image

TL;DR

To effectively use the sort command in Linux, you can use these methods:

  1. Sorting Lines of Text: sort filename.txt to sort the lines in ascending order, or sort -r filename.txt to sort in descending order.
  2. Sorting Numerical Data: sort -n filename.txt to sort the numerical values in ascending order, or sort -n -r filename.txt to sort in descending order.
  3. Sorting Fields within a Line: sort -k 2 filename.txt to sort by the second field, or sort -k 2 -r filename.txt to sort in descending order.
  4. Sorting Files: sort filename.txt to sort the contents of a single file, or sort file1.txt file2.txt file3.txt to sort multiple files.

Be mindful of unexpected sorting results due to leading spaces or hidden characters by using the -b flag. Ensure accurate numeric sorting by including the -n flag. Consistently sort fields within a line by specifying the fields correctly using the -k option. Understand locale-based sorting with the -g flag and validate the output against your expectations.

Read the guide below to learn different methods to use the sort command in Linux and common errors that can occur when using the command.

The sort command is a game-changer when it comes to efficiently sorting and ordering data. Whether working with massive log files, analyzing complex datasets, or simply needing to alphabetize a list, the sort command is your go-to solution. With its ability to handle diverse sorting requirements, such as numeric sorting, field-based sorting, and locale-specific collation, the sort command empowers you to arrange your data effortlessly in a matter of seconds. In this comprehensive guide, I’ll explore different methods to utilize the sort command in Linux and common errors to avoid when using the sort command.

How to Use the Sort Command in Linux

To use the sort command in Linux, effortlessly sort lines of text, numerical data, fields within a line, and entire files. Simplify your sorting tasks and efficiently organize your data with ease.

1. Sorting Lines of Text

Sorting lines of text is one of the most common use cases for the sort command. Whether you’re organizing a list of names, sorting log entries, or alphabetizing text, the sort command simplifies the process. Follow these steps to sort lines in Linux:

  1. Open your Terminal.
opening terminal
  1. Navigate to the directory containing the file you want to sort.
navigating to directory 1
  1. Use the following command to sort the lines in ascending order: 

<strong>sort filename.txt</strong>
  1. This command will alphabetically sort the lines in the file.
sorting line of file in ascending order
  1. To sort in descending order, add the -r flag:

<strong>sort -r filename.txt</strong>
  1. This command will sort the lines from Z to A in reverse order.
sorting lines of file in descending order

2. Sorting Numerical Data

Sorting numerical data accurately is crucial for various tasks such as financial analysis or statistical computations. The sort command comes to the rescue, allowing you to sort numerical values effortlessly. Follow these steps to sort numerical data using the sort command:

  1. Access your Terminal window and navigate to the directory containing the file with numerical data.
  2. Use the following command to sort the numerical values in ascending order: 

<strong>sort -n filename.txt</strong>
  1. This command will sort the numbers in ascending order, from the smallest to the largest.
sorting numerical values in ascending order
  1. To sort in descending order, combine the -n and -r flags: 

<strong>sort -n -r filename.txt.</strong>
  1. This command will sort the numbers in reverse order, from the largest to the smallest.
sorting numerical values in descending order

3. Sorting Fields within a Line

When working with structured data containing fields, such as CSV files, sorting based on specific fields becomes necessary. The sort command allows you to sort data within lines using the -k option. Follow these steps to sort fields within a line using the sort command:

  1. Launch your command prompt.
  2. Use the -k option to specify the field you want to sort by. For example, to sort by the second field, use: 

<strong>sort -k 2 filename.txt</strong>
  1. This command will sort the lines based on the values in the specified field.
sorting content of file base on the second field

4. Sorting Files

The sort command is not limited to sorting individual lines or fields. It can sort entire files, making it a handy tool for organizing large datasets. You can seamlessly arrange their contents in the desired order by executing the sort command on one or multiple files. Follow these steps to sort files using the sort command:

  1. Enter the command window and navigate to the directory.
  2. To sort multiple files, run the following command: 

<strong>sort file1.txt file2.txt file3.txt</strong>
  1. This command will sort the lines within each file individually.
sorting multiple files at a time
  1. Redirect the sorted output to a new file using the > symbol: