FactorPad
Build a Better Process

Linux sort Command Summary with Examples

Linux sort allows you to sort lines within text files.
  1. Purpose - Learn what sort is for an how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with sort.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux find, 31 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sort-command.html


An ad-free and cookie-free website.


Examples of the Linux sort Command

Beginner

Learn to sort files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux sort Command Summary with Examples (3:39)

Video Script

The Command and Why You Need It

Our thirty-first word, or command to memorize is sort from our category Files.

sort allows you to sort lines within text files.

Common Linux sort Options
-options description
--help Print help screen
-r, --reverse Reverse the sort results
-o, --output=FILE Output to FILE

Recall from previous videos, we learned how to inspect files, now we'll see how we can use the computer to quickly process information from these files.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The sort command has 31 options, and arguments include the one or several files you'd like to sort.

Like most commands, help is available with double-dash --help. The -r option reverses sort order and -o outputs the sort to a file.

So why is sort an important command? Well, we're often faced with data sets we'd like to see in numeric, alphabetic or date order. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, let's start using the cat command from video 10. For the most part we've been using cat to print files straight to the screen. So let's use it for its other purpose, to concatenate, or combine files, video05.txt and the same file video05.txt and call it video31.txt and then let's just cat it to the screen.

$ cat video05.txt video05.txt > video31.txt $ cat video31.txt This is the first line This is the second line This is the thrid line This is the fourth line This is the fifth line # A file for video 5 on less to demonstrate the -N option This is the first line This is the second line This is the thrid line This is the fourth line This is the fifth line # A file for video 5 on less to demonstrate the -N option

So we duplicated the file right?

Now, let's use sort on video31.txt.

$ clear $ sort video31.txt # A file for video 5 on less to demonstrate the -N option # A file for video 5 on less to demonstrate the -N option This is the fifth line This is the fifth line This is the first line This is the first line This is the fourth line This is the fourth line This is the second line This is the second line This is the thrid line This is the thrid line

Okay, good. Oh look, a misspelling (thrid).

Next, we'll sort it in reverse order with sort -r video31.txt.

$ clear $ sort -r video31.txt This is the thrid line This is the thrid line This is the second line This is the second line This is the fourth line This is the fourth line This is the first line This is the first line This is the fifth line This is the fifth line # A file for video 5 on less to demonstrate the -N option # A file for video 5 on less to demonstrate the -N option

There we go. And last, let's sort it the regular way, but write it to a file called video32.txt and ls to verify.

$ clear $ sort --output='video32.txt' video31.txt $ ls stuff video11.txt video19.txt video21.txt video24.txt video27.txt video32.txt video05.txt video16.txt video20.txt video23.txt video25.txt video31.txt

A Final Tip

Okay now we know how to use sort. And we know the syntax for commands, options and arguments.

One last tip about the sort command. So why did we create a file for video 32? You'll have to tune in to see a command that is often used in conjunction with sort. Ooh, it's a cliffhanger.

Okay, thanks for visiting today. I hope this was a helpful introduction to the sort command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you support what we're doing, why not subscribe to our YouTube Channel? Also, if you don't want to lose this free resource hop on our no-spam email list and follow @factorpad on Twitter, if that's your thing.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sort-command.html


linux sort command
sort linux
linux sort
unix sort
sort unix
linux sort file
man sort
sort alphabetic
sort numeric
sort date linux
sort command syntax
sort command summary
sort command options
sort command tutorial
linux sort examples

A newly-updated free resource. Connect and refer a friend today.