Sort text files in place with Linux

The command sort is useful to order text lines. Sort orders alphabetically by default but there are options that enable you to order in other ways like in reverse alphabetical order or by string numerical value.

In this example I’ll demonstrate how to order a text file in place, that is, take a file and order all its lines alphabetically without making a copy first.

Example:

I got a file named namelist.txt which contains an unordered list of names.

This file can be easily ordered in place with the following shell command.

The result is the file with ordered text lines.

Use cases:

Sort can help you keep text files ordered according to your requirements. I use it to order an alias dotfile after I add a new command at the bottom of the list. However, sort isn't useful for ordering source code, where arbitrary order is required.

Software Developer and Computer Engineering student. Writes about technology and poetry. Creates glitch art. victoroalvarez.com/tree

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store