Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # See http://www.reddit.com/r/bash/comments/2xh32d/how_can_i_make_this_bash_script_better/
- dirs=("16x10" "16x9" "5x4" "4x3");
- # Fills in known ratios -> dirs in an associative array
- declare -A ratios;
- for r in "${dirs[@]}"; do
- ratios[$(echo $r | tr "x" "/" | bc -ql)]="$r"; # 1.77777... = 16x9
- done;
- mkdir -p "${dirs[@]}" "Others"; # Creates directories
- shopt -s nocaseglob # Case insensitive glob
- for file in *.{jpg,jpeg,gif,bmp,png}; do
- if [[ -f "$file" ]]; then # Check if file exists (prevents *.xxx in $file)
- # Current image ratio
- curRatio=$(identify -format "%w/%h" "$file" -quiet | bc -ql 2>&1);
- if [[ ! -z "$curRatio" ]] ; then
- # Move the file in the appropriate dir.
- if [ ${ratios[$curRatio]+_} ]; then # Check if ratio exists
- mv "$file" "${ratios[$curRatio]}";
- else
- mv "$file" "Others/$file";
- fi;
- else
- echo "Got a problem with $file.";
- fi;
- fi;
- done;
- find "${dirs[@]}" "Others" -type d -empty -delete # Delete empty dirs
- exit;
- # ----------------------------------------------------------------------------
- # https://www.reddit.com/r/bash/comments/2xh32d/how_can_i_make_this_bash_script_better/cp0ofc8 (iscfrc)
- mkdir -p 16x10 16x9 5x4 4x3 Others
- declare -A ratio_map=([160]=16x10 [177]=16x9 [125]=5x4 [133]=4x3)
- find . -maxdepth 1 -type f -iregex '.*\.\(jpe?g\|gif\|png\|bmp\)$' -printf '%f\t' -exec identify -format '%[fx:(w/h)*100]' '{}' \; |
- while IFS=$'\t' read -r file ratio; do
- [[ "${ratio_map[${ratio%%.*}]}" ]] && outdir=${ratio_map[${ratio%%.*}]} || outdir=Others
- mv -v "$file" $outdir
- done
- find . -maxdepth 1 -type d -regex '.*/\([0-9]+x[0-9]+\|Others\)$' -empty -print -delete
Add Comment
Please, Sign In to add comment