Humble start of Bash scripting (part 3)

In Part III of my Bash scripting adventures I’ll write a script rename all .jpg files in a folder.

My third post on Bash scripting. This time I used a loop in the script. It’s still a simple script but also a very useful one. I regularly import photos from my digital camera or from my smartphone. The file names of these photos are either a number or a random string of characters. I like to rename these photos to give them a more meaningful name for instance summer2018.

The script lets me choose a folder and next asks for a file name. Then the script determines whether the folder exists and if it does it copies all .jpg files and gives the copied files a new name and a number starting with 0 and increasing the number with one for every photo in the folder. The files are copied so the old files still exists just in case a mistake was made. If I’m satisfied I delete the original files manually.

Question: I’d rather have a file name number with three decimals starting from 000 but I haven’t got I clue how to do that. Any ideas?

If you want to use this script, copy the code below and paste it into an editor like vim, vi or Geany. Save it e.g under the name rename and make this file executable by typing.

chmod 755 rename

To run the script just type.

./rename

Cheers

The script

  1 #!/bin/bash
  2 # written by Eric Buijs 29 januari 2019
  3 # rename changes all the jpg files in a user specified folder
  4 
  5 counter=0
  6 
  7 read -p 'type the name of the folder here (e.g: /Users/myname/Documents): ' dname
  8 
  9 read -p 'type the new name of the files (without extension): ' fname
 10 
 11 if [ -d $dname ]
 12 then
 13   echo directory exists
 14   cd $dname
 15   echo $dname
 16   files=$dname/*.jpg
 17   for file in $files
 18   do
 19     cp $file $dname/$fname$counter.jpg
 20     ((counter++))
 21   done
 22 else
 23   echo directory does not exist
 24 fi
 25 
 26 echo 'All done'

Click here to see my previous Bash post.

Humble start of Bash scripting (part 2)

In Part II of my Bash scripting adventures I’ll write a script to resize an image.

As a follow up on my Bash scripting efforts that I started this year I wrote a script that resizes a photo to a specified width in pixels. The script should work both on Linux and OSX (tested on OSX, will test on Linux later). For this script to work ImageMagick needs to be installed. The script tests if an argument is specified (the original photo of course), if ImageMagick has been installed and if the file output.jpg exists. If output.jpg exists the user gets the option to overwrite the existing file or exit the script. Finally the user needs to specify the width in pixels of the output.jpg.

If you want to use this script, copy the code below and paste it into an editor like vim, vi or Geany. Save it e.g under the name resize and make this file executable by typing.

chmod 755 resize

Now if the photo is in the same folder as the script just type.

./resize yourphoto.jpg

Cheers.

The script

#!/bin/bash
# written by Eric Buijs 12 january 2019

if [ $# -eq 0 ]
	then
		echo You need to specify a file.
		echo e.g: resize photo.jpg
		exit 1
fi

if [ ! $(which convert) ]
	then
		echo This script requires Imagemagick!
		echo You can download it at http://www.imagemagick.org/
		exit 1
fi

if [ -e output.jpg ]
	then
	echo output.jpg already exists.
	read -p "Do you want to overwrite it? (Y/N) " answer
	echo $answer
	if [ $answer = "N" ]
		then
		exit 1
	fi
fi

read -p "What is the desired width in px: " width
convert $1 -resize $width output.jpg

Humble start with Bash scripting

2019 is a good year to start scripting with Bash

To kick off the year I started to learn Bash scripting, something I wanted to do a very long time. I humbly began with tutorials on the web like this one and I’m rewriting scripts from Smokey01, a fanatic Puppy Linux user. As an exercise I simply rewrote this script to work on OSX. If you want to use it you need to install exiftool. I installed exiftool with Homebrew and typed brew install exiftool but there’s also a dmg file available. If you don’t use Homebrew you do have to change the check if exiftool is installed.

Now for the script. It reads a jpeg file that must contain geocoordinates. After some checks for parameter and exiftool installed it reads the geocoordinates and stores them in the variable coord. This variable is then added to a Google search query and the result displayed in the browser.

Save the script e.g. with the name place and run with place /path/to/photo.jpg to display the location where photo.jpg was taken.

The script

1 #!/bin/bash
2 # Originally written by smokey01 28 May 2017
3 # Rewritten for OSX by Eric Buijs 9 Jan 2019
4 if [ $# -eq 0 ]
5 then
6 echo You need to specify a file.
7 echo EG: place photo.jpg
8 exit 1
9 fi
10
11 if [ ! -d /usr/local/Cellar/exiftool ]
12 then
13 echo “This script requires exiftool!”
14 exit 1
15 fi
16
17 coords=`exiftool -n -p ‘$GPSLatitude,$GPSLongitude’ $@`
18 read -e -p “Do you want to see the location in your Browser? ” choice
19 [[ “$choice” == [Yy]* ]] && open https://www.google.com/search?q=$coords || exit 1