X
Tech

5 MacOS commands every user should know

Ever find yourself wanting to do things on your Mac with a bit more efficiency? The command line is ready and waiting! Here are the first five commands you ought to learn.
Written by Jack Wallen, Contributing Writer
An Apple keyboard.
Jack Wallen/ZDNET

Because I've been using Linux for such a long time, the command line is second nature to me. I know which commands I need to do certain things -- and they never fail. That's one of the reasons why I decided using the command line in MacOS made sense too. After all, the MacOS and Linux CLI (command line interface) are quite similar.

But if you're new to the practice of running commands, you probably have no idea where to start. That's where I come in. Allow me to introduce you to five commands that not only will serve you well but aren't even remotely hard to use.

Also: I've used Linux for 30 years. Here are 5 reasons why I'll never switch to Windows or MacOS

And with that, let's dive into the commands.

1. Available free space

When you want to find out how much space is left on your hard drive (or multiple drives, if you have them attached), you could always go to System Settings, search for Storage, and then allow the GUI to calculate the available free space. Or, you could open the terminal app and issue a single command that will display how much free space is available on all attached drives. The command in question is df, which displays free disk space. You can view the free space on all of your drives by issuing the command like this:

df

My favorite way of using df is in human-readable format. When you use df without options, it will display the results in blocks, which can be very confusing. Instead, use the command:

df -h

This will present the output in GB, which is much easier to understand.

2. Killing a rogue app

It might be a rare occasion, but sometimes an app goes rogue and refuses to close. Sure, you could always use the Force Quit tool (from the Apple menu in the top bar). Or, you could use the kill command. I've had one instance where the MacOS GUI refused to respond. Instead of doing a hard restart, I used SSH to log into the machine, locate the problematic app, kill it from the command line, and my GUI came back to me. 

Before you can kill the app, however, you must first locate its PID (Process ID). To do that, issue the command:

ps aux | grep NAME

Where NAME is the name of the app (such as Safari). 

Once you have the PID, you can kill the rogue app with the command:

kill PID

Where PID is the PID of the app in question.

3. Open a file

Did you know you can open a file from the command line? Not only can you open a file, but using this command will open the file in the correct application. For example, if you have a document that you want to open in Pages, this command will do it. When you use this command, it's as if you're double-clicking the file in Finder. 

Also: 5 Linux commands you need to know to troubleshoot problems

How do you do it? You first have to navigate into the folder housing the file. Say your file (we'll call it zdnet.docx) is in Documents. Open the terminal app and issue the command cd ~/Documents. Once there, you can open that file with the command:

open zdnet.docx

Whatever default application you have set to open docx files will launch and you're ready to work. If you want to open a file with a non-default application, you can do so using the -a option. For example, you might have both Apple Pages and LibreOffice Writer installed and you want to open the document with Writer. For that, you could issue the command:

open -a LibreOffice.app zdnet.docx

4. Restart your machine

Have you ever found your iMac or MacBook refusing to restart? I have and it can be frustrating. Fortunately, there's a simple command you can use to force a restart. One thing to keep in mind, however, is that this command requires the elevated privileges that come with sudo (which stands for superuser do). When you use sudo, you will be prompted to type your user password. Only after successfully typing your user password will the command in question run.

The command to force a reboot is:

sudo reboot

After typing your user password, the machine will reboot.

5. Learn about a command

Finally, this little doozy of a command will come in very handy as you navigate the CLI. The man command allows you to read the manual pages for each command. For instance, say you want to learn more about the reboot command. For that, you could issue the command:

man reboot

Also: 6 features I wish MacOS would copy from Linux

The manual page will open and you can scroll through it, learning all there is to know about the command. Nearly every command on your MacOS machine will have a manual page. You can even read the manual page for the man command, which would be:

man man

And there you have it, my friends: the first five commands I believe every MacOS user should learn. That doesn't mean you have to learn them. But if you want to get the most out of the operating system, this is a great place to start.

Editorial standards