SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # 20190811-19 | tatze
  3. #   crawl through the current and its subdirectories and create
  4. #   smaller pictures by reducing height/width and quality;
  5. #   save them as a copy inside "./reduced" with subfolder structure;
  6. #   reduced files have the original's modified date
  7. # ------------------------------------------
  8. width=2220
  9. quality=90
  10. # ------------------------------------------
  11. global_path=~/Pictures/_reduced/
  12. local_path='reduced/'
  13. # ------------------------------------------
  14.  
  15. # bold or normal?
  16. bold=$(tput bold)
  17. normal=$(tput sgr0)
  18.  
  19. printf "\n${bold}Where shall the reduced images be created? ${normal}
  20. 1. locally ($local_path) ${bold}[default]${normal}
  21. 2. globaly ($global_path)\n${bold}
  22. [?] : ${normal}"
  23. read RP
  24.  
  25. case $RP in
  26.   2)
  27.     printf "using $global_path ${normal}\n\n"
  28.     path=$global_path
  29.     pth=$(basename $PWD)/
  30.     path+=$pth
  31.     ;;
  32.   *)
  33.     printf "using $local_path ${normal}\n\n"
  34.     path=$local_path
  35.     ;;
  36. esac
  37.  
  38. if [ ! -d $path ]
  39.   then
  40.   printf "${bold}creating folder $path ${normal}\n"
  41.   mkdir -p -- $path
  42.   if [ ! -d $path ]
  43.     then
  44.     printf "${bold}creating folder $path FAILED, aborting script.${normal}\n"
  45.     exit 1
  46.   fi
  47. fi
  48.  
  49. # find all jp[e]g s, recursively, excluding the "*_reduced"
  50. find . -iregex '.*\.jpe?g$' -not -iregex '.*_reduced\.jpe?g$' -not -path "$path/*" -print0 | sort -z |
  51. while IFS= read -rd '' file; do
  52.   dn=$(dirname "$file")
  53.   rd="$path"
  54.   # get rid of that ./
  55.   rd+=$(sed 's|^./||' <<< $dn)
  56.   if [ ! -d "$rd" ]
  57.     then
  58.     printf "\n${bold}creating folder $rd ${normal}\n"
  59.     mkdir "$rd"
  60.   fi
  61.  
  62.   bn=$(basename "$file")
  63.   bn="${bn%.*}_reduced.${bn##*.}"
  64.   if [ ! -f "$rd/$bn" ]
  65.   then
  66.     convert "$file" -resize $width -verbose -quality $quality "$rd"/"$bn"
  67.     # preserve modified timestamp  
  68.     touch -r "$file" "$rd"/"$bn"
  69.   else
  70.     printf "$rd/$bn already exists, skipping.\n"
  71.   fi
  72. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top