Skip to content

Instantly share code, notes, and snippets.

@robex
Last active May 1, 2018 00:14
Show Gist options
  • Save robex/66f61d58ec344bbdefdf35592570b5eb to your computer and use it in GitHub Desktop.
Save robex/66f61d58ec344bbdefdf35592570b5eb to your computer and use it in GitHub Desktop.
rename.sh: rename files in a directory into a sequential numbering order
# Rename all the files specified in a directory to a sequential order.
# CAREFUL: DON'T USE WITH THE SAME NDIGITS IF THE FILES IN THE DIRECTORY
# ARE ALREADY NAMED SEQUENTIALLY, OR THEY MAY BE OVERWRITTEN AND LOST
# A workaround for this is to run rename.sh with another value for the ndigits
# and then run it again with the original.
#
# /robex/ - 2018
#no arguments
if [ $# -eq 0 ]; then
echo "error: arguments"
echo "usage: ./rename.sh [dir] [ndigits] [prefix]"
echo "args:"
printf "\tdir: directory to rename files inside of\n"
printf "\tndigits (optional): number of digits in the format (ex: 0001.gif), USE TWO DIGITS. default = 04\n"
printf "\tprefix (optional): add something before the digits\n"
exit 1
fi
NDIGITS=$2
PREFIX=$3
#only dir provided (set default)
if [ $# -eq 1 ]; then
NDIGITS="04"
PREFIX=""
fi
#only dir+ndigits
if [ $# -eq 2 ]; then
PREFIX=""
fi
#confirm
read -p "Are you sure? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
# add your extensions here if necessary
find $1 -name '*.gif' | cat -n | while read n f; do mv -n "$f" `printf "%s%s%${NDIGITS}d.gif" $1 "$PREFIX" $n`; done
find $1 -name '*.png' | cat -n | while read n f; do mv -n "$f" `printf "%s%s%${NDIGITS}d.png" $1 "$PREFIX" $n`; done
find $1 -name '*.jpg' | cat -n | while read n f; do mv -n "$f" `printf "%s%s%${NDIGITS}d.jpg" $1 "$PREFIX" $n`; done
find $1 -name '*.webm' | cat -n | while read n f; do mv -n "$f" `printf "%s%s%${NDIGITS}d.webm" $1 "$PREFIX" $n`; done
fi
#print newline for prettiness
echo
# Rename all the files specified in a directory to a sequential order.
# WARNING: If the files are already named sequentially, backups will
# be created with a tilde (~) appended at the end.
# A workaround for this is to run rename.sh with another value for
# the ndigits and then run it again with the original.
#
# /robex/ - 2018
#no arguments
usage() {
echo "usage: ./rename.sh -npd dir"
echo "args:"
printf "\tdir: directory to rename files inside of\n"
printf "\t-n: ndigits (optional), number of digits in the format (ex: 0001.gif),\n\t USE TWO DIGITS. default = 04\n"
printf "\t-p: prefix (optional), add a prefix before the digits\n"
printf "\t-d: dry-run (optional), show the changes, dont execute\n"
exit 1
}
NDIGITS="04"
PREFIX=""
DRYRUN=0
while getopts ":n:p:hd" opt; do
case $opt in
n)
NDIGITS=$OPTARG
;;
p)
PREFIX=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
h)
usage
;;
d)
DRYRUN=1
;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
usage
exit 1
fi
# add your extensions here if necessary
exts=(".gif" ".png" ".jpg" ".webm" ".mp4")
if [ $DRYRUN -eq 1 ]
then
for i in "${exts[@]}"; do
find $1 -name "*$i" -o -name "*$i~" | cat -n | while read n f; do printf "%30s renamed to %s%s%${NDIGITS}d$i\n" "$f" "$1" "$PREFIX" $n; done
done
else
#confirm
read -p "Are you sure? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
for i in "${exts[@]}"; do
find $1 -name "*$i" -o -name "*$i~" | cat -n | while read n f; do mv -b "$f" `printf "%s%s%${NDIGITS}d$i" $1 "$PREFIX" $n`; done
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment