Skip to content

Instantly share code, notes, and snippets.

@kaisermann
Last active January 1, 2024 06:09
Show Gist options
  • Save kaisermann/3f7ecf5237dbcaa721465dd5c2cf993d to your computer and use it in GitHub Desktop.
Save kaisermann/3f7ecf5237dbcaa721465dd5c2cf993d to your computer and use it in GitHub Desktop.
Useful CLI

MacOS exclusive

  • Open and bring to front a specific app
osascript -e "tell application \"APP\" -e "activate" -e "end tell"
osascript -e 'tell app "Terminal" to activate'
osascript -e 'tell app "Terminal" to do script "cmatrix"'
  • Managing login items
# Add
osascript -e 'tell application "System Events" to make login item at end with properties {name: "Notes",path:"/Applications/Notes.app", hidden:false}'

# Alternative Add
defaults write loginwindow AutoLaunchedApplicationDictionary -array-add '{ "Name" = "Notes" ; "Path" = "/Applications/Notes.app"; "Hide" = 0; }'

# Delete
osascript -e 'tell application "System Events" to delete login item "itemname"'

# List
osascript -e 'tell application "System Events" to get the name of every login item'

Renaming

  • Rename files with media creation date with file creation date as fallback.

exiftool -d '%Y-%m-%d_%H.%M.%S%%-2c%.%%e' '-filename<fileModifyDate' '-filename<CreateDate' '-filename<MediaCreateDate' '-filename<DateTimeOriginal' *.EXTENSION

  • Fix other Data tags for instagram videos. Used after Fix video media create date hour (timezone)

exiftool -overwrite_original_in_place '-TrackCreateDate<MediaCreateDate' '-TrackModifyDate<MediaCreateDate' '-MediaModifyDate<MediaCreateDate' *.mp4

exiftool -overwrite_original_in_place '-VideoDates<FileModifyDate' *.mp4

  • Lowercase extensions
#!/bin/bash
for f in *;
 do mv "$f" "$f.tmp";
 mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`";
done;

Resizing

  • Resize imagens bigger than ...

mogrify -thumbnail 'MAXWIDTHxMAXHEIGHT>' *

Optimizations

  • Lossless optimization of png/jpg (strips metadata) (source)
find . -iname '*.png' -print0 -exec optipng -o7 -strip all  "{}" \;
find -E . -iregex '.*\.(jpg|jpeg|jpe|jif|jfif|jfi)$' -exec jpegoptim --all-progressive --strip-all --strip-iptc --strip-icc "{}" \;

Notes

  • STRIPS METADATA
  • optipng version older than 7 doesn’t work with -strip all, remove it from command in this case.
  • For fast png optimization change -o7 to -o2, compress ratio will be just a bit worse, but it will work more faster
  • Jpeg optimization is lossless, but you can use –max=90 to change compress ratio (100 for lossless compress, less is better compress but worse quality.

Metadata

Note: if using this .exiftool_config, you can switch AllDates for PhotoDates or VideoDates

  • Copy all metadata from one file to another exiftool -overwrite_original_in_place -TagsFromFile FROM.jpg TO.jpg

  • Fix video media create date hour (timezone)

exiftool -overwrite_original_in_place '-AllDates-=2:0:0' '-MediaCreateDate-=2:0:0' '-MediaModifyDate-=2:0:0' *.mp4

  • Set image creation dates based on filename (example is for images generated by instagram stories)

Images generated by instagram:

exiftool -overwrite_original_in_place '-AllDates<${Filename;s/IMG_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})/$1:$2:$3 $4:$5:$6/}' *.EXTENSION

Images generated by dropbox camera upload:

exiftool -overwrite_original_in_place '-AllDates<${Filename;s/(\d{4})-(\d{2})-(\d{2})_(\d{2})\.(\d{2})\.(\d{2})/$1:$2:$3 $4:$5:$6/}' *.EXTENSION

Video

  • Copy metadata from one video to another (source)

ffmpeg -i in.mp4 -i out.mp4 -map 1 -map_metadata 0 -c copy fixed.mp4

  • H.264 batch encoding (not tested THAT much)
find . -type f \( -iname '*.mp4' -o -iname '*.mov' -o -iname '*.mkv' -o -iname '*.m4v' \) -not -path './ffmpeg-output/*' -exec bash -c '
  SRC_PATH=$1;
  TARGET_DIR="$(pwd)/ffmpeg-output/$(dirname "$1")/";
  TARGET_FILENAME="$(basename "${1%.*}").mp4";
  TARGET_PATH="$TARGET_DIR$TARGET_FILENAME";
  
  mkdir -p "$TARGET_DIR";

  ffmpeg \
    -i "$1" \
    -preset slow \
    -c:v libx264 \
    -crf 19\
    -map_metadata 0 \
    "$TARGET_PATH" \
  ;

  touch -r "$SRC_PATH" "$TARGET_PATH";

  echo -ne "\007";
' _ '{}' \;

If you want to convert 1080p to 720p, just add a -vf scale=-1:720 \ parameter to the above ffmpeg call.

Organization

  • Copy specific files to another location while mantaining the same directory structure

rsync -avh --include='*/' --include='*.EXTENSION' --exclude='*' --prune-empty-dirs SRC TARGET

Processes

  • Kill processes by name search

ps aux | grep -i SEARCH | awk '{print $2}' | xargs sudo kill -9

  • Kill processes by name pattern

pkill "PATTERN"

Sync

To mount:

  • sshfs -o debug,loglevel=debug,allow_other,defer_permissions USER@REMOTE:SOURCE TARGET

To unmount:

  • diskutil unmount TARGET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment