NASutils Knowledge BaseUnofficialPolski

Bash · lesson 25 of 30 · Tools for the NASFind the largest files

find prints the size and path of every file in the folder and its subfolders, sort puts the largest first.

Programming on the NAS › Bash in 30 lessons

25-largest-files.sh Download 25-largest-files.sh

#!/usr/app/TurboShell/bin/bash
# The largest files of a folder and its subfolders
set -euo pipefail
folder=${1:?give a folder}
count=${2:-3}
find "$folder" -type f -printf '%s %p\n' | sort -rn | awk -v n="$count" 'NR <= n' \
  | while read -r size path; do
    printf '%8d  %s\n' "$size" "$path"
  done

Terminal

$ bash 25-largest-files.sh photos
   15800  photos/wedding.mov
    7300  photos/trip.mp4
    4200  photos/beach.jpg
$ bash 25-largest-files.sh photos 1
   15800  photos/wedding.mov

Exercise

Leave out files under 1000 bytes.