Skip to content

Instantly share code, notes, and snippets.

@hpohlmeyer
Last active April 25, 2023 01:42
Show Gist options
  • Save hpohlmeyer/18bbc6790af6bb514758 to your computer and use it in GitHub Desktop.
Save hpohlmeyer/18bbc6790af6bb514758 to your computer and use it in GitHub Desktop.
Useful mac terminal commands

Useful Terminal commands

Table of Contents

Change system behavior

Disable screenshot shadow

This command disables the default shadow if you take a screenshot of a window. Especially if you take a screenshot, using + + 4 + Space, this comes in handy.

# Disable Shadows
defaults write com.apple.screencapture disable-shadow -bool TRUE
killall SystemUIServer

# Enable Shadows
defaults write com.apple.screencapture disable-shadow -bool FLASE
killall SystemUIServer

Change screenshot location

Avoid Desktop Cluttering by putting your Screenshots in its own dedicated folder. Replace ~/Desktop/Screenshots with your folder location.

defaults write com.apple.screencapture location ~/Desktop/Screenshots
killall SystemUIServer

Enable text selection in QuickLook

This command lets you select test directly from QuickLook to quickly copying text from a file without opening it. However, some files do not let you copy text from it.

# Enable text selection
defaults write com.apple.finder QLEnableTextSelection -bool TRUE
killall Finder

# Disable text selection
defaults write com.apple.finder QLEnableTextSelection -bool FALSE
killall Finder

Remove “Open with …” entries

Sometimes your “Open with …” menu can be cluttered by the same application multiple times. To remove the duplicates, simply use this command.

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Prevent Apps from saving to iCloud by default

# Set save location to HD
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool FALSE

# Reset save location to iCloud
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool TRUE

View hidden files

# Show hidden files
defaults write com.apple.finder AppleShowAllFiles -bool TRUE

# Reset
defaults write com.apple.finder AppleShowAllFiles -bool FALSE

Control the system from the terminal

Shutdown, sleep or restart your Mac

All of these actions use the shutdown command with different flags. -r is for restart, -h is for shutdown (halt) and -s is for sleep. All of these commands take a specific time. You can use the keyword now to execute the command immediately, +number to restart in the given amount of minutes relative from now, or you can define a specific date/time in the format yymmddhhmm, while year, month and day default to the current system values.

# Put mac to sleep in 60 minutes from now
sudo shutdown -s +60

# Shutdown now
sudo shutdown -h now

# Restart on 16th March 2015 at 16:20 (yymmddhhmm)
sudo shutdown -r 1503161620

# Restart today at 17:20
sudo shutdown -r 1720

Wake your Mac from sleep

If your mac is in sleep mode, you can schedule a wake up call at a given time.

# Wake up your mac from sleep on 1st January 2017 at 20:00
pmset schedule wake "01/01/2017 20:00:00"

Prevent your mac from going to sleep

Sometimes you need to prevent your mac to go to sleep. Instead of altering your energy saving settings, you can just use the caffeinate command. If just type in caffeinate without any flags, it will run until you cancel it with Ctrl + C

# Start imediately, cancel with Ctrl + C
caffeinate

# Prevent to sleep for 600 seconds (10 Minutes)
caffeinate -u -t 600

Clear (overwrite) empty space

Caution: Avoid using this on SSDs. They have a limit number of reads/writes to a disk, so this can drastically shorten its lifespan. If you need to use it, keep the level as low as possible. If you delete a file, it is flagged free for overwriting. This means it is still on the HD until the place is needed and the file will be overwritten. This means, special tools may be able to recover the files, that are not overwritten, which is a bad thing if you want to sell your mac. Use this command to overwrite the free space with empty data to ensure that nobody can recover the contents of your HDD. The number after freespace defines the level of security. You can choose one of the following:

0 - Single-pass zero-fill erase.
1 - Single-pass random-fill erase.
2 - US DoD 7-pass secure erase. (US Department of Defense's standard)
3 - Gutmann algorithm 35-pass secure erase. (Overwrite files 35 times)
4 - US DoE algorithm 3-pass secure erase.
# Overwrite free space on Macintosh HD 7 times
diskutil secureErase freespace 2 "/Volumes/Macintosh HD"

File Handling

Create a dummy file

Creates a file with a specific size (1mb in this case). You can use any extension you want.

mkfile 1m test.tmp

Base64 encode any file

You can base64 encode any file with this command. It creates a copy of the file with a new name in the same destination.

base64 lato-regular.ttf -o latoregular-base64.txt

Backup a folder

This is a simple backup method using rsync. The BackupFolder gets copied from the Volume SourceVolume to the Volume TargetVolume. The --delete flag removes all files that have been deleted on the source folder. The -aE flag makes sure permissions and metadata are included in the copy.

rsync -aE --delete "/Volumes/SourceVolume/BackupFolder" "/Volumes/TargetVolume"
@christopheranderton
Copy link

Enable text selection in QuickLook Don't work anymore in El Capitan (very annoying). Also, Prevent Apps from saving to iCloud by default don't work on some Apps (you can use plistbuddy on the individual apps and look for similar defaults and change them. In Terminal, use man plistbuddy to see options and usage. Quit the man page with Q).

Cheers!

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