Last active
September 29, 2021 05:16
-
-
Save zro404/57012cda83a4ba0e98c73715626124b8 to your computer and use it in GitHub Desktop.
Bash Script to Convert and Rename images in a directory !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if a path is given | |
if [[ -z $1 ]]; then | |
line="Provide a path !!!!" | |
echo -e "\e[01;31m$line\e[0m" >&2 # Print in Red to Stderr | |
exit # Stop file execution | |
fi | |
# Check if the given path is a Directory | |
ERR=$(cd $1 2>&1) | |
[ $? -eq 1 ] && echo -e "\e[01;31mProvide and path to a Directory !!!!\e[0m" && exit | |
# cd into the provided directory | |
cd $1 | |
# Prompt to continue | |
read -r -p "Are you sure rename all files in $(pwd) ? [y/N] " CHECK | |
# Check if the user wants to continue | |
if [[ $CHECK = "y" || $CHECK = "Y" ]]; then | |
# Get the number of files in the directory | |
FILE_COUNT=`ls -1 | wc -l` | |
# split ls into an array by newline | |
IFS=$'\n' | |
set -o noglob | |
FILES_IN_DIR=($(ls -1)) | |
# Iterate through a sequence from 0 to the total FILE_COUNT | |
for CURRENT_COUNT in `seq -w 0 $FILE_COUNT`; do | |
CURRENT_FILE=${FILES_IN_DIR[10#${CURRENT_COUNT}]} | |
# if the CURRENT_FILE is a .png | |
if [[ "$CURRENT_FILE" == *".png"* ]]; then | |
# Convert it to a .jpg using ImageMagik | |
convert $CURRENT_FILE $CURRENT_COUNT.jpg | |
# Remove the .png | |
rm $CURRENT_FILE | |
continue | |
fi | |
# if the CURRENT_FILE is a .jpg | |
if [[ "$CURRENT_FILE" == *".jpg"* ]]; then | |
# Rename the file | |
mv $CURRENT_FILE $CURRENT_COUNT.jpg | |
continue | |
fi | |
done | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment