TL;DR
To effectively use the sort command in Linux, you can use these methods:
- Sorting Lines of Text:
sort filename.txtto sort the lines in ascending order, orsort -r filename.txtto sort in descending order. - Sorting Numerical Data:
sort -n filename.txtto sort the numerical values in ascending order, orsort -n -r filename.txtto sort in descending order. - Sorting Fields within a Line:
sort -k 2 filename.txtto sort by the second field, orsort -k 2 -r filename.txtto sort in descending order. - Sorting Files:
sort filename.txtto sort the contents of a single file, orsort file1.txt file2.txt file3.txtto 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:
- Open your Terminal.

- Navigate to the directory containing the file you want to sort.

- Use the following command to sort the lines in ascending order:
<strong>sort filename.txt</strong> - This command will alphabetically sort the lines in the file.

- To sort in descending order, add the
-rflag:
<strong>sort -r filename.txt</strong> - This command will sort the lines from Z to A in reverse 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:
- Access your Terminal window and navigate to the directory containing the file with numerical data.
- Use the following command to sort the numerical values in ascending order:
<strong>sort -n filename.txt</strong> - This command will sort the numbers in ascending order, from the smallest to the largest.

- To sort in descending order, combine the
-nand-rflags:
<strong>sort -n -r filename.txt.</strong> - This command will sort the numbers in reverse order, from the largest to the smallest.

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:
- Launch your command prompt.
- Use the
-koption 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> - This command will sort the lines based on the values in the specified 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:
- Enter the command window and navigate to the directory.
- To sort multiple files, run the following command:
<strong>sort file1.txt file2.txt file3.txt</strong> - This command will sort the lines within each file individually.

- Redirect the sorted output to a new file using the > symbol: