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 flists 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 -rnsorts the lines numerically (-n) in reverse order (-r), so the largest files come first.head -n 10keeps only the first 10 lines (the 10 largest files).cut -f2drops the size column, leaving just the paths.xargs -d '\n' mv -t /target/dir/passes the paths tomv, 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/If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow