Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Last active March 4, 2019 01:16
Show Gist options
  • Save jongacnik/fd433da96c8e120f6b9a594c34e84b2e to your computer and use it in GitHub Desktop.
Save jongacnik/fd433da96c8e120f6b9a594c34e84b2e to your computer and use it in GitHub Desktop.
bash snippets
  1. zip folder using name of folder (replace folder)
zip -r folder{.zip,}
  1. recursive find and replace in files
# on mac
find . -type f -name '*.txt' -exec sed -i '' 's/oldword/newword/' {} +

# on ubuntu
find . -type f -name '*.txt' -exec sed -i 's/oldword/newword/' {} +
  1. edit cron tab
env EDITOR=nano crontab -e
  1. view current cron tabs
crontab -l
  1. recursive image resize (without upscale)

This technique works but affects colors, alternate approach using sips below

find . -name '*.jpg' -exec mogrify -resize 2000^ {} +

# multiple filetypes
find . -type f \( -name "*.jpg" -or -name "*.png" \) -exec mogrify -resize 2000^ {} +
# single file (resize width)
sips --resampleWidth 500 file.jpg

# single file (resize longer size)
sips -Z 500 file.jpg

The issue is sips will upscale an image if below the input size, which is usually not the desired behavior. We implement a conditional in order to check to see if an image is greater than the desired width first, before doing the resize.

#!/bin/bash

# ./resize-all.sh 500
# will resize all images (recursively), to specified width

find . -type f \( -name "*.jpg" -or -name "*.png" \) |\
  while read FILENAME
  do
    size=($(sips -g pixelWidth -g pixelHeight "${FILENAME}" | grep -o '[0-9]*$'))
    if [[ ${size[0]} -gt $1 ]]; then
      sips --resampleWidth $1 "${FILENAME}"
    fi
  done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment