Maybe I’ve just reinvented the hot water. But I needed it and it took me less time to write it on my own than searching for the solution.
The task was to take all files in a directory and copy them into an output directory renaming them sequentially starting from a provided number. So files will be something like 0000.jpg, 0001.jpg, …, 0035.jpg, etc.
Less words as usual 🙂
#!/bin/bash # copy all files in the directory to an output one renaming them # sequentially. # # GNU LESSER GENERAL PUBLIC LICENSE # Version 2.1, February 1999 # # Copyright (C) 1991, 1999 Free Software Foundation, Inc. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # Everyone is permitted to copy and distribute verbatim copies # of this license document, but changing it is not allowed. # # http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt # OUT_DIR=out PAD_LENGTH=5 if [ -z "$1" ] || [ -z "$2" ] ; then echo "./move.sh <number-to-start-from> <extension>" echo " " echo "./move.sh 12 JPG" else # testing output directory exist. if not create it. if [ ! -d ${OUT_DIR} ] ; then mkdir ${OUT_DIR} fi COUNTER=$1 EXT=$2 for file in *.${EXT} ; do OUTFILE=$(printf "%0${PAD_LENGTH}i\n" "${COUNTER}") cp -v $file ${OUT_DIR}/${OUTFILE}.${EXT}; let COUNTER++; done fi
Here is the source code (pdf) for easy read/copy/paste.
Thanks, thats exactly what I was looking for, though it didn’t work for me until I’ve put double apostrophes on line 34.
(cp -v “$file” “${OUT_DIR}/${OUTFILE}.${EXT}”;)