How to Sort Files by Size in Linux/Unix?

To sort files in Linux, use the ls command with the -S flag. You can also use the du and sort command in combination.

Sorting files in regard to their size can be easily done by using the “ls” command and by adding specific flag options. Alternatively, you can also use other available commands or the GUI to sort files in a Linux-based or a Unix-based operating system.

When working with Linux Directories, especially on a server machine, sorting files in a specific order is super helpful as it allows you to organize files in a manner that a single glance can interpret. 

Let’s get started with the first method!

How to Sort Files in Linux Using the ls Command with -S Option?

The first method revolves around the list show command “ls” and an optional flag “-S.” The “-S” sorts the files according to their file/directory size. 

To showcase, first, get the normal “ls” command output by typing ls in a terminal session. Doing this will display the following output on the terminal:

Currently, the output only shows the names of the file. However, before using the sort option, use the “-l” to display the properties of files in the output:

ls -l

In the output image above, you can see the size of each file in the fourth column. Currently, it is not sorted. To sort this out, let’s add the “-S” flag option and the “-l” option.

ls -lS

You will get the following out on the terminal:

You can clearly see that the output’s order has been changed according to the size of the files in the current directory.

How to Sort Files in Reverse Order With ls Command?

To sort files in reverse in Linux or Unix-based operating Systems, you can use the “-r” with the ls command and the “-S” flag. The -r flag stands for reversing the output of the command. 

Type the following command in a terminal window:

ls -lSr

This will return the following output on the terminal window:

As you can see, the files are now sorted and displayed in the reverse order. If you want to minimize the output on the terminal, then you can remove the -l. Executing the following command in a terminal window:

ls -Sr

You will get the following command on the terminal window:

You have successfully sorted the files in a directory in the reverse order.

Affordable VPS Hosting With Dracula Servers

Dracula Servers offers high-performance server hosting at entry-level prices. The plans include Linux VPS, Sneaker Servers, Dedicated Servers & turnkey solutions. If you’re looking for quality self-managed servers with high amounts of RAM and storage, look no further.

Dracula Server Hosting is also Perfect for Hosting Telegram.Forex App with built-in support for MT4 with trade copier. Check the plans for yourself by clicking Here!

How to Sort Files Using the –sort Option With the ls Command?

Using the –sort=size option within the ls command is an alternative to the -S flag. The result of this command will display the files in ascending order according to their file size.

Type the following command in the terminal window:

ls --sort=size

In the output screenshot, you can clearly see that the files are now sorted according to their file size.

How to Sort Files and Display Results in Human-Readable Format?

To get the output of the sort option in the ls command, you can use the -h flag. The -h returns the size output in a human-readable format by mentioning K for kilobytes and M for megabytes next to the file size. 

Let’s take an example to showcase the working of the -h flag. First, get the output of the sort in ls command of the directory by typing “ls -lS”:

The size in the output displays the total number of bytes each file is taking in the disk. Even the total size value is not readable-friendly. Now, use the -h flag:

ls -lhS

Executing this command will display the following output on the terminal:

As you can see from the output, the size is now represented in Megabytes by placing the letter M next to it. It is very easy to deduce that the total size in the current directory is 148 MBs. The size values without any letters mean KBs.

How to Sort Files and Hidden Files in Linux / Unix?

If you want to sort the files and display them in order which also includes the hidden files and the hidden directories, then you can use the -a flag in the “ls -lS” command. Therefore, in the terminal of the current working directory, simply type the following command:

ls -laS

Once you execute this command, you will get the following output on the terminal window:

This time around, the output contains the hidden directories. If there are any other hidden files or directories, they will also be listed in the output of this command.

How to Sort Files Using the du and sort Command in linux?

You can also use the du command to list the size of all the files in the present working directory and then sort them using the sort command. To demonstrate this, simply type the following command in the terminal:

du -hs * | sort -h

Executing this command will display the following output on the terminal:

The benefit of using the du and sort command over the ls command is that this command displays only the names of the files and their file size. Moreover, the output is already in a readable-friendly format.

Wrap up

To sort files in Linux / Unix-based operating systems, you can use the ls command with the “-S” flag. The -S flag means to sort, which will sort the files in ascending order according to their file size. 

However, to get a better-formatted output, you can use the -l flag to list the details of the files, the -h flag to have a more readable output in which size is displayed as MBs and KBs, and the -a flag to include the hidden files in the output as well. Lastly, another method is to combine the du command and the sort command.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.