Affiliate Disclosure
If you buy through our links, we may get a commission. Read our ethics policy.

How to get more from Terminal and use it to control your Mac

Last updated

Even without becoming an expert in Apple's Terminal app, you can use simple commands to achieve things your Mac won't let you do in any other way. If you're just getting used to Terminal, AppleInsider has some more tips for you.

While cosmetic changes like altering how fast the Dock appears do speed up your work as well as look good, there is more. Terminal can tell you exactly how long your Mac has been switched on since you last restarted. Or it can more robustly prevent your Mac from sleeping or napping than the Settings option does. And, there is a simple way in Terminal to make your computer automatically restart after its crashed.

Last time we covered the Dock and screengrabs and we also warned you — while you shouldn't be put off using Terminal, you should make certain you are typing or copy/pasting precisely what we tell you. Otherwise, you can create problems that can be hard to undo.

Curiosities

There can be times when knowing exactly how long your Mac has been switched on and running is useful. Even if it's just because you're curious, it only takes a moment to find out.

Open the Terminal app and type or paste exactly this:

uptime

It's 4 hours 39 minutes since this Mac was restarted It's 4 hours 39 minutes since this Mac was restarted

Maybe you're finding that your Mac turns itself off when you don't want it to. You may have set a sleep time in System Preferences, Energy Saver but something vital isn't going to finish in time.

In that case, you can force your Mac to stay on permanently, or you can say it must stay on for a certain amount of time.

caffeinate

caffeinate -t 10800

These are two versions of the same command. The first one will keep your Mac on forever — so long as there's power and you don't quit Terminal.

The second specifies how long the Mac must stay on for and it does so in seconds. For an hour, that's 3600, for three hours it's 10800 and so on.

Whichever one you choose, you can stop it early, though. While in Terminal, hold down the Control key and tap C.

Up and down

So you can see how long your Mac has been on and you can tell it not to switch off— but still there are going to be times when it crashes or when there's a power cut. If an app crashes than there's little you can do unless you're at the keyboard and you spot it.

However, if you need your Mac to be running all the time, that's when you just know there's going to be a power surge or something in your neighbourhood.

In Terminal, though, you can enter this:

sudo systemsetup -setrestartfreeze on

You'll then be asked for your Mac's password, but once you've entered it, you'll never have to use this command again. And the next time your Mac powers down for any reason other than you choosing to switch off, this command will make the Mac start up again.

Ditto

The truth is that Terminal lets you do just about anything you can think of your Mac, and the only reason you don't use is that there are more user-friendly ways to do so. One of those is when you want to copy a lot of files from one folder to another.

Usually you would just select and drag them. When you've done that, you know you get a progress bar with a time showing you how long the copying should take. You also know that figure is rubbish. It will vary more than your GPS's estimate of time to destination.

You can do something in Terminal. You can use the Ditto command.

This is specifically for making a copy of files and putting them in a new place.

Ditto -V ~/Documents/Origin/ ~/Documents/Destination/

Using the Ditto command to copy files Using the Ditto command to copy files

This will copy all the files in a folder called Origin which is in your Documents folder and it will put the copies into Destination.

That looks fine but it also looks a bit pointless. What makes it more useful than the regular drag and drop is that -V part. This turns on Verbose mode and it means that Terminal will list every file it's copying, as it's copying it. You get to see more detail and you see it immediately.

So that looks good — except that Ditto has one tedious feature. You have to know the path to the source and the destination folders. The easiest way is to move between folders on the same Mac and it's easy because you can take a shortcut. Rather than figuring out your hard disk name, your username and so on, you can write ~/Documents/. That ~ symbol stands for whatever comes in the path before your Documents folder.

The regular way you can find out the path to the current folder The regular way you can find out the path to the current folder

Only, that's too simple an example. When you're copying between folders that are several levels deep, you have to know the names of every part of the path. You can navigate by opening a window and right-clicking on the title at the top, or you can do this in Terminal:

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

killall Finder

Do this once and until you repeat it with NO instead of YES, every Finder window will show you its own path.

Type one Terminal command and forever after you'll see the path in each Finder window Type one Terminal command and forever after you'll see the path in each Finder window

Files instead of folders

Terminal is handy for the big, powerful, sweeping moves like copying entire folders from place to place. There is something it can do with individual files, though, which is sometimes useful.

Only sometimes. This is not a command you'll use a lot and it isn't a command that will work very often, either. However, if someone has sent you a file that you cannot open in any other way, you can make Terminal show you the contents.

In Terminal, type the word cat and then you could again type the entire path to the file. Much better, though, is to drag that file onto the Terminal window.

It's only occasionally useful, but you can type a command and drag a file into Terminal to see what's in it It's only occasionally useful, but you can type a command and drag a file into Terminal to see what's in it

When you let go, Terminal replaces the document's icon with the correct path to it and then shows you what's in the file.

The giant majority of times this is worthless because what it shows is incomprehensible. However, if there's text inside that file, this is a way to read it and that can at least help you figure out what app you need to get to open it properly.

There's useless and there's useless

If that way of seeing the contents of a document isn't useful all the time, there is one last Terminal command that is never useful — but you won't be able to resist it anyway.

On macOS Sierra or later, type:

nc towel.blinkenlights.nl 23

You've seen ASCII art where people make images out of letters and punctuation. This is a movie. This is Star Wars in ASCII art.

To be fair, we're really just taking it on trust that this is the entire film because we've never got to the end. Somehow we prefer the Blu-Ray version of A New Hope to this. Yet for the sheer effort and chutzpah, it is fun to watch for a few minutes before you close Terminal.

Close down

We said this last time too. When you're done with Terminal, close it. Even if you are planning to go back in a few minutes' time, close it now.

Terminal is powerful and with that power does come the chance of causing problems. So use Terminal with care — but do use it.

Keep up with AppleInsider by downloading the AppleInsider app for iOS, and follow us on YouTube, Twitter @appleinsider and Facebook for live, late-breaking coverage. You can also check out our official Instagram account for exclusive photos.



3 Comments

djames4242 14 Years · 654 comments

...also, if you live and breathe in the Unix shell as I do, you probably already know that iTerm2 is a much better terminal app than is Terminal.app

ivanh 12 Years · 596 comments

...also, if you live and breathe in the Unix shell as I do, you probably already know that iTerm2 is a much better terminal app than is Terminal.app

Quote examples, please.

fastasleep 14 Years · 6451 comments


So that looks good -- except that Ditto has one tedious feature. You have to know the path to the source and the destination folders. The easiest way is to move between folders on the same Mac and it's easy because you can take a shortcut. Rather than figuring out your hard disk name, your username and so on, you can write ~/Documents/. That ~ symbol stands for whatever comes in the path before your Documents folder.

The regular way you can find out the path to the current folder
The regular way you can find out the path to the current folder


Only, that's too simple an example. When you're copying between folders that are several levels deep, you have to know the names of every part of the path. You can navigate by opening a window and right-clicking on the title at the top, or you can do this in Terminal:

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
killall Finder


Do this once and until you repeat it with NO instead of YES, every Finder window will show you its own path.

Type one Terminal command and forever after youll see the path in each Finder window
Type one Terminal command and forever after you'll see the path in each Finder window

Well, that's one way to do it — BUT why would you, when you can just select it from the Finder menu:


Also, there's no reason to type a path name, ever. You can either:

1) Simply copy the full path to any folder or file by right-clicking on it, holding option, and selecting "Copy xxx as Pathname" and pasting into Terminal

OR

2) Type the first part of the command, eg "Ditto -V " then drag the folder/file into Terminal and it will fill the path to that folder/file.