JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Sort Files by Size in Linux

Author:JIYIK Last Updated:2025/04/06 Views:

Sometimes you want to do some deep cleaning of your system by finding unnecessary large files and deleting them or removing files that are smaller than a predetermined size, such as logs. Linux provides various utilities that can help us find such files when used in conjunction.

This tutorial will show you how to use it in daily usage find, such as finding files in a folder based on their size.

To find the largest files in a given folder, we can use the duand sortcommands.

user@linux:~$ ls -lh
-rw-r--r-- 1 user user 8.0M Jan 1 00:00 a
-rw-r--r-- 1 user user 4.0M Jan 1 00:00 b
-rw-r--r-- 1 user user 2.0M Jan 1 00:00 c
-rw-r--r-- 1 user user 1.0M Jan 1 00:00 d
user@linux:~$ du -h * | sort -h
1.0M    d
2.0M    c
4.0M    b
8.0M    a

This will print out the files in order of increasing size, so the largest files in the directory will be at the end of the program's output, and the smallest files will be at the beginning.

Note -hthe use of the - flag. This tells the command to give the sizes in human-readable form.

The following command finds and sorts the directories in your home directory in ascending order by size.

user@linux:~$ sudo find /var/ -maxdepth 1 -type d -exec du -sh {} \; | sort -h
4.0K    /var/local
4.0K    /var/mail
4.0K    /var/opt
56K     /var/spool
60K     /var/tmp
92K     /var/snap
7.3M    /var/backups
4.3G    /var/log
4.4G    /var/cache
17G     /var/lib
25G     /var/

If you know the minimum or maximum size of the files you are searching for, you can use findthe command to list such files.

Let's say you want to find all files larger than 200 MB (200M). We can do this with the following command, which also prints out the size of each file found. We use sudoto go into all root-owned directories.

Keep in mind that the output of a run may produce different files.

user@linux:/var$ sudo find . -type f -size +200M -exec ls -lh {} \;
-rw------- 2 root root 363M Jan 1 00:00 ./lib/snapd/snaps/qt513_24.snap
-rw------- 2 root root 363M Jan 1 00:00 ./lib/snapd/cache/cf177ca655544816bb73b6d8e89c83753b96548f105acd563c1bf1b7d0d046bd3e99a96db5bfe912f8a446a8e9d5b6c5

The Linux command dfallows us to view the overall disk usage of each partition of the file system, which can help shrink partitions that are over-utilizing space. We run the following command to find the disk usage.

Once again, this may look completely different depending on your disk configuration. snapThe entries with are called loopback disks, they are virtual disks that mount disk images, which are required by the Snap utility on Ubuntu.

user@linux:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  2.1M  1.6G   1% /run
/dev/nvme0n1p6  200G   45G  146G  24% /
tmpfs           7.8G  397M  7.4G   5% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/nvme0n1p2   96M   36M   61M  37% /boot/efi
/dev/loop1       56M   56M     0 100% /snap/core18/2253
/dev/loop0      165M  165M     0 100% /snap/gnome-3-28-1804/161
/dev/loop2      512K  512K     0 100% /snap/gifex/3
/dev/loop3       66M   66M     0 100% /snap/gtk-common-themes/1519
/dev/loop4      128K  128K     0 100% /snap/bare/5
/dev/loop6      363M  363M     0 100% /snap/qt513/24
/dev/loop7      100M  100M     0 100% /snap/core/11993
/dev/sda7       1.1T  384G  677G  37% /home
tmpfs           1.6G  1.9M  1.6G   1% /run/user/1000
/dev/loop8       56M   56M     0 100% /snap/core18/2284

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial