Get Under the Hood with Terminal
Here is a collection of command-line tricks for boring, repetitive, and sometimes difficult tasks.
File tricks
show/hide hidden files
defaults write com.apple.finder AppleShowAllFiles true/false killall Finder
Change a file’s modification date to now:
touch file
prevent .DSstore files
defaults write com.apple.desktopservices DSDontWriteNetworkStores true killall Finder
Image manipulation
Rotate image 45 degrees clockwise
sips --rotate 45 image.jpg
Flip image horizontally or vertically
sips --flip horizontal|vertical image.jpg
Crop image to a height of 100px and a width of 150px. fit specified size.
sips --cropToHeightWidth 100 150 image.jpg
Pad image to a height of 100px and a width of 150px, use red as the padding color (default is black).
sips --padToHeightWidth 100 150 --padColor FF0000 image.jpg
Resample image to a height of 100px and a width of 150px. Image apsect ratio may be altered.
sips --resampleHeightWidth 100 150 image.jpg
Resample image to width of 300px.
sips --resampleWidth 300 image.jpg
Resample image to height of 200px.
sips --resampleHeight 200 image.jpg
Resample image so height isn’t greater than 100px and width isn’t greater than 150px.
sips --resampleHeightWidthMax 100x150 image.jpg
Text Trickery
Count number of words in the text in the Clipboard:
pbpaste | wc -w
Sort lines of text in the Clipboard and copy them back to the Clipboard:
pbpaste | sort | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line back to the Clipboard (output is sorted):
pbpaste | sort | uniq | pbcopy
Find duplicate lines from lines of text in the Clipboard and copy only one instance of each duplicate line (stripping non-duplicates) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -d | pbcopy
Strip duplicate lines from lines of text in the Clipboard and copy only one instance of each line (stripping duplicates entirely) back to the Clipboard (output is sorted):
pbpaste | sort | uniq -u | pbcopy
Tidy up HTML in the Clipboard and copy it back to the Clipboard:
pbpaste | tidy | pbcopy
Convert tabs to spaces for the lines in the Clipboard:
pbpaste | expand | pbcopy
Convert spaces to tabs for the lines in the Clipboard:
pbpaste | unexpand | pbcopy