How to move the first x files
mv `ls | head -500` ./subfolder1/
With zsh
:
mv -- *(D.oN[1,5000]) ./subfolder1
To move up to 5000 regular files in the order they are in the directory.
For the first 5000 in the lexicographically sorted list:
mv -- *(D.[1,5000]) ./subfolder1
If you get an error about arg list too long. You can use zsh
's buitin mv
command by issuing:
zmodload zsh/files
first.
POSIXly:
set --
for f in .* *; do
[ "$#" -lt 5000 ] || break
[ -f "$f" ] || continue
[ -L "$f" ] && continue
set -- "$@" "$f"
done
mv -- "$@" subfolder1/
A version that is simple and supports special chars, spaces, etc.
ls -Q dir1 | head -1000 | xargs -i mv dir1/{} dir2/
For this to work as-is dir2
must exist and you have to execute it from the parent directory of dir1
and dir2
.
This will move 1000 files from dir1 to dir2.