[email protected]Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 22, 2023

How find large files with du and find command.

How Find Large Files With Du And Find Command.

This tutorial explains how to find the largest files and directories in Linux systems using the find and du commands.

 

Find Large Files Using the find Command.

sudo find . -xdev -type f -size +100M

Replace . with the path to the directory where you want to search for the largest files.

The output will show a list of files without any additional information.

/var/lib/libvirt/images/centos-7-desktop_default.img
/var/lib/libvirt/images/bionic64_default.img
/var/lib/libvirt/images/win10.qcow2
/var/lib/libvirt/images/debian-9_default.img
/var/lib/libvirt/images/ubuntu-18-04-desktop_default.img
/var/lib/libvirt/images/centos-7_default.img

The find command also can be used in combination with other tools such as ls or sort to perform operations on those files.

In the example below, we are passing the output of the find command to ls which will print the size of each found file and then pipe that output to the sort command to sort it based on the 5th column which is the file size.

find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r

The output will look something like this:

-rw-------  1 root   root 40967M Jan  5 14:12 /var/lib/libvirt/images/win10.qcow2
-rw-------  1 root   root  3725M Jan  7 22:12 /var/lib/libvirt/images/debian-9_default.img
-rw-------  1 root   root  1524M Dec 30 07:46 /var/lib/libvirt/images/centos-7-desktop_default.img
-rw-------  1 root   root   999M Jan  5 14:43 /var/lib/libvirt/images/ubuntu-18-04-desktop_default.img
-rw-------  1 root   root   562M Dec 31 07:38 /var/lib/libvirt/images/centos-7_default.img
-rw-------  1 root   root   378M Jan  7 22:26 /var/lib/libvirt/images/bionic64_default.img

If the output contains a lot of lines of information you can use the head command to print only the first 10 lines:

find . -xdev -type f -size +100M -print | xargs ls -lh | sort -k5,5 -h -r | head

Let’s break down the command:

  • find . -xdev -type f -size +100M -print – search only for files (-type f) in the current working directory (.), larger than than 100MB (-size +100M), don’t descend directories on other filesystems (-xdev) and print the full file name on the standard output, followed by a new line (-print).

  • xargs ls -lh – the output of the find command is piped to xargs which executes the ls -lh command that will print the output in long listing human-readable format.

  • sort -k5,5 -h -r – sort lines based on the 5th column (-k5,5), compare the values in human-readable format (-h) and reverse the result (-r).

  • head : prints only the first 10 lines of the piped output.

The find command comes with a lot of powerful options. For example, you can search for large files that are older than x days, large files with a specific extension, or large files that belong to a particular user.

 

Find Large Files and Directories Using the du Command

The du command is used to estimate file space usage, and it is particularly useful for finding directories and files that consume large amounts of disk space.

The following command will print the largest files and directories:

du -ahx . | sort -rh | head -5

The first column includes the size of the file and the second one the file name:

55G	.
24G	./.vagrant.d/boxes
24G	./.vagrant.d
13G	./Projects
5.2G	./.minikube

Explanation of the command:

  • du -ahx . : estimate disk space usage in the current working directory (.), count both files and directories (a), print sizes in a human-readable format (h), and skip directories on different file systems (x).

  • sort -rh : sort lines by comparing values in human-readable format (-h) and reverse the result (-r).

  • head -5 : prints only the first five lines of the piped output.

The du command has many other options that can be used to refine the output of the disk space usage.

 

Conclusion

We’ve shown you how to find the largest files and directories using the find and du commands.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Mon – Fri 9:00AM – 5:00PM