I'm moving png
files from source/
to dest/
with this:
mv /source/*.png /dest/
How can I change that command so I only move 10 png
files?
I'm moving png
files from source/
to dest/
with this:
mv /source/*.png /dest/
How can I change that command so I only move 10 png
files?
You can do this in Zsh with a glob qualifier:
mv /source/*.png([1,10]) /dest/
Moves the first 10 ones in alphabetic order. You can pick a different order using the o
/O
/n
qualifiers. For instance:
mv /source/*.png(OL[1,10]) /dest/
Would move the 10 largest ones.
An optimised version that selects the first 10 matches without bothering to sort can be done with the Y
qualifier:
mv /source/*.png(Y10) /dest/
POSIXly, that could be done with:
set -- /source/*.png
[ "$#" -le 10 ] || shift "$(( $# - 10 ))"
mv -- "$@" /dest/
Which would move the 10 last ones in alphabetic order.
Note that it excludes hidden ones and if there's no match, it would attempt to move a file called /source/*.png
and likely fail.
ls /source/*.png | head -n10 | xargs -I{} mv {} /dest/
ls
with the --zero
option, combined with head -z -n 10
, and xargs -0r ...
. And, since everything else relies on GNU versions, you may as well use GNU mv
's -t
option too. ls --zero /source/*.png | head -z -n 10 | xargs -0r mv -t dest/
. Even so, it's still better to use find
: find /source -maxdepth 1 -name '*.png' -print0 | head -z -n 10 | xargs -0r mv -t dest/
.
– cas
Dec 02 '22 at 07:43
ls --zero
avoids the problem of never parse ls
? I have no idea about --zero
and how that works but with your example I understood better.
– Edgar Magallon
Dec 02 '22 at 16:13
--zero
is fairly new for GNU ls (first added as --null in July 2021, renamed to --zero a few days later. BTW, according to the changelog, "--zero also implies -1, -N, --color=none, --show-control-chars"). I wouldn't say "avoids the problem" - won't help at all for parsing metadata out of ls -l
(that's what stat
is for, anyway) but should be OK for just generating a list of NUL-separated files to pipe into other programs or read into an array with mapfile
. Personally, I'll stick with using find
, although the convenience of ls's sorting capabilities might be useful.
– cas
Dec 02 '22 at 16:53
find
or for in /somepath
too
– Edgar Magallon
Dec 02 '22 at 17:50
ls | head -10
? – U. Windl Dec 02 '22 at 07:16ls
(and what to do instead)? – cas Dec 02 '22 at 07:43