How to move 100 files from a folder containing thousands?
for file in $(ls -p | grep -v / | tail -100)
do
mv $file /other/location
done
That assumes file names don't contain blanks, newline (assuming the default value of $IFS
), wildcard characters (?
, *
, [
) or start with -
.
It's easiest in zsh:
mv -- *([1,100]) /other/location/
This moves the first 100 non-hidden files (of any type, change ([1,100])
to (.[1,100])
for regular files only, or (^/[1,100])
for any type but directory) in name lexicographic order. You can select a different sort order with the o
glob qualifier, e.g. to move the 100 oldest files:
mv -- *(Om[1,100]) /other/location/
With other shells, you can do it in a loop with an early exit.
i=0
for x in *; do
if [ "$i" = 100 ]; then break; fi
mv -- "$x" /other/location/
i=$((i+1))
done
Another portable way would be to build the list of files and remove all but the last 100.
If you're not using zsh:
set -- *
[ "$#" -le 100 ] || shift "$(($# - 100))"
mv -- "$@" /target/dir
Would move the last (in alphabetical order) 100 ones.