Pregunta:
Tengo muchos archivos en una carpeta, llamada 00802_Bla_Aquarium_XXXXX.jpg
. Ahora necesito copiar cada cuarto archivo a una subcarpeta, diciendo en selected/
.
00802_Bla_Aquarium_00020.jpg <= this one
00802_Bla_Aquarium_00021.jpg
00802_Bla_Aquarium_00022.jpg
00802_Bla_Aquarium_00023.jpg
00802_Bla_Aquarium_00024.jpg <= this one
00802_Bla_Aquarium_00025.jpg
00802_Bla_Aquarium_00026.jpg
00802_Bla_Aquarium_00027.jpg
00802_Bla_Aquarium_00028.jpg <= this one
00802_Bla_Aquarium_00029.jpg
¿Cómo hago esto?
Respuesta:
Con zsh, podría hacer:
n=0; cp 00802_Bla_Aquarium_?????.jpg(^e:'((n++%4))':) /some/place
POSIXly, la misma idea, solo un poco más detallada:
# put the file list in the positional parameters ($1, $2...).
# the files are sorted in alphanumeric order by the shell globbing
set -- 00802_Bla_Aquarium_?????.jpg
n=0
# loop through the files, increasing a counter at each iteration.
for i do
# every 4th iteration, append the current file to the end of the list
[ "$(($n % 4))" -eq 0 ] && set -- "$@" "$i"
# and pop the current file from the head of the list
shift
n=$(($n + 1))
done
# now "$@" contains the files that have been appended.
cp -- "$@" /some/place
Dado que esos nombres de archivo no contienen ningún carácter en blanco o comodín, también puede hacer:
cp $(printf '%s\n' 00802_Bla_Aquarium_?????.jpg | awk 'NR%4 == 1') /some/place