Skip to content

Instantly share code, notes, and snippets.

@jcchikikomori
Forked from giannis/rename_script.sh
Created June 14, 2019 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcchikikomori/dbf5b48aa30b4b7abc30de04bc12f26c to your computer and use it in GitHub Desktop.
Save jcchikikomori/dbf5b48aa30b4b7abc30de04bc12f26c to your computer and use it in GitHub Desktop.
Renaming script for shell
#!/bin/bash
# this is a renaming script
# @arguments:
# -f filename the filename to add (optional)
# -p path the folder to path to run (optional)
# -r if set runs recuresively
#
# it renames the files inside the folder
# adds a sequential number filename (01).jpg etc
# if no filename specified there is the option to use
# the name of the containing folder as filename
#
# @install:
# $ chmod +x rename_script.sh
# $ ln -s ~/rename_me.sh /usr/local/bin/rename_me
#
function filename_select {
select yn in "Yes" "No"; do
case $yn in
Yes ) FILENAME=${PWD##*/};break;;
No ) echo "Exiting..."; exit;;
esac
done
}
function set_temp_name {
local dir=$1;
local file_count=1;
ls -tUr "$dir" | while read i;
do
ext=$(echo $i | awk -F . '{ if (NF>1) { print $NF } }')
if [ "$ext" = "sh" ] || [ "$ext" = "" ]|| [ -d "$i" ]; then
continue;
else
mv "$dir$i" "$dir$TEMP_NAME$file_count.$ext";
((file_count++));
fi
done;
}
# Batch renaming files in ordered numbers
function rename_folder() {
local filename=$1;
local dir=$2;
local pic_count=1;
local vid_count=1;
local pic_num=;
local vid_num=;
local ext= ;
if [[ -z "$dir" ]]
then
dir=$DIR;
fi
if [[ -z "$SAME_AS_FOLDER" ]] || [[ -z "$filename" ]]
then
filename=$FILENAME;
fi
set_temp_name "$dir"
# remove - from the beginning of the name
filename=$(echo $filename | sed -e "s/-//");
ls -tUr "$dir" | while read i;
do
ext=$(echo $i | awk -F . '{ if (NF>1) { print $NF } }')
ext=$(echo $ext | awk '{print tolower($0)}');
if [ "$ext" = "sh" ] || [ "$ext" = "" ]|| [ -d "$i" ]; then
if [ -d "$dir$i" ] && $RECUR ; then
rename_folder "$i" "$dir$i/";
fi
continue;
else
if [ $ext = 'jpg' ] || [ $ext = 'JPG' ] || [ $ext = 'png' ] || [ $ext = 'PNG' ] || [ $ext = 'tiff' ] || [ $ext = 'TIFF' ] || [ $ext = 'gif' ] || [ $ext = 'GIF' ]; then
pic_num=$(printf "%02d" ${pic_count})
echo "Renaming: \"$dir$i\"" "to: \"$dir$filename ($pic_num).$ext\"";
mv "$dir$i" "$dir$filename ($pic_num).$ext";
((pic_count++));
else
vid_num=$(printf "%02d" ${vid_count})
echo "Renaming: \"$dir$i\"" "to: \"$dir'Video - '$filename ($vid_num).$ext\"";
video_str="Video - ";
mv "$dir$i" "$dir$video_str$filename ($vid_num).$ext";
((vid_count++));
fi
fi
done;
}
# temp name
TEMP_NAME="_rename_temp"$(date +%s);
# default to run on current directory
DIR='./'
# not recursive is the default
RECUR=false
SAME_AS_FOLDER=;
while [ $# -gt 0 ]
do
case "$1" in
-r) RECUR=true;;
-p) DIR='./'$2;;
-f) FILENAME="$2"; shift;;
--) shift; break;;
-h)
echo >&2 \
"usage: rename_script [-p path/]"
exit 1;;
*) break;; # terminate while loop
esac
shift
done
# check fi filename is provided
if [[ -z "$FILENAME" ]]
then
echo "Do you want to use the folder name as filename?"
SAME_AS_FOLDER=true;
filename_select;
fi
rename_folder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment