Skip to content

Linux commands: sort

A quick guide to the `sort` command, used to sort records/lines of text

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

This list is unordered.

The sort command helps us sorting them by name:

Use the r option to reverse the order:

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:

You can use the -u option to remove them:

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:

ls | sort

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

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


→ Get my Linux Command Line Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list