FactorPad
Faster Learning Tutorials

Linux sort command summary with examples

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.
Paul Alan Davis, CFA, October 17, 2016
Updated: July 23, 2017
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / sort command


The Linux sort command

Beginner

Learn to sort files at the Linux command line.

Video Tutorial

Linux sort command summary with examples (3:39)

Videos can also be accessed from the YouTube Playlist.

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.

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?

  • To see the Outline of all 100 tutorials, click Outline.
  • To go back to the find command, hit Back.
  • To go to the uniq command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / sort command



 
 
Keywords:
Linux sort command
sort linux
sort alphabetic
sort numeric
sort date
sort text file
reverse sort
sort to file
combine file sort