BASH File Manipulation

From Indie IT Wiki
Jump to: navigation, search

HOWTO: Create Directory From Photo Creation Date

sudo aptitude install libimage-exiftool-perl

YEAR/MONTH/DAY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F' ' '{print $4}' | tr ':' '/');
echo "${IMGDATE}";
mkdir -p "~/Pictures/Photos/${IMGDATE}"

YEAR/MONTH

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
echo "${IMGDATE}";
mkdir -p "~/Pictures/Photos/${IMGDATE}"

Tweaks...

HOWTO: TEXT FILES:

HOWTO: Convert Text To Lowercase

echo "FISH" | tr '[:upper:]' '[:lower:]'
cat file.txt | tr '[:upper:]' '[:lower:]'

View A Text File With NO Word Wrap

less -S file.txt

Remove Blank Lines

sed -i '/^$/d' file.txt
or
cat myfile.txt |sed '/^$/d'

Convert Tabs To Spaces In a File

expand file.txt

COPY FILE TO NEW DIRECTORY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="~/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
cp -a -v "${IMG}" "${IMGDIR}"

MOVE FILE TO NEW DIRECTORY

IMG=~/Pictures/PhotosFromPhone/IMG_20150924_100801.jpg;
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="~/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
mv -v "${IMG}" "${IMGDIR}"

COMPLETE WORKING SCRIPT

#!/bin/bash
PHOTOS=/home/paully/Pictures/PhotosFromPhone/*.jpg;
for P in ${PHOTOS}
do
IMG=${P};
IMGDATE=$(exiftool "${IMG}" | grep -m1 'Create Date' |awk -F':' '{print $2"/"$3}' |tr -d ' ');
IMGDIR="/home/paully/Pictures/Photos/${IMGDATE}";
mkdir -p "$IMGDIR";
cp -a -v "${IMG}" "${IMGDIR}"
done

Thanks - http://stackoverflow.com/questions/32062159/how-retrieve-the-creation-date-of-photos-with-a-script

HOWTO: Show The Resolution / DPI Of An Image

identify -verbose photo.jpg |grep 'Resolution'

Resolution: 72x72

Add A Zero To The Beginning Of A Filename

$ renamexm -t -v -s/^/0/r [0-9] *.mp3
rename 1 Subdivisions.mp3   => 01 Subdivisions.mp3  : tested
rename 2 The Analog Kid.mp3 => 02 The Analog Kid.mp3 : tested
rename 3 Chemistry.mp3      => 03 Chemistry.mp3     : tested
rename 4 Digital Man.mp3    => 04 Digital Man.mp3   : tested
rename 5 The Weapon.mp3     => 05 The Weapon.mp3    : tested
rename 6 New World Man.mp3  => 06 New World Man.mp3 : tested
rename 7 Losing It.mp3      => 07 Losing It.mp3     : tested
rename 8 Countdown.mp3      => 08 Countdown.mp3     : tested

Delete Characters On Just The First Line

This deletes all the quotation (") characters on just the first line of a CSV file...

sed '1s/"//g' /tmp/oldfile.csv > /tmp/newfile.csv

http://sed.sourceforge.net/sed1line.txt

HOWTO: Mass File Rename Deleting Part Of Name

e.g.

Scooby_Doo_Mystery_Incorporated_S02E25 Through The Curtain 720p x264.mp4 => Scooby_Doo_Mystery_Incorporated_S02E25.mp4

Do...

renamexm -v -s/" .*.mp4"/".mp4"/e *.mp4

HOWTO: Rename All Files Add File Extension

for f in *; do mv "$f" "$f.jpg"; done

HOWTO: Find Out Length Of Longest Line In A File

wc -L file

HOWTO: Find Line Exceeding N Characters

grep '.\{N\}' file

HOWTO: Trim Leading White Space

sed 's/^ *//g'

e.g.

lynx -nolist -dump http://wiki.indie-it.com/wiki/Television_Paully |sed -n '/What I Am Currently Watching/,/What I Have Been Watching/p' |sed 's/^ *//g' |egrep '^[0-9]'

Thanks to Stackoverflow.

HOWTO: Pattern Match Between 2 Lines

I Love Linux
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****
I Love Linux
sed -n '/BEGIN/,/END/p'

Thanks to Shell Hacks]