1. Home
  2. Computing & Technology
  3. Linux
Linux / Unix Command: sort
Command Library
NAME
sort (sort lines of text files)
SYNOPSIS
sort [-cmus] [-t separator] [-o output-file] [-T tempdir] [-bdfiMnr] [+POS1 [-POS2]] [-k POS1[,POS2]] [file...]
sort {--help,--version}
DESCRIPTION
sort sorts, merges, or compares all the lines from the given files, or the standard input if no files are given.

sort has three modes of operation: sort (the default), merge, and check if the file is sorted.

OPTIONS
-c
Check whether the given files are already sorted: if they are not all sorted, print an error message
and exit with a status of 1.

-m
Merge the given files by sorting them as a group. Each input file should already be individually sorted. It always works to sort instead of merge; merging is provided because it is faster, in the case where it works.

-b
Ignore leading blanks when finding sort keys in each line.

-d
Sort in `phone directory' order: ignore all characters except letters, digits and blanks when sorting.

-f
Fold lower case characters into the equivalent upper case characters when sorting so that, for EXAMPLE, 'b' is sorted the same way 'B' is.

-i
Ignore characters outside the ASCII range 040-0176 octal (inclusive) when sorting.

-M
An initial string, consisting of any amount of white space, followed by three letters abbreviating a month name, is folded to UPPER case and compared in the order 'JAN' < 'FEB' < ... < 'DEC.' Invalid names compare low to valid names.

-n
Compare according to arithmetic value an initial numeric string consisting of optional white space, an optional - sign, and zero or more digits, optionally followed by a decimal point and zero or more digits.

-r
Reverse the result of comparison, so that lines with greater key values appear earlier in the output instead of later.

Other options are:

-o output-file
Write output to output-file instead of to the standard output. If output-file is one of the input files, sort copies it to a temporary file before sorting and writing the output to output-file.

-t separator
Use character separator as the field separator when finding the sort keys in each line. By default, fields are separated by the empty string between a non-whitespace character and a whitespace character. That is to say, given the input line 'foobar', sort breaks it into fields ' foo' and 'bar'. The field separator is not considered to be part of either the field preceding or the field following it.

-u
For the default case or the -m option, only output the first of a sequence of lines that compare equal. For the -c option, check that no pair of consecutive lines compares equal.

--help
Print a usage message on standard output and exit successfully.

--version
Print version information on standard output then exit successfully.

(see man page for more options)

EXAMPLE

% sort -f name_list.txt

arranges the lines in file name_list.txt according to the ascii character table. The option -f tells sort to ignore the upper and lower character case.

Important: Use the man command (% man) to see how a command is used on your particular computer.

>> Linux Command Library Index

>> Also see Shell Command Library

Explore Linux

More from About.com

  1. Home
  2. Computing & Technology
  3. Linux

©2008 About.com, a part of The New York Times Company.

All rights reserved.