/ factorpad.com / tech / linux-essentials / sort-command.html
An ad-free and cookie-free website.
Beginner
Learn to sort files at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux sort Command Summary with Examples (3:39)
Our thirty-first word, or command to memorize is
sort
from our category
Files.
sort
allows you to sort lines within
text files.
-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.
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.
So we duplicated the file right?
Now, let's use sort
on
video31.txt
.
Okay, good. Oh look, a misspelling (thrid).
Next, we'll sort it in reverse order with
sort -r video31.txt
.
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.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
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.
find
command, hit Back.uniq
command, click
Next./ factorpad.com / tech / linux-essentials / sort-command.html
A newly-updated free resource. Connect and refer a friend today.