Updated April 20, 2023
Introduction to Linux Sort by Size
In the Linux operating system, we are using the “sort” for sorting the multiple files in a specific order. We can sort the files in terms of size, name, etc. While sorting the normal files, the sorting is based on the ASCII format. If we need to sort the files in terms of size, we can use the kilobyte, megabyte, gigabyte, etc. The sort command help to identify the large size files on the Linux environment. It will be really helpful to clean the unwanted large file and keep the environment healthy. The Linux sort utility was written by Mike Haertel and Paul Eggert.
Syntax of Sort Command:
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
- sort: We can use the “sort” keyword in the syntax or command. It will take different arguments like OPTION, file name, path, etc. As per the provided arguments, it will sort the necessary files as per the giving parameters or options.
- OPTION: We can provide the different flags as the option that is compatible with the “sort” command.
- FILE: We can provide the specific file or path to the “sort” command. It will help to identify the files as per the given parameters.
How Linux Sort by Size Command works?
In the Linux operating system, it is mandatory to check the files and folders size. If the size will increase and we haven’t taken the necessary action on it. The server or the environment will be in an unhealthy state or may the server will not work. To avoid such incidents, we can use the sort command-line utility to identify the large size files and folders.
The sort command will accept the different arguments like the option, file, file path, etc. As per the arguments, the sort command will sort the files as per the file size.
Below are the lists of options that are compatible in sort command as per the file size.
Sr No | Option | Description |
1 | -b, –ignore-leading-blanks | It will help to ignore the leading blanks. |
2 | -d, –dictionary-order | It will only consider the blanks and alphanumeric characters. |
3 | -f, –ignore-case | It will help to convert the lower case to upper case characters. |
4 | -g, –general-numeric-sort | It will help to compare the general numerical value. |
5 | -i, –ignore-nonprinting | It will only consider the printable characters. |
6 | -M, –month-sort | It will help to compare the month parameters like [ compare (unknown) < ‘JAN’ < … < ‘DEC’ ]. |
7 | -h, –human-numeric-sort | It will help to compare the human readable numbers like 2K 1G. |
8 | -n, –numeric-sort | It will help to compare the string numerical value. |
9 | -R, –random-sort | It will help to sort by the random hash of keys. |
10 | –random-source=FILE | The parameters help to get the random bytes from FILE. |
11 | -r, –reverse | It will help to reverse the result of comparisons. |
12 | –sort=WORD | It will help for sort according to the WORD but we need to use the specific flags with it like general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V. |
13 | -V, –version-sort | It is the natural sort of (version) numbers within the text. |
Examples of Linux Sort by Size
Given below are the examples mentioned :
Example #1
Sort Command – Sort Big File
The sort command, we are able to sort the largest file size in the environment. It is the most common way to practice the sort command in the Linux operating system. We need to use the “-r and -n” option with the sort command.
Code:
du -ah / | sort -n -r | head -n 3
Explanation:
- As per the above command, we are using the sort command with different options like “-n & -r”. We are sorting the files that are in big size on the “/” location and printing only the first 3 largest files.
Output:
Example #2
Sort Command – Sort File in Working Directory
In the sort command, we are having the functionality to sort the largest file in the current working directory.
Code:
du -a | sort -n -r | head -n 3
Explanation:
- In sort command, we can sort the file in the current working directory and print the first 3 largest files. The current working directory is “/home”.
Output:
Example #3
Sort Command – Sort with File Size
In the Sort Command, we can search out the number of files. From the same files, we can sort the size of the files in human-readable format like K, M, G. We can use the “-r” option for the recursive search.
Code:
du -hs * | sort -r | head -3
Explanation:
- In the above sort command, we are able to find the top 3 files those are in big size. In the first screenshot, we are having the number of files in “/home” directory. As per the above sort command, we are able to find the top 3 files those are big in size refer to second screenshot.
Output:
Example #4
Sort Command – With Find Option
In the Linux environment, we are having the functionality to sort the file as per the file name as well as the file size. But we can also use the sort command with the combination of different Linux commands like find.
Code:
find /home/ -type f -exec du -Sh {} + | sort -r -n | head -n 3
Explanation:
- As per the above command, we are using the sort command with the combination of the find command. We are finding the top three large files in “/home” directory.
Output:
Conclusion
We have seen the uncut concept of “Linux Sort by Size” with the proper example, explanation and command with different outputs. The Sort Command will help to sort the multiple files in terms of size and string. We can use the sort command with the multiple commands like du, find, etc.
Recommended Articles
We hope that this EDUCBA information on “Linux Sort by Size” was beneficial to you. You can view EDUCBA’s recommended articles for more information.