Tuesday, December 25, 2018

Image Rotation in Bulk

We have been having problems with rotating images.  All the software so far rotate correctly on our system or at least it seems so but they all leave the metadata intact.  Here is how I rotated in bulk with the assumption that i grouped all the wrong ones that needed rotation of the same orientation.

Use exiftool to accomplish this task
Navigate to the directory of all the wrong images that needed rotation
cd needRotate90CW
# Remove all metadata from the images
exiftool -all= *.jpg# Rotate them 90 degrees counter clockwise and overwrite
exiftool -orientation="Rotate 90 CW" -overwrite_original *.jpg

The above seemed to work but did not change the metadata to the width and height.  It only rotated.

SOLUTION! 
I found the easiest method is to use Nautilus file manager and 
Nautilus Image Converter

Monday, December 10, 2018

Find Differences Between Two Files Using Bash

grep -v -F -x -f file1 file2
The problem with the above command is that you dont know which way is the differences.  Here is a better way to accomplish this.  I have a directory of ID cards named MASTER_ID and is mounted remotely.  I host a local MYSQL database with all the accounts.  The problem we are trying to solve is move all the ID cards that are no longer in the database to the !Graveyard directory for consideration of permanently removal by a human.

if [[ $EUID -ne 0 ]]; then
 echo "This script must be run as root"
 exit 1
fi

MASTER_ID_DIR="/home/projects/mount_directory/srvr-file/SchoolPhotos/MASTER_ID/"
MASTER_ID_LST="/tmp/master_id.lst"
CAS_LST="/tmp/cas.lst"

# Generate list of all users within CAS by ID
if [ -f $CAS_LST ]; then
 rm $CAS_LST
fi
mysql -u cas_god -p <dbpasswd> -D cas -e "select id_number from cardinfo_cardinfo into outfile '/tmp/cas.lst'"

# Get listing of MASTER_ID regardless of the file extension case
echo "" > $MASTER_ID_LST
for x in `ls $MASTER_ID_DIR`; do
 echo $(basename $x .[Jj][Pp][Gg]) >> $MASTER_ID_LST
done

# Find extras in MASTER_ID that is NOT in CAS
for y in `cat $MASTER_ID_LST`; do
 if [ `grep $y $CAS_LST|wc -l` -eq 0 ]; then
   echo mv $MASTER_ID_DIR${y}.* $MASTER_ID_DIR!Graveyard/${y}_delete.jpg
   mv $MASTER_ID_DIR${y}.* $MASTER_ID_DIR!Graveyard/${y}_delete.jpg
 fi
done

Monday, December 3, 2018

Find Digit in Filename & Bulk Rename

Extracted from StackOverflow: https://stackoverflow.com/questions/428109/extract-substring-in-bash
Generic solution where the number can be anywhere in the filename, using the first of such sequences:
number=$(echo $filename | egrep -o '[[:digit:]]{5}' | head -n1)
Another solution to extract exactly a part of a variable:
number=${filename:offset:length}
If your filename always have the format stuff_digits_... you can use awk:
number=$(echo $filename | awk -F _ '{ print $2 }')
Yet another solution to remove everything except digits, use
number=$(echo $filename | tr -cd '[[:digit:]]')
I need to loop through the directory and take the filename and crop off the name assuming that the first 6 numbers are the ID number then change it to the final form.  I had to ignore all the spacing and junk after the number and just made the assumption that all I needed was the numbers in front of the file.

find . -print0 | while read -d $'\0' file; do number=$(echo $file | egrep -o '[[:digit:]]{6}' | head -n1); mv ${number}* ${number}.jpg ; done

I had some problems with files with the hyphen.

ls > /tmp/list

I needed to export a list of file then manipulate with VIM, keeping only the first work in each line then use the file list to iterate through

:1,$s/\(\s*\w\+\).*/\1/g

for x in `cat /tmp/list`; do  number=$(echo $x | egrep -o '[[:digit:]]{6}' | head -n1); mv ${number}* ${number}.jpg; done 



Sunday, December 2, 2018

Bulk Rename of Filename

Bulk change of file extension.  For an example, change the JPG to jpg

for x in `ls *JPG`; do mv $x $(basename $x .JPG).jpg; done

To find all basename case insensitive

$(basename $x .[Jj][Pp][Gg])