Introduction
The commandsortis one of the most used utilities in the Linux command line. It allows you to order text lines quickly and efficiently, whether they come from a file, from the output of another program or from the standard input. Its operation is based on comparing the characters of each line according to the set of options indicated to it, which makes it an indispensable tool for the processing of log, CSV files, user lists and any other data structured in line form.
Although at first sight it seems simple,sortoffers a lot of options that allow you to adapt your behavior to specific needs: numerical, reverse, duplicate removal, specific column management, depending on the language or even using a custom buffer size. Knowing these options helps to write more efficient scripts and avoid unnecessary post-processing.
Basic syntax
The simplest way to usesortis:
delete [options] file
If no file is specified, the command reads from the standard input, which allows it to be combined with pipes (|) and readdresses. By default,sortorder in an ascending way using the lexicographic order based on the ASCII or UTF-8 code, according to the environment configuration.
A quick example:
sort name.txt
It alphabetically orders the content ofnombres.txtand shows the result on the screen.
Most commonly used options
-n: orders numerically, treating each line as a number. Ideal for age lists, scores or IP addresses when you want 10 to stay before 2.-r: reverse the exit order, getting a down list. It can be combined with-nto get the highest values first.-u: removes duplicate lines after ordering. It is useful to obtain a unique set of values, for example, a list of users without repetitions.-kfield: specifies the column or field to be ordered on. You can indicate a range like-k2,4to consider from the second to the fourth column.-tdelimiter: defines the character that separates the fields. By default, any blank space or tabulation acts as a delimiter, but with-tYou can use a coma, point and coma or any other symbol.-M: orders according to the names of the short months (Jan, Feb, Mar...), useful for logs containing dates in textual format.
Practical examples
Order a simple text file:
sort lista.txt
Numerically order and remove duplicates:
sort -nu numeros.txt
Order according to the second field of a comma-separated CSV:
sort -t, -k2 data. csv
Order short months:
sort -M meses.txt
Order by columns
When the file contains several columns, the option-kallows to specify exactly which field to use as a command key. The syntax is-kstart[type][,end[type]]. For example,-k2,2orders using only the second column, while-k2,3considers the second and third as a composite key. If the type is omitted, the lexicographic order is assumed; addn (-k2n) forces a numerical comparison for that field.
Example with a log file where the ninth field is the size of the answer:
sort -k9n acceso.log
This orders the entries from the smallest response to the largest. If you need to reverse the order, just add-r:
sort -k9nr acceso.log
Combinesortwith other commands
To get the ten most featured IP addresses in an access log:
awk '{print $1} 'access.log | sort | uniq -c | sort -nr | head -10
In this chain, the firstsortgroups the same IP,uniq -ctells the events and one secondsort -nrorder in a numerical and descending way to show the most frequent first.
To order the result ofls -lby file size:
ls -l | sort -k5nr
Here the fifth field corresponds to the size in bytes;-k5nrindicates numerical and descending order.
Tips and tricks
- Always use
LC_ALL=Cif you need a byte-based order and want to avoid surprises with locations that can change the order of special characters. - For very large files, control the used memory with the option
-S(e.g.,-S 50%to use up to half of the available RAM) and work directory with-T. - If you just need to know if a file is ordered,
sort -c archivoreturns output code 0 when correctly ordered and 1 if not, allowing for use in scripts tests. - Combine
sortwithgreporawkto filter before ordering and reducing the workload. - In environments where the locale affects the order (e.g. Spanish with
es_ES.UTF-8), force order «C» withLC_COLLATE=C sort archivo.
Conclusion
The commandsortis an essential piece of the arsenal of any system manager or developer working in Linux. Its flexibility, thanks to options such as-n, -r, -kand-t, allows it to be adapted to almost any text line management scenario. Dominating its use not only saves time, but also opens the door to powerful combinations with other shell utilities, improving the efficiency of scripts and data processing on a daily basis.
Whether you need to organize logs, prepare reports, debug data or simply keep your files sorted,sortoffers a fast, reliable and highly configurable solution. Practice with the examples presented and explore the less used options will allow you to make the most of this classic tool.


