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

awk tutorial – 7 awk printing examples

Publish Date:2025/04/06 Views:118 Category:OPERATING SYSTEM

This is the first article in the new awk tutorial series. We will publish several articles on awk in the coming time, which will explain all the features of awk with practical examples. In this article, let's review the basic awk working me

Sed Tutorial: Advanced Sed Substitution Examples

Publish Date:2025/04/06 Views:80 Category:OPERATING SYSTEM

In this article, let's review some interesting workarounds using the "s" substitution command in sed with a few real-world examples. 1. sed replaces the delimiter As we discussed in our previous article, we can use different delimiters in s

Installing Python modules without root access

Publish Date:2025/04/06 Views:67 Category:OPERATING SYSTEM

Use --user the -p option to install Python modules without root access, for example pip install requests --user . --user The -p option installs the package in the user's home directory and helps resolve permission issues. $ pip install requ

How to run TypeScript files from the command line

Publish Date:2025/04/06 Views:54 Category:OPERATING SYSTEM

Run TypeScript files from the command line using the ts-node package, for example npx ts-node myDirectory/myFile.ts . ts-node The command will convert the TypeScript file to JavaScript and run the code in one step. This is the TypeScript fi

PBKDF2+HMAC Hash Conflict

Publish Date:2025/04/06 Views:55 Category:OPERATING SYSTEM

Cryptocurrency enthusiast Christian 'CodesInChaos' Winnerlein once wrote: plnlrtfpijpuhqylxbgqiiyipieyxvfsavzgxbbcfusqkozwpngsyejqlmjsytrmd and eBkXQTfuBqp'cTcarg* have the same PBKDF2-HMAC-SHA1 hash. This intrigued me, so I decided to find

How to create simple Mac applications from shell scripts

Publish Date:2025/04/06 Views:200 Category:OPERATING SYSTEM

Basically, a Mac application has a .app ./file extension, but it's not really a file - it's a package. We can view the contents of an application by navigating to it in the Finder, right-clicking it, and then selecting " Show Package Conten

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial