Skip to content

Instantly share code, notes, and snippets.

@unacceptable
Last active March 27, 2017 20:57
Show Gist options
  • Save unacceptable/43a3aa5ef321f88bfa577500de8a6b93 to your computer and use it in GitHub Desktop.
Save unacceptable/43a3aa5ef321f88bfa577500de8a6b93 to your computer and use it in GitHub Desktop.
This is a was one of my first bash scripting projects: using cron and bash script to move photos to appropriately named directories under ~/Pictures. Please don't judge this too hard. I need to rewrite this in a better fashion at some point.
#!/bin/bash
set -e
# Writen by: Robert J.
##### Recommended Crontab:
##########################
# * * * * * ~/Applications/Startup/Desktop-Cleanup.sh
# Here is a fish for your viewing pleasure:
# ..ZM$,..
# .7N?.+DM7..
# . ... .... . . ... +N+ .. ,NN.. .. ...... . .
# ..NDDNDI.... ... ... .IN:...,+$8DDDDDDDDDD$I~.. .... .
# ..N,.. $DNI. ....:+=,. .,:?ON+NND8Z?~...... ...:+$8DNNNZ~, .....
# ..N? .. .7NMO=:D8?IZDMDO$+:. . .... . . .. . .. :$DNN7,.... .
# . OZ ... ...ONZZ=.. ... ....... .... . ... I8.. .+DN8~...
# . ?8 .. ..:M:. ..... . .......:NO. ..II7.:8NO. .
# . $8... . :N,.. ... .. ....DI....~,7.=. .+N+
# ..N+. .. . .ONOMM8I++. . .....:$NMNMNNM......=...ONNN. ..
# .N, . .:7MM87~,7NZ,,7N? . . . . .. MO~....:.+N8... ..,~=?$M. .
# ...N+.:8MD+. ....7D. . ,NNI~.... . . .. ~NMN8NMZ...=NNONNMO7I=~.. .
# ..?NND:. .. .~D.....DN?$ONNNNN...OOOOZ777I++=7$$O88DNN8 , .... .
# ... .. . . ....N~..DN=.. .:N,..IN+....~~~~~~~~~:..... . .......
# . .. . .O88N+. . ....N?.MD.... . . . . .. . . .
# ....... .. .Z7 . .
# Configured to lapse for 2 second every minute to account for command runtime,
# and prevent crontabs from stacking up and utilizing excess resources
# I should probably use a named pipe as a lock file for this portion.
for ((i=1; i<58; i++));
do
# Check for new screenshots (only returns one value at a time)
file=$( find "$HOME/Desktop/" -name "Screen*png" | awk 'NR == 1' );
# Get file name if new screenshot exists on desktop
[[ -z $file ]] || {
name=$( printf "%s" "$file" | awk -F "/Desktop/" '{print$2}' | sed 's/ /-/g');
# Verify directory's existance
[[ -d "$HOME/Pictures/Temp" ]] || {
# make directory if one does not exist
mkdir "$HOME/Pictures/Temp";
};
# Rename the file to not contain spaces if the file variable is not null
mv "$file" "$HOME/Pictures/Temp/$name";
};
# Set variable for moving of files in temp
dfile=$( find "$HOME/Pictures/Temp/" -name "Screen*png" | sort -r | awk 'NR > 5' | awk 'NR == 1' );
# Do nothing if dfile does not exist
[[ -z $dfile ]] || {
# New directory that screenshots are archived to ( MM-DD-YYYY )
newdir=$( printf "%s" "$dfile" | awk -F "-" '{print($4,$5,$3)}' | sed 's/ /-/g' );
# Verify directory's
[[ -d "$HOME/Pictures/$newdir" ]] || {
# make directory if one does not exist
mkdir "$HOME/Pictures/$newdir";
} && {
# check directory's existence and move to dated directory
mv "$dfile" "$HOME/Pictures/$newdir/";
};
};
sleep 1;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment