Terminal Mac Os



If you’ve been using a Mac for any length of time, you know that it’s more than just a pretty point-and-click, window-and-icon interface. Beneath the surface of the operating system is an entire world that you can access only from the command line. Terminal (in your /Applications/Utilities folder) is the default gateway to that command line on a Mac. With it, instead of pointing and clicking, you type your commands and your Mac does your bidding.

There are times when your Mac may misbehave and refuse to boot into OS X. You may get a sad Mac face, an audible beep, or another ailment keeping your Mac from properly booting. What is Terminal? Apple's Terminal app is a direct interface to OS X's bash shell — part of its UNIX underpinnings. When you open it, Terminal presents you with a white text screen, logged in with your OS X user account by default. Beneath the surface of the operating system is an entire world that you can access only from the command line. Terminal (in your /Applications/Utilities folder) is the default gateway to that.

Why would you want to do that? For almost all of your computing needs, the regular graphical user interface is enough. But the command line can be handy when it comes to troubleshooting your Mac, to turn on “hidden” settings, and other advanced chores. It’s a good idea for anyone who isn’t an utter beginner to be familiar with it.

If you aren’t already familiar with your Mac’s command-line interface. First up: How to navigate the file system from the command-line prompt.

The prompt

By default, when you open Terminal, the first thing you’ll see is something like this:

The first line shows the last time you logged into your Mac via the command line; that’s the current time, when you’re using Terminal. The second line is the prompt, and while it can change from system to system depending on configuration, by default it contains several bits of information.

In my prompt, walden is the name of my Mac (same as the name in the Sharing pane of System Preferences), and kirk is my user name. The ~ shows where I am in the file system of my Mac; ~ is a shortcut that means the current user’s home folder. (In the Finder, that’s the folder with your user name and the house icon.) Finally, the $ is a character that the bash shell (the default interface that Terminal uses) displays to indicate that it’s ready to accept a command.

Where Is Terminal Mac

What’s in a folder

Terminal Mac Os

When you first get to the command line, you’re in your home folder. While you’re there—or when you’re in any folder (directory in Unix-speak)—you might want to know what’s in it. To do that you use the ls (or list) command. Type ls and press the Return key, and you’ll see the folders (and/or files) in the current directory.

The output of the plain ls command is pretty sparse; it shows you the names of files and folders contained in the current directory (including some familiar ones such as Movies, Music, Pictures, and so on). Fortunately, you can add a number of optional switches to the ls command that allow you to see more information. So, for example, try typing ls -l (that’s a lower-case L), then pressing Return. You’ll see something like this:

Don’t worry too much about what all that means right now; we’re just getting our feet wet. The point is that ls can provide additional information about files and folders, depending on the options you specify. In this case, that additional information includes the name of the user who owns each item in the directory. (That ownership is part of the Unix system’s file-permissions regime.) The kirk kirk next to most of those items above means that each one is owned by the user kirk, who is in the group kirk. The other understandable bit of information next to each file and folder is the date and time each one was last modified.

One other handy option: You can view invisible files—ones that the Finder doesn’t normally show you—by typing ls -a. (These hidden files all have dots (.) in front of their names.)

Moving around

When you’re in the Finder and you want to move to another folder, you find that folder and double-click it. From the command line, you use the cd (or change directory) command instead. So let’s say you’re in your Home folder and want to peek inside the Downloads folder. To do that, you’d type cd Downloads. (Remember to always type a space after any command that has an additional argument, such as the name of a directory in the previous example.) Once you’ve done that, ls will show you the contents of your Downloads folder.

Here are a couple of quick tricks for moving around in your Mac’s file system.

  • If you type cd and press the Return key—with no directory specified—you’ll go back to your Home folder. (You can also type cd ~ to go there.)
  • If you type cd /, you’ll go to the root level of your startup disk.
  • If you type cd .. (that’s two periods), you’ll go to the directory above the one you’re currently in. So if you’re in your home folder, and type cd .., you’ll go to your Mac’s /Users folder.
  • And if you type cd - (hyphen) you’ll go back to the directory you were in before the last time you issued the cd command.

To learn more Terminal commands, see our articles on how to copy and move folders as well as delete files and folders using the command line and get help when you need it from man pages.

The command line—that hidden world of code behind your Mac’s pretty face—sometimes offers a quicker way to do everyday tasks, plus it’s just a cool way to establish your tech cred. You’ve learned how to navigate files and folders as well as delete files and folders with the command line and get help when you need it from man pages. Here, I’ll show you how to copy and move files, common operations that often come in handy. I’ll also show you how to create directories (that’s Unix-speak for folders), so you can move files to new places.

Why bother with the command line?

It’s certainly easy to copy and move files in the Finder, but there are a number of reasons why you might want to do this from the command line instead:

  • You can copy or move files from one location to another without opening windows in the Finder.
  • You can copy or move files that are hidden in the Finder. These files, which can contain settings for certain apps or parts of the Mac, contain a dot (.) before their names, and the Finder doesn’t show them.
  • You can copy or move multiple files using wildcards.
  • You can rename a file quickly.
  • If you’ve lost access to the Finder because your Mac is on the blink, you might be able to use the command line to troubleshoot the problem.

The difference between copying and moving files

If you’re in the Finder, and you drag a file from, say, your Desktop to your Documents folder, or any other folder on the same disk or volume, you move the file. The file is no longer on the Desktop, and is found only in the Documents folder. However, if you drag a file from your Desktop to an external hard disk, you’ll see that the file remains in its original location; this file has been copied. (You may know that you can copy a file in the Finder, even on the same hard disk, by holding down the Option key when you drag it.)

The same is the case from the command line. There are two commands for moving and copying: mv and cp. The first does the same as dragging a file to a new location on the same hard disk; the second does what an Option-drag does, or what happens when you drag a file to a different disk or volume.

How to copy files

Terminal

Copying files with the cp command is simple. First, launch Terminal (in your /Applications/Utilities folder). Then, use the following syntax to create your command:

cp source destination

For example, to copy a file named MyFile.rtf from your Desktop folder to your Documents folder, you would type in the following command in Terminal and then press Return:

cp ~/Desktop/MyFile.rtf ~/Documents

You’ll now have a file named MyFile.rtf on your Desktop, and a copy of that file in your Documents folder.

You’ll remember from “Master the command line: Navigating files and folders” that the tilde (~) symbol is a shortcut for your Home folder, which contains your Documents folder. This command takes the file at the precise path you specify as the source argument, and moves it to the directory (folder), which is the destination. Note that if there’s no file there, or if you type the name incorrectly, Terminal will give you a “No such file or directory” error.

You can also copy directories, including all the files they contain. This uses a special “flag” or “option” with the cp command: the -R or recursive flag. When you use options with commands, this additional letter—always preceded by a hyphen (-)—tells the command to do something a bit differently. The recursive option tells the cp command to copy every item in the folder: every sub-folder, every file and folder in every sub-folder, and so one, all the way down, to the new location. So you can copy a directory from your Desktop to your Documents folder like this:

cp -R ~/Desktop/MyFolder /Documents

How to move files

You’ve probably guessed that the mv command works in the same way. But there are two ways you can use the mv command. The first moves a file to a different disk or volume; remember, just as in the Finder, copying a file to a different volume won’t delete the original, whereas moving will. So you could issue this command to move a file from your Desktop to a folder on a backup disk:

You can also move directories with the mv command. The syntax is the same, and you don’t need the -R flag as you do with the cp command.:

mv ~/Desktop/MyFolder /Volumes/Backup

How to copy or move multiple files

One of the great things about the command line is the way you can use wildcards to simplify commands. For example, if you want to copy all the .rtf files (Rich Text Files) from your Desktop to your Documents folder, you can use the asterisk (*) wildcard:

cp ~/Desktop/*.rtf ~/Documents

You can use the same wildcard with the mv command to move multiple files.

How to rename files

The mv command also lets you quickly rename files. What you do is essentially move a file to the same location, but change its name. If you specify a name for its destination, the mv command changes the file’s name when it moves the file. You can change a file name like this:

Mac

mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf

This is a valuable tool for troubleshooting; you can use this to create a backup copy of a file, such as a preference file, in case you need it again. But you can also use this renaming method simply because you want to rename a file.

How to open terminal mac

You can also copy a file with cp and change its name. In this case, you need to specify not just a destination directory, but also a name for the file:

cp ~/Desktop/MyFile.rtf ~/Documents/MyFile1.rtf

How to create directories (a.k.a. folders)

Here’s one final command that may come in handy: mkdir, the make directory command. This is very useful when you need to make a bunch of folders in one fell swoop, say for a new project you’re starting. First use the cd (change directories) command to move into the directory where want to create a new directory. Once you’re there, run this command:

mkdir MyDirectory

You can use any name for the directory (for instance, “Hot Project” or “TPS Reports”), and you can make multiple directories with a single command:

Mac

mkdir MyDirectory1 MyDirectory2 MyDirectory3

Macos Terminal App

With these three simple commands—mv, cp, and mkdir—you’ll be able to copy and move files, as well as create directories to hold files anywhere in your Mac’s file system. As you become proficient with these commands, you’ll see how easy they are to use.