5 Useful Terminal Tricks for Mac Users

Turns out you can do a lot of things faster, easier, and with more room for customization—all by peeking under the hood.

Buried underneath your Mac’s pretty interface is a decades-old backbone called UNIX, and you can use its old-school Terminal for running simple commands that aren’t accessible from the apps or menus. These aren’t just useful for developers and hackers—even if you’ve never delved into the Terminal before, there are a few tweaks that’ll make your Mac better and boost your command line confidence. 

The Terminal app is stored under Applications > Utilities, but you can launch it quickly at any time by pressing Command+Space to open Spotlight, searching for “Terminal,” and pressing Enter.

Set a Shutdown Timer

Here’s an easy one. I sometimes want to keep my computer on for an hour or two while a job finishes (like a big download) and have it shut down when it’s done. To set a timer for shutdown, open a Terminal window and run:

sudo shutdown -h +60

To break down that command, here’s what it does:

  • sudo tells the Terminal to run the following command as a super user, or administrator. You’ll need to enter your password, though it won’t show asterisks when you type. Don’t worry, it’s receiving your keystrokes.
  • shutdown is the main command we’re sending to the system.
  • The -h flag tells it to halt, or shut down. You can replace this with –r if you want to restart, or –s if you want to put the computer to sleep instead.
  • Finally, the +60 is our timer in minutes. The above command tells the system to shut down after 60 minutes, or one hour—but you can replace this number with any amount of time you want. You can also use a specific date and time in the format yymmddhhmm if you want.

To cancel your timer before it’s up, just run:

sudo killall shutdown

… which kills the shutdown process running in the background.

Prevent Your Mac From Falling Asleep

On the other side of the coin, you may want to prevent your Mac from going to sleep using its automatic energy-saving features. In this case, you can just use the “caffeinate” command to set an anti-sleep timer:

caffeinate -u -t 3600

The -u flag tells the system to act as if the user is active (so the display doesn’t go to sleep either), while -t sets a timer for, in this case, 3,600 seconds (or one hour). At that point, your usual energy savings rules will come back into effect.

Show Hidden Files and Folders

In general, most people shouldn’t need to view or edit any hidden files. They’re hidden for a reason: The system needs them, you don’t. But if you find you need to access one for some reason—or you want to hide some secret files of your own—you can run the following command to show hidden files in the Finder:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE

Then, to let those changes take effect, restart the Finder by running:

killall Finder

You can also combine those two commands with some ampersands:

defaults write com.apple.finder AppleShowAllFiles -bool TRUE && killall Finder

(For the sake of simplicity, I’ll do that for the rest of the commands in this list that require restarting a service.)

To hide your own folder or file, you can run:

chflags hidden ~/Dekstop/MySecrets && killall Finder

… replacing ~/Dekstop/MySecrets with the path to your own secret folder or file. (The ~ denotes your home folder, also found at /home/[yourusername].) To make hidden files and folders invisible again, just run the original command with FALSE in place of TRUE.

Customize the Dock

The Dock is an important piece of the macOS interface: You store your most-used shortcuts there, use it to hop between windows, and hide minimized apps you don’t need right now. And while you’ll find some useful tweaks under macOS’ Settings > Dock menu, you can customize it even further with a few terminal commands.

For example, want to add a blank spacer to help organize your apps into groups? Run:

defaults write com.apple.Dock persistent-apps -array-add '{"tile-type"="spacer-tile";}' && killall Dock

Or, if you’d prefer to keep the dock as minimalist as possible, you can hide all apps that aren’t currently running with:

defaults write com.apple.Dock static-only -bool TRUE && killall Dock

If you use Command+H to “hide” apps on the regular, you can even dim their icons in the dock, so you know they’re hidden:

defaults write com.apple.Dock showhidden -bool TRUE && killall Dock

Finally, if you like to show and hide the Dock automatically, you’ve probably noticed there’s a one second delay to that animation—that is, when you mouse over the bottom of your screen, it’ll take about a second before the Dock slides in. To remove this delay, run:

defaults write com.apple.Dock autohide-delay -float 0 && killall Dock

Alternatively, you can change that 0 to a higher number to increase the delay. To go back to the default auto-hide settings, run:

defaults delete com.apple.Dock autohide-delay && killall Dock

Tweak the Way Your Mac Takes Screenshots

Taking a screenshot on a Mac is super easy: Just press Command+Shift+4 to grab a window or portion of the screen. Unfortunately, you don’t have a lot of control over how these screenshots are stored—at least, from the onscreen menus. You can, however, customize things from the Terminal.

If you want to change where screenshots are stored, for example, you can run:

defaults write com.apple.screencapture location ~/Pictures && killall SystemUIServer

Replacing ~/Pictures with whatever folder you want to use. If you want to restore the default behavior, just replace that path with ~/Desktop instead.

Next, you can remove the drop shadows around screenshots with:

defaults write com.apple.screencapture disable-shadow -bool TRUE && killall SystemUIServer

You can bring them back by rerunning that command with FALSE instead of TRUE.

In addition, you can change the file type of those screenshots—which is PNG by default—to something else with:

defaults write com.apple.screencapture type JPG && killall SystemUIServer

You can replace JPG with a few file types, like PDF, if you so choose.

Finally, you can change the default name of the screenshot files with:

defaults write com.apple.screencapture name "mycapture" && killall SystemUIServer

You can replace mycapture with whatever you want the filename to be. With these few commands, you should be able to get your Mac taking screenshots exactly how you want without an extra program.

Watch Star Wars (Yes, Really)

A long time ago, in a terminal far, far away, some enterprising folks recreated the entirety of A New Hope in ASCII. It’s still available in terminals today, and on current versions of macOS, you can run:

nc towel.blinkenlights.nl 23

To watch the story play out in text form. Enjoy.

All Rights Reserved for Whitson Gordon 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.