Linux Commands | File Management | sort Sorting
sort Command
The sort command is used to sort command output or file contents.
Syntax
sort [-options] [-o output file name] file to sort [file to merge]
Options
-b: Ignores leading blanks.-o: Specifies the output file.-r: Displays in reverse order.-f: Ignores case.-t: Specifies a field separator.-u: Removes duplicate lines.-m: Merges sorted files.-n: Compares as numbers.
sort Command Examples
Example: Sort file contents
If there is a file containing only numbers as shown below:
cat sample.txt
3
1
5
9
7
You can sort the output with the sort [file name] command.
sort sample.txt
1
3
5
7
9
Example: Sort when there are multiple columns
If there are two columns as shown below:
cat sample.txt
3 f
1 b
5 a
9 c
7 e
Use the sort +1 [file name] command to sort by the second column.
sort +1 sample.txt
5 a
1 b
9 c
7 e
3 f