sort

sort linux command cheatsheet by Thamizhiniyan C S

Introduction

sort command, as the name suggests sorts the lines alphabetically and numerically, automatically.


Syntax

sort [OPTION]... [FILE]...


Important Flags

FlagsDescription

-r

Sorts in reverse order

-c

This flag is used to check whether the file is already sorted or not(If not, it will list, where the disorder started)

-u

To sort and removes duplicate lines(does work same as stdin redirected into uniq)

-o save.txt

To save into a output file

-t

Define Field Separator

-k

Reorders data in a specific field

-n

Sorts the file numerically

-M

Sorts by month names


Examples

CommandDescription

Order the lines in lexicographical order

Order the lines in reverse lexicographical order (i.e. Z-A instead of A-Z)

Sort the lines in ascending order based on numbers

Sort the lines in descending order based on numbers

Sorts lines of text based on the second field (using a numerical comparison) in reverse order, considering tab characters as the field separator

Sort lines of text based on the second field in ascending numerical order, considering tab characters as the field separator

Sort lines of text based on the second field in descending numerical order, considering '|' as the field separator.

Last updated