If you are only moving the oldest one, use this:
_mv(){ mv -- "$1" /root/Dropbox-archive/; }
_mv /Dropbox/apache2-backup-*
But your title doesn't say so.
Since processing ls
output is a bad idea, here is a better approach:
if [ "$BASH" ]; then
# If you happen to use bash or any other shell that has some similar array:
move_first_things(){ mv -- "${@:0:50}" /root/Dropbox-archive/; }
else # Unfortunate POSIX way
# As long as copy-pasting is acceptable, loop-unrolling with
# `mv -- "$1" "$2" "$3"... /root/Dropbox-archive/` is better since it calls `mv` less.
move_first_things(){
local max=50 count=0
while [ "$count" -lt "$max" ]; do
mv -- "$1" /root/Dropbox-archive/
shift
: $((count = count + 1))
done
}
fi
And since globbing sorts the output just like ls
does:
move_first_things /Dropbox/apache2-backup-*
PS: This can be indeed done with external programs (mikeserv, explanation here). With a bit more care and extra obfuscation, people can have it done safely. But I am still going to use the nice filename in this case.
PPS: don_crisst mentioned some zshism. zsh has nice support for anonymous things, like functions, param expansions and arrays. Still using the filename way (since bash
doesn't have such sorting operators), the whole thing can be written as baks=( /Dropbox/apache2-backup-* ); mv "$baks" /root/Dropbox-archive/
(referencing an array like this simply gives its first member), or for multiple things, mv "${baks[@]:0:50}" /root/Dropbox-archive/
. Well, this looks a bit better than those wrapper functions actually.
ls -1 /Dropbox/apache2-backup-*
, doesn't it automatically order alphabetically by name, i.e. by date (since date is formatted with most significant digit on the left)? Then you can get the oldest with|head -1
(assuming there are no newlines in file names). – Sparhawk Oct 24 '15 at 00:22mv "$(ls -1 /root/Dropbox/apache2-backup-* | head -1)" /root/Dropbox-archive/
? – Alex Stewart Oct 24 '15 at 00:32