15+ Tips to PROPERLY sort files in Linux [Cheat Sheet]


CheatSheet

Author: Omer Cakmak
Reviewer: Deepak Prasad

When you look at the terminal history of Linux users, you can see that they have sorted the files multiple times. So much so that sometimes sorting files has become a habit. Sorting files is also a need for repetitive operations like bash scripts.

Now let's exemplify the methods that will increase your knowledge about file sorting in Linux.

 

How to use -k with sort command in Linux?

The -k option in the sort command specifies the field or fields to be used for sorting the input. The general syntax for using -k is:

sort -k <start>[,<end>][<options>]

Where:

  • <start> is the starting field number or character position for the sorting key (required).
  • <end> is the ending field number or character position for the sorting key (optional).
  • <options> are additional options that can be used to modify the sorting behavior (optional).

The <start> and <end> arguments can be specified in a few different ways, depending on whether you want to sort by field number or character position:

  • To sort by field number, specify a positive integer for <start> and optionally <end>. For example, -k 2 would sort by the second field, and -k 2,4 would sort by fields 2 through 4.
  • To sort by character position, specify a dot-separated pair of integers for <start> and optionally <end>. For example, -k 2.3 would sort by the third character of the second field, and -k 2.3,4.5 would sort by the characters between positions 3 and 5 of fields 2 through 4.

Additionally, you can use some options to modify the sorting behavior:

  • -n sorts numerically rather than lexicographically.
  • -r sorts in reverse order.
  • -f sorts case-insensitively.

Here are some examples of how to use -k with sort:

To sort a file called file.txt by the second field:

sort -k 2 file.txt

To sort a file called file.txt by the third character of the second field:

sort -k 2.3 file.txt

To sort a file called file.txt by the third through fifth characters of the second field, numerically:

sort -k 2.3,2.5n file.txt

To sort the output of an ls command by file size, with the largest files at the top:

ls -l | sort -k 5nr

 

1. Sort by name

The simplest way to sort files in Linux is to sort them alphabetically by name. This is done using the ls command with the -l option and piping it to the sort command. Here's an example:

~]# ls -l | sort -k9
total 2860
-rw-------. 1 root root    1295 Sep 18 11:09 anaconda-ks.cfg
drwxr-xr-x  2 root root       6 Jan 15 13:40 Desktop
drwxr-xr-x  2 root root       6 Jan  6 23:07 Documents
drwxr-xr-x  2 root root      21 Jan 21 16:12 Downloads
-rw-r--r--  1 root root     185 Jan 22 17:04 example.log
-rw-r--r--  1 root root    6498 Jan 11  2020 getpass_ak.py
-rw-r--r--  1 root root    2338 Oct  5 23:54 getpass_ak.rar
...

This sorts the files based on their names in ascending order.

 

2. Sort by size

To sort files by size, you can use the ls command with the -l option and pipe it to the sort command using the -n (numeric) option. Here's an example:

~]# ls -l | sort -nk5
total 2860
-rw-r--r--. 1 root root     114 Sep 18 12:53 test1.txt
drwxr-xr-x  3 root root     138 Feb 14 13:44 goexamples
-rw-r--r--  1 root root     185 Jan 22 17:04 example.log
-rw-r--r--. 1 root root     585 Sep 18 12:41 test.txt
-rw-r--r--  1 root root     801 Dec 12 18:03 logging.conf
-rw-------. 1 root root    1295 Sep 18 11:09 anaconda-ks.cfg
-rw-r--r--  1 root root    1608 Jan  7 15:33 manage_service.sh
-rw-r--r--  1 root root    2338 Oct  5 23:54 getpass_ak.rar
drwxr-xr-x  2 root root    4096 Jan 17 10:55 pprof
drwxr-xr-x  4 root root    4096 Jan  7 16:01 repo
-rw-r--r--  1 root root    6498 Jan 11  2020 getpass_ak.py
-rw-r--r--  1 root root 2876791 Jan 24 22:22 music.mp3
...

This sorts the files based on their size in ascending order.

 

3. Sort by modification time

To sort files by their modification time, you can use the ls command with the -l option and pipe it to the sort command using the -t (time) option. Here's an example:

ls -l | sort -nt6

This sorts the files based on their modification time in descending order.

 

4. Sort by creation time

Sorting files by creation time is a bit more complicated as it's not a standard attribute of a file. However, it can be done using the stat command to get the creation time and piping it to the sort command. Here's an example:

stat --printf="%W %n\n" * | sort -n | cut -d" " -f2-

This sorts the files based on their creation time in ascending order.

 

5. Sort by extension

To sort files by their extensions, you can use the ls command with the -l option, pipe it to the awk command to extract the file extensions, and then pipe it to the sort command. Here's an example:

ls -l | awk -F"." '{print $NF " " $0}' | sort -k1

 

6. Sort by owner

To sort files by their owner, you can use the ls command with the -l option and pipe it to the sort command using the -k3 option. Here's an example:

ls -l | sort -k3

This sorts the files based on their owner in ascending order.

 

7. Sort by group

To sort files by their group, you can use the ls command with the -l option and pipe it to the sort command using the -k4 option. Here's an example:

ls -l | sort -k4

This sorts the files based on their group in ascending order.

 

8. Sort by permissions

To sort files by their permissions, you can use the ls command with the -l option and pipe it to the sort command using the -k1 option. Here's an example:

ls -l | sort -k1

This sorts the files based on their permissions in ascending order.

 

9. Find files and sort by size recursively

To sort files by size, use the following command where we are searching files with .txt extension:

find . -name "*.txt" -type f -printf "%s %p\n" | sort -n

This will print the size of each file followed by its path, and then sort them based on size in ascending order.

 

10. Find files and sort by modification time

To sort all files in current directory with extension .txt by modification time, use the following command:

find . -name "*.txt" -type f -printf "%T+ %p\n" | sort

This will print the modification time of each file followed by its path, and then sort them based on modification time in ascending order.

 

11. Sort content of file in reverse order

The -r parameter in the sort command is used to sort the file contents in reverse order. Example:

foc@fedora:~$ sort -r example.txt
Young
Yesterday
Welcome
Tonight
...

This command sorts the contents of example.txt in reverse order, meaning that the lines at the bottom of the file appear at the top, and vice versa.

 

12. Sort content of file numerically

The -n option is used to sort numerically in the sort command. Before:

foc@fedora:~$ cat example2.txt
5108
152
126
16
529
70
1
990
56
269
86
69
147

Later:

foc@fedora:~$ sort -n example2.txt
1
16
56
69
70
86
126
147
152
269
529
990
5108

This command sorts the contents of file.txt numerically, rather than lexicographically. This means that numbers will be sorted in ascending order, regardless of their position in the line.

 

13. Sorting content of file by multiple fields

The file content can consist of more than one column. The -k parameter is used to sort these columns according to the specified column:

sort -k 1,3 -k 2 file.txt

This command sorts the contents of file.txt first by fields 1 through 3, and then by field 2. This means that lines with the same values in fields 1 through 3 will be sorted by the value in field 2.

 

14. Sorting a file with a custom delimiter

This command sorts the contents of file.csv numerically by the values in the second column (which are separated by commas), using a comma as the delimiter (-t ',').

sort -t ',' -k 2,2 -n file.csv

 

15. Sorting a file in place

This command sorts the contents of file.txt and overwrites the original file with the sorted output. The -o option specifies the output file, which in this case is the same as the input file.

sort -o file.txt file.txt

 

16. Sort and remove duplicates

You can remove duplicates using the sort command by piping the output to the uniq command with the -u option, which prints only the unique lines. Here's an example:

sort file.txt | uniq -u > output.txt

This command sorts the contents of file.txt and removes duplicate lines, then writes the unique lines to output.txt. If you want to remove duplicates from a file in place, you can use the -i option with sort, which modifies the file directly:

sort -o file.txt -u file.txt

This command sorts the contents of file.txt and removes duplicate lines, then overwrites the original file with the sorted output. The -o option specifies the output file, which in this case is the same as the input file. The -u option tells sort to only output unique lines.

 

Summary

We've done sorting examples with both the ls and sort commands. You can access more parameters from the sort command man page:

foc@fedora:~$ man sort
NAME
sort - sort lines of text files

SYNOPSIS
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
...

 

References

man7.org - Linux manual page for sort command
stackoverflow.com - How to sort a file in-place?

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment