Pages

Some Useful Terminal Commands in OSX

These are some commands for the Terminal in Mac OSX that I have found useful and were, at the time, difficult to locate on the Internet. Often, instructions for shell commands are specific to the Linux environment but one must use a different command to complete the same function in OSX; hence, some of the commands below are specific to OSX.

# Show hidden files by default in the Finder
defaults write com.apple.finder AppleShowAllFiles YES

# OSX stores a list of items you download from the Internet and some information about them. View that list with this command. 
sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'select LSQuarantineDataURLString from LSQuarantineEvent'

# Delete local database of downloads
sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent'

# Prevent computer sleep for 600 seconds
caffeinate -u -t 600

# Get local network IP address
ipconfig getifaddr en0

# Get global IP address
curl ipecho.net/plain; echo

# Set Desktop background
osascript -e ‘tell application “Finder” to set desktop picture to POSIX file “/path/to/picture.jpg”’

# Set the default save format of TextEdit to .txt instead of .rtf
defaults write com.apple.TextEdit RichText -int 0

# Disable throttling on low-priority tasks, set back to '1' to reenable
sudo sysctl debug.lowpri_throttle_enabled=0

# Download all .pdf files from a website
wget -A pdf -m -p -E -k -K -np http://

# Check hardware configuration like 'lscpu' in Linux
sysctl hw

# Mute the Mac startup sound
osascript -e 'set volume without output muted'
osascript -e 'set volume with output muted'

# Monitor CPU temperatures from Terminal
gem install iStats
istats

# Create a tar file with gzip compression
tar -czf file.tar.gz files

# Extract tar files
tar -xvf file.tar

# Get a random number, useful in scripts to initialize variables
echo $RANDOM

# Convert image files to new formats
sudo port install convert
convert image.jpg image.png

# Convert PDFs to DJVU files and vice versa
sudo port install pdf2djvu
sudo port install djvu2pdf
pdf2djvu -o out.djvu in.pdf
djvu2pdf in.pdf

I set the native language on my MacBook to German and, as a result, I have noticed that machines in the United States I need to SSH into (SLAC or MIT computing resources, for example) tend to throw a warning at login or, sometimes, with every command I enter. It is quite annoying and the solution is to add the following to .bash_profile in the home directory.

export LC_ALL=en_US.UTF-8

I have not tested this for other language settings in OSX. However, I imagine this would fix warnings resulting from setting any language (other than English) as the default.