A bash script multi-tool and easy editable

Submitted by Nicola Rainiero on 2013-05-20 (last updated on 2013-05-23)

I happen very often to convert/resize/rename a directory of files, so sometimes I can fix manually file by file or if there are many of the ones, I can modify an existent bash for accelerating the process. However it is a waste of time the changing the whole file to adapt it at the new commands (hazards are always lurking!).

Therefore I have created a kind of bash template that it is easy to setup and adapt it according to what required. It also replaces all spaces with the "underscore" symbol, exits if any statement returns a non-true return value and it has a debug flag to test it before making some irreversible mistakes.

I believe that two examples may help to understand how it works then I publish it. But first I put the main references which helped me:

Example 1: mogryfy

Goal: scale a directory of JPG images reducing the quality

# SETTING START
COMMAND="mogrify"
EXTENSION_IN="jpg" # initial extension
EXTENSION_OUT="" # final extension
FLAG_B="-resize 400x400 -quality 40"
FLAG_M=""
FLAG_F=""
RM_FILE=false # if true, delete initial file after executing command
DEBUG=false # if true, print statement without executing any command
OPTION="2"

Example 2: avconv

Goal: convert a directory of WEBM movies in AVI format at the high quality (deleting the original ones)

# SETTING
COMMAND="avconv"
EXTENSION_IN="webm" # initial extension
EXTENSION_OUT="avi" # final extension
FLAG_B="-i"
FLAG_M="-b 2048k"
FLAG_F=""
RM_FILE=true # if true, delete initial file after executing command
DEBUG=false # if true, print statement without executing any command
OPTION="1"

The bash converter programmable script

It is a beta version and a working in progress script, but it works quite well, if you have some hint, correction or any idea to improve it, please let your comment. How does it work?

  • Just copy it and save in a text file with the .sh extension;
  • remember giving it the execute permission;
  • finally you can launch in the following manner from the terminal: ./filename.sh directly in the target directory or adding the correct path if it is different from the working folder.

Here it is the source:

#!/bin/bash
#
# SETTING:
COMMAND="avconv"
EXTENSION_IN="webm" # initial extension
EXTENSION_OUT="avi" # final extension
FLAG_B="-i"
FLAG_M="-b 2048k"
FLAG_F=""
RM_FILE=false # if true, delete initial file after executing command
DEBUG=false # if true, print statement without executing any command
OPTION="1"
#
# Description of the options:
#
# [0] just clean the file.EXTENSION_IN:
# replace empty space with "_", remove "(", ")", "-"
# [1] like 0 and execute:
# COMMAND FLAG_B file.EXTENSION_IN FLAG_M file.EXTENSION_OUT FLAG_F
# delete file.EXTENSION_IN if RM_file true
# [2] like 0 and execute:
# COMMAND FLAG_B file.EXTENSION_IN
# [3] a commented template
#
#

# Check if the user specifies a destination directory
if [ -n "$1" ]
then
directory=$1 # use this new working directory
else
directory=$PWD # use the same directory where you have launched the script
fi

eval cd $directory
OLDIFS=$IFS
IFS=$'\n'

for file in `ls *.${EXTENSION_IN}`
do
newfilename=`echo $file | sed 's/ /_/g'` # change empty spaces with "_"
newfilename=`echo $newfilename | sed 's/(//g;s/)//g'` # remove "(" and ")"
newfilename=`echo $newfilename | sed 's/-//g'` # remove "-"

# Remove the extension
newfile=${newfilename%.*}
newfile=`echo $newfile | sed 's/\./_/g'` # if there are some ".", replace them with "_"
if [ "$DEBUG" == true ]; then echo -e "Remove extension and other dots:\n$newfile"; fi

if [ "$DEBUG" == true ]; then echo "--------------------"; echo -e "Original file:\n$file"; \
echo -e "After cleaning:\n$newfile.$EXTENSION_IN"; fi

if [ "$file" != "$newfile.$EXTENSION_IN" ]
then
if [ "$DEBUG" == true ]; then echo -e "Rename file:\nmv $file $newfile.$EXTENSION_IN"; \
else mv $file $newfile.$EXTENSION_IN;fi
fi

case "$OPTION" in
0)
if [ "$DEBUG" == true ]; then echo -e "Nothing to do!"; fi
;;
1)
if [ "$DEBUG" == true ]
then
echo -e "Execute:\n$COMMAND $FLAG_B $newfile.$EXTENSION_IN $FLAG_M $newfile.$EXTENSION_OUT $FLAG_F";
else
eval $COMMAND $FLAG_B $newfile.$EXTENSION_IN $FLAG_M $newfile.$EXTENSION_OUT $FLAG_F \
|| { echo "$COMMAND failed! Check again the script"; cd $PWD; exit 1; }
fi

if [[ $RM_FILE == true && $DEBUG == true ]];
then echo -e "Delete:\nrm $newfile.$EXTENSION_IN"; fi
if [[ $RM_FILE == true && $DEBUG == false ]];
then eval rm $newfile.$EXTENSION_IN; fi
;;
2)
if [ "$DEBUG" = true ]
then
echo -e "Execute:\n$COMMAND $FLAG_B $newfile.$EXTENSION_IN"
else
eval $COMMAND $FLAG_B $newfile.$EXTENSION_IN \
|| { echo "$COMMAND failed! Check again the script"; cd $PWD; exit 1; }
fi
;;
# 3) # Add a new option using this template
# if [ "$DEBUG" == true ]
# then
# echo -e "Execute:\n((NEW COMMAND STATEMENT))"
# else
# eval ((NEW COMMAND STATEMENT)) \
# || { echo "$COMMAND failed! Check again the script"; cd $PWD; exit 1; }
# fi
# if [[ $RM_FILE == true && $DEBUG == true ]];
# then echo -e "Delete:\nrm $newfile.$EXTENSION_IN"; fi
# if [[ $RM_FILE == true && $DEBUG == false ]];
# then eval rm $newfile.$EXTENSION_IN; fi
# ;;
esac
done

# Return in the working directory
cd $PWD
exit 0



Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.