Categories: Text Tools

Linux sort command (sort data) [Basic Guide]

The sort command in Linux sorts the lines of a file. Once ordered, a file can be processed by other commands, such as removing duplicate occurrences with the uniq command.

Your options are:

  • -b: Ignore spaces at the beginning of the line;
  • -d: Put lines in alphabetical order and ignore punctuation;
  • -f: Ignore the difference between uppercase and lowercase letters;
  • -I: Ignore control characters;
  • -h: sort in human format
  • -m: Merge two or more files into an ordered output file; -M: Treat the first three letters of
  • the lines as month (e.g. JAN); -n: Sort by numbers at the beginning of the lines;
  • -r: Sort by the numbers at the beginning of the lines;
  • -r: Sort in reverse order;
  • -u: If the line is duplicated, it shows only the first line;
  • -o: Send the command output to the file.

As an example, let’s sort an email archive:

$ sort emails
alessandra@empresa.com

arthur@empresa.com
carla@empresa.com
demetrios@empresa.com
flavia@empresa.com
paulo@empresa.com
root@empresa.com
uira@empresa.com

Care must be taken when ordering numbers. See the example of the unordered file below:

$ cat numbers 330 40 4 3 31 19 1

When using sort without any parameters, it sorts first using the first byte, then the second:

$ sort numbers 1 19 3 31 330 4 40

To sort using numbers as characters, you can use the -n option, or the -h option to sort in human format:

$ sort -n numbers 1 3 4 19 31 40 330

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.