In Unix, how do I use the sort command?
The sort command sorts the contents of a file, in numeric
or alphabetic order, and prints the results to standard output
(usually the terminal screen). The original file is unaffected.
For example, if filename is a file containing a list of
words, at the Unix prompt, you would enter:
This will print the list to the screen in alphabetical order (numbers first, then capital words, then lowercase words). To eliminate any duplicate entries in the list, use:
sort -u filenameTo sort case-insensitively, use:
sort -f filenameTo sort case-insensitively and in reverse order, use:
sort -fr filenameAs with many Unix commands, you can redirect the output to a new file:
sort filename > newfilename
The output of the sort command will then be stored in a
file named newfilename in the current directory.
You can also pipe the output of the sort command into
other Unix commands, for example:
This sends the output through the more command for easy
reading.
To print only the first word of each line, enter:
sort filename | cut -f1 -d" "
The cut command selects the field specified by the
-f option and distinguishes fields by the
delimiter character specified by the -d
option, a space in this example.
To view the online manual for the sort command, at the
Unix prompt, enter:
man sort
At Indiana University, to get support for personal or departmental Linux or Unix systems, see At IU, how do I get support for Linux or Unix?
Also see:
- What do some common Unix file extensions mean?
- In Unix, what is the man command, and how do I use it to read manual pages?
- Introduction to Unix commands
Last modified on August 22, 2008.






