How to move the N largest files in a directory on the command line

This command moves the N largest files in the current directory to a target directory. It uses find to list files with their sizes, sorts them by size in descending order, takes the first N, and moves them with mv. Filenames containing spaces or newlines are handled correctly.

move-largest-files.sh
find . -maxdepth 1 -type f -printf '%s\t%p\n' \
  | sort -rn \
  | head -n 10 \
  | cut -f2 \
  | xargs -d '\n' mv -t /target/dir/

Replace 10 with the number of files you want to move, and /target/dir/ with the destination directory.

How it works

  • find . -maxdepth 1 -type f lists only regular files in the current directory (no recursion, no directories).
  • -printf '%s\t%p\n' prints each file’s size in bytes, a tab, and its path.
  • sort -rn sorts the lines numerically (-n) in reverse order (-r), so the largest files come first.
  • head -n 10 keeps only the first 10 lines (the 10 largest files).
  • cut -f2 drops the size column, leaving just the paths.
  • xargs -d '\n' mv -t /target/dir/ passes the paths to mv, using newlines as delimiters so spaces in filenames are preserved.

Create the target directory first

If the destination directory may not exist yet, create it before running the command:

mkdir-target.sh
mkdir -p /target/dir/

Check out similar posts by category: Linux Shell