Linux - How to find large file size

By xngo on June 8, 2019

When you are running out of disk space, it is good to know where are the large files so that you can delete them. Here is the command to find big files in your hard drive.

# Find big files.
find /directory/path/ -type f -size +100M -printf '%s %p\n' \
    | sort -k1,1n \
    | numfmt --field=1 --to=iec-i  --format='%10f'

Here are the explanations of each commands:

  • -size +100M: Find files that are bigger than 100MB.
  • -printf '%s %p\n': Print the size in bytes and the path.
  • sort -k1,1n: Sort the first column numerically.
  • numfmt --field=1 --to=iec-i --format='%10f': Convert the first column to human readable size(i.e. Ki=Kilo, Mi=Mega, Gi=Giga, etc)

Complete script to find big files

#!/bin/bash
set -e
# Description: Find big files.
script_name=$(basename "${0}")
 
target_directory=$1
size_mb=$2
 
# Error handling.
    cmd_eg=$(printf "%s\n%s\n" \
            "  e.g. ./${script_name} directory size_in_MB" \
            )
    if [ ! -d "${target_directory}" ]; then
        echo "Error: ${target_directory} is not a directory. Aborted!"
        exit 1;
    fi
 
    if [ -z "${size_mb}" ]; then
        echo "Error: Enter size number in megabyte. Aborted!"
        exit 1;
    fi
 
# Find big files.
find "${target_directory}" -type f -size +${size_mb}M -printf '%s %p\n' \
    | sort -k1,1n \
    | numfmt --field=1 --to=iec-i  --format='%10f'

Usage

# Find all files in /tmp directory that are bigger than 230 MB .
./find-big.sh /tmp 230

Output

     275Mi ./2016-08/File_46JB.rar
     282Mi ./2016-08/File_12E.rar
     294Mi ./2016-08/File_Pwr.rar
     314Mi ./2016-08/File_53FB.rar
     317Mi ./2016-08/File_45M.rar
     354Mi ./2019-03/Project_19J.rar
     419Mi ./2019-03/Project_25MP.rar
     483Mi ./2019-03/Mpv_SInstall.rar
     733Mi ./2019-03/Project_45PG.rar
     941Mi ./2019-03/Presentation_8Bil.rar

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.