Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # 20190811-19 | tatze
- # crawl through the current and its subdirectories and create
- # smaller pictures by reducing height/width and quality;
- # save them as a copy inside "./reduced" with subfolder structure;
- # reduced files have the original's modified date
- # ------------------------------------------
- width=2220
- quality=90
- # ------------------------------------------
- global_path=~/Pictures/_reduced/
- local_path='reduced/'
- # ------------------------------------------
- # bold or normal?
- bold=$(tput bold)
- normal=$(tput sgr0)
- printf "\n${bold}Where shall the reduced images be created? ${normal}
- 1. locally ($local_path) ${bold}[default]${normal}
- 2. globaly ($global_path)\n${bold}
- [?] : ${normal}"
- read RP
- case $RP in
- 2)
- printf "using $global_path ${normal}\n\n"
- path=$global_path
- pth=$(basename $PWD)/
- path+=$pth
- ;;
- *)
- printf "using $local_path ${normal}\n\n"
- path=$local_path
- ;;
- esac
- if [ ! -d $path ]
- then
- printf "${bold}creating folder $path ${normal}\n"
- mkdir -p -- $path
- if [ ! -d $path ]
- then
- printf "${bold}creating folder $path FAILED, aborting script.${normal}\n"
- exit 1
- fi
- fi
- # find all jp[e]g s, recursively, excluding the "*_reduced"
- find . -iregex '.*\.jpe?g$' -not -iregex '.*_reduced\.jpe?g$' -not -path "$path/*" -print0 | sort -z |
- while IFS= read -rd '' file; do
- dn=$(dirname "$file")
- rd="$path"
- # get rid of that ./
- rd+=$(sed 's|^./||' <<< $dn)
- if [ ! -d "$rd" ]
- then
- printf "\n${bold}creating folder $rd ${normal}\n"
- mkdir "$rd"
- fi
- bn=$(basename "$file")
- bn="${bn%.*}_reduced.${bn##*.}"
- if [ ! -f "$rd/$bn" ]
- then
- convert "$file" -resize $width -verbose -quality $quality "$rd"/"$bn"
- # preserve modified timestamp
- touch -r "$file" "$rd"/"$bn"
- else
- printf "$rd/$bn already exists, skipping.\n"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement