# Linux commands: sort

> Learn how the Linux sort command orders the lines of a file or piped input, with -r to reverse, -n for numeric order, and -u to drop duplicate lines.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-10-03 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-sort/

Suppose you have a text file which contains the names of dogs:

![Terminal showing dogs.txt file with unsorted list of dog names: Roger, Syd, Vanille, Luna, Ivica, Tina](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_07.56.28.png)

This list is unordered.

The `sort` command helps us sorting them by name:

![Terminal showing sort dogs.txt command with output alphabetically sorted: Ivica, Luna, Roger, Syd, Tina, Vanille](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_07.57.08.png)

Use the `r` option to reverse the order:

![Terminal showing sort -r dogs.txt command with output in reverse alphabetical order: Vanille, Tina, Syd, Roger, Luna, Ivica](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_07.57.28.png)

Sorting by default is case sensitive, and alphabetic. Use the `--ignore-case` option to sort case insensitive, and the `-n` option to sort using a numeric order.

If the file contains duplicate lines:

![Nano text editor showing dogs.txt file with duplicate entries: Roger, Syd, Vanille, Luna, Ivica, Tina, Roger, Syd](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_07.59.03.png)

You can use the `-u` option to remove them:

![Terminal showing sort -u dogs.txt command with duplicates removed: Ivica, Luna, Roger, Syd, Tina, Vanille](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_07.59.16.png)

`sort` does not just works on files, as many UNIX commands it also works with pipes, so you can use on the output of another command, for example you can order the files returned by `ls` with:

```bash
ls | sort
```

`sort` is very powerful and has lots more options, which you can explore calling `man sort`.

![Terminal showing man sort manual page with command description, synopsis, and various options like -c, -m, -o, -S, -T, -u](https://flaviocopes.com/images/linux-command-sort/Screen_Shot_2020-09-07_at_08.01.27.png)

The `sort` command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

If you have a list in your clipboard and want to sort or dedupe it without the terminal, I built a free [line sorter](https://flaviocopes.com/tools/line-sorter/) that does both in the browser.
