• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

sort Command Examples in Linux

by admin

The sort command is used to sort the lines of a text file in ascending or descending order, or sort as per a specified key. The sort command arranges the lines in a file. Common sort command options are provided in the table.

Option Description
-k{column numbers} Specify field values. For example, -k2 indicates the second field.
-n Compare and sort lines based on the string numerical value.
-r Sort fields in descending order. By default, the fields are sorted in ascending order.
-t{delimiter} Separate one field from another.

Syntax

The syntax of the sort command is:

# sort [options] {file names}

sort Command Examples

1. Sort a file in ascending order:

# sort path/to/file

2. Sort a file in descending order:

# sort --reverse path/to/file

3. Sort a file in case-insensitive way:

# sort --ignore-case path/to/file

4. Sort a file using numeric rather than alphabetic order:

# sort --numeric-sort path/to/file

5. Sort `/etc/passwd` by the 3rd field of each line numerically, using “:” as a field separator:

# sort --field-separator=: --key=3n /etc/passwd

6. Sort a file preserving only unique lines:

# sort --unique path/to/file

7. Sort a file, printing the output to the specified output file (can be used to sort a file in-place):

# sort --output=path/to/file path/to/file

8. Sort numbers with exponents:

# sort --general-numeric-sort path/to/file

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 6,7 : How to increase system log message verbosity (rsyslogd)
  2. CentOS / RHEL : How to configure iptable rules to allow FTP ports 20/21
  3. Linux OS Service ‘cups’
  4. fallocate Command Examples in Linux
  5. How to enable SFTP Logging without chroot in CentOS/RHEL
  6. fdisk: command not found
  7. isosize: command not found
  8. NFSv4 Client Shows “nobody” As Owner And Group For Mount Point (CentOS/RHEL)
  9. CentOS / RHEL : How to adjust the telnet timeout (and how to disable it)
  10. asterisk: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright