Linux 0

How to Find Large Files Using the CLI in Linux


Managing disk space efficiently is crucial for maintaining system performance and preventing storage issues. One of the most effective ways to free up space is by identifying and removing large, unnecessary files. This article will guide you through finding large files on a Linux system using the command line interface (CLI).

1. Using the find Command

The find command is a powerful tool that can search for files based on various criteria, such as size, name, or modification date. To locate files larger than a specific size, use the -size option.

Example: Finding Files Larger than 100MB

find /path/to/search -type f -size +100M
  • /path/to/search: Replace this with the directory where you want to search. If you want to search the entire system, use /.
  • -type f: This option ensures that the command only looks for files (not directories).
  • -size +100M: This searches for files larger than 100MB. The + sign means "greater than", and M stands for megabytes. You can also use G for gigabytes or k for kilobytes.

Example: Finding Files Larger than 1GB in the /var Directory

find /var -type f -size +1G

Example: Finding Files Larger than 500MB and Sorting by Size

To find files larger than 500MB and display them sorted by size, you can combine find with xargs and du:

find /path/to/search -type f -size +500M -print0 | xargs -0 du -h | sort -rh
  • -print0 and -0 ensure that the command handles filenames with spaces or special characters properly.
  • du -h: Displays file sizes in human-readable format (e.g., MB, GB).
  • sort -rh: Sorts the results in reverse order (largest first).

2. Using the du Command

The du (disk usage) command can also help in finding large files or directories. While du is often used to get the size of directories, it can be useful when combined with sorting.

Example: Finding Large Files and Directories in the Current Directory

du -ah . | sort -rh | head -n 10
  • -a: Displays the size of both files and directories.
  • -h: Outputs sizes in human-readable format.
  • sort -rh: Sorts the results by size, with the largest files/directories at the top.
  • head -n 10: Limits the output to the top 10 largest files or directories.

Example: Displaying Only Files Larger than 100MB

You can filter results from du to display files larger than 100MB:

du -ah /path/to/search | awk '$1 ~ /[0-9]+M/ || $1 ~ /[0-9]+G/' | sort -rh
  • This command uses awk to filter out files larger than 100MB (or gigabytes if applicable) and sorts the result in descending order.

3. Using the ncdu Tool (Optional)

For users who prefer a more interactive approach, ncdu is a terminal-based tool for analysing disk usage. It’s not installed by default, but it provides an easy-to-navigate interface to quickly locate large files.

Install ncdu:

sudo apt install ncdu   # On Debian-based systems
sudo yum install ncdu   # On Red Hat-based systems

Usage:

Simply run the following command and navigate through the results:

ncdu /

Summary

To find large files on a Linux system, you can use the following commands:

  • find for locating files based on size.
  • du for checking disk usage and sorting files by size.
  • ncdu for an interactive disk usage viewer.

Regularly running these commands can help you identify and remove large files, freeing up disk space and ensuring your system remains efficient.

 


Was this answer helpful?

One email a month. Endless business benefits.

Don't miss out on WMTWWFY — the newsletter that keeps your website fast, safe, and visible.

« Back
Spinner
aluminium-anthropoid Security Check