The Living Thing / Notebooks : Commands I need often but which are tedious to work out

General, not OSX-specific or linux-specific, and see also shell scripting, and the vast online repository, commandlinefu. Or howdoi, the command line command line.

Reduce size of bloated PDF:

.. code-block:: bash
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/ebook
-dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

or, wrapped up into a nice little script, ShrinkPDF (infile, outfile, dpi):

./shrinkpdf.sh in.pdf out.pdf 90

This works to concatenate PDFs:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=output.pdf input*.pdf

Speaking of concatenation, here’s how to concatenate photos into low-framerate movie from IMG_5815 and subsequent sequential shots:

ffmpeg -framerate 3 -start_number 5815 -i IMG_%04d.jpg -c:v libx264 \
  -pix_fmt yuv420p -c:a copy ../outfile.m4v

sync only if drive present:

test -d  /Volumes/syncdrive/ && rsync --delete --iconv=UTF8-MAC,UTF-8 -avz 192.168.0.1:/path/to/stuff/ /Volumes/syncdrive/

replace file extension using bash:

mv "$file" "`basename $file .html`.txt"

or:

mv "$file" "${file%.html}.txt"

Don’t forget the quotes, or it will explode when you have spaces in your filenames and delete stuff that you loved.

IMAP mailboxes full of duplicate nonsense: Use IMAPdedup.

Start ssh-agent on startup, but not if it’s already running: See ` Joseph M. Reagle’s solution <http://mah.everybody.org/docs/ssh>`_:

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

Sensible history search from the shell in .inputrc:

set meta-flag on
set input-meta on
set output-meta on
set convert-meta off

"\ep": history-search-backward
"\en": history-search-backward
"\e[A": history-search-backward
"\e[B": history-search-forward

$if Bash
  Space: magic-space
$endif

Erase all (or most) of the explicit metadata from an image (as per my media metadata page):

exiftool -all= filename.jpg

A shell script to remove trailing whitespace from a file - put this in trimspace.sh:

#!/bin/bash
sed -i '' -e's/[[:space:]]*$//' "$1"

Then you can trim whitespace your… whatever… by putting this line in there:

find . -type f -print0 | xargs -0 -I {} trimspace.sh \{\}