EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux Sort by Size

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux Sort by Size

Linux Sort by Size

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,486 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

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:

Linux Sort by Size 1

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:

Working Directory

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:

Linux Sort by Size 3

Linux Sort by Size 4

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:

With Find Option

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

This is a guide to Linux Sort by Size. Here we discuss the introduction, how Linux sort by size command works with examples respectively. You may also have a look at the following articles to learn more –

  1. Linux wget
  2. Linux tracepath
  3. Linux Export
  4. Linux Network Command

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More