find_largest_file.sh
Find the single largest file in $HOME with human-readable output.
Usage
Make executable:
chmod +x find_largest_file.sh
Run:
./find_largest_file.sh
Example Output
Searching for the largest file in /home/ben...
Largest file found:
Path : /home/ben/media/Skyfall.mp4
Size : 2,147,483,648 bytes (2 GB)
Details
Recursively searches $HOME (includes all subdirectories like Documents, Downloads, etc.)
Byte counts include comma separators via sed
Units auto-scale (KB/MB/GB/TB) via bc
Gracefully handles permission errors
Avoids polluting system $PATH variable
Requirements
Standard Unix tools: find, sort, cut, sed, bc
#!/bin/bash
# Find the largest file in the user's home directory
SEARCH_DIR="$HOME"
echo "Searching for the largest file in $SEARCH_DIR..."
# Find largest file
result=$(find "$SEARCH_DIR" -type f -printf '%s\t%p\n' 2>/dev/null | sort -n | tail -n 1)
if [[ -z "$result" ]]; then
echo "No files found or permission denied in $SEARCH_DIR"
exit 1
fi
# Split size and path
SIZE=$(echo "$result" | cut -f1)
FILE_PATH=$(echo "$result" | cut -f2-) # Changed from PATH to FILE_PATH
# Function for human-readable size (using bc for portability)
human_size() {
local size=$1
local value=$size
local unit="B"
if [ $size -ge 1099511627776 ]; then
value=$(echo "scale=1; $size / 1099511627776" | bc)
unit="TB"
elif [ $size -ge 1073741824 ]; then
value=$(echo "scale=1; $size / 1073741824" | bc)
unit="GB"
elif [ $size -ge 1048576 ]; then
value=$(echo "scale=1; $size / 1048576" | bc)
unit="MB"
elif [ $size -ge 1024 ]; then
value=$(echo "scale=1; $size / 1024" | bc)
unit="KB"
fi
# Remove trailing .0 if present
value=${value%\.0}
echo "$value $unit"
}
HUMAN_SIZE=$(human_size "$SIZE")
# Format raw bytes with commas for readability using sed
SIZE_FMT=$(echo "$SIZE" | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
echo "-------------------------------------"
echo "Largest file found:"
echo "Path : $FILE_PATH"
echo "Size : $SIZE_FMT bytes ($HUMAN_SIZE)"
echo "-------------------------------------"
exit 0
Running the Linux OS, there are so many tasks you can accomplish with bash scripting alone. Hope you find this useful.
Ben Santora - January 2026
Top comments (0)