The command line—that hidden world of code behind your Mac’s pretty face—sometimes offers a quicker way to do everyday tasks. You’ve learned how to navigate 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?
How to compress and uncompress files and folders in macOS via the Terminal/command line using zip, tar.gz, tar.bz2 and dmg formats.
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
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: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.
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:![Mac Terminal Commands Mac Terminal Commands](/uploads/1/2/5/6/125627773/806528799.png)
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:
mkdir MyDirectory1 MyDirectory2 MyDirectory3
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.To comment on this article and other Macworld content, visit our Facebook page or our Twitter feed.
Occasionally in Mac OS X, it may be necessary to force a program or process to quit. For example, if a particular program fails to respond or unexpectedly hangs. Every application on a Mac comprises of one or more processes.
It’s usually possible to use the Force Quit command (⌘⌥ esc) in the Apple Menu, but only individual applications are listed in the Force Quit Applications window rather than all processes which are running on your computer.
If you are new to using the OSX Terminal, I can recommend the Macintosh Terminal Pocket Guide by Daniel J. Barrett as a great way to get started.
The basic steps to check and kill a process are:If you are new to using the OSX Terminal, I can recommend the Macintosh Terminal Pocket Guide by Daniel J. Barrett as a great way to get started.
- Open the Terminal application
- List the running processes
- Find the process you want to close
- Kill the process
Each step is described in more detail below.
About Terminal
Probably the most useful tool to check and kill processes is called Terminal, which is an application that provides access to the lower levels of the Mac OS X operating system and files. Terminal is a text-based tool which lets you conduct all manner of routine tasks such as viewing directories, copying, moving and deleting files, as well as obtain detailed information about each process running including:
- the process ID (PID)
- the elapsed time spent running
- the command or application file path
![Commands Commands](/uploads/1/2/5/6/125627773/273225924.png)
About Activity Monitor
A related indispensable application is Activity Monitor – a graphical tool that allows you to manage processes, however it doesn’t have quite the same capabilities that Terminal does. Activity Monitor shows common process-related details such as the memory used and percentage of CPU that each process is consuming. When used together, Activity Monitor and Terminal provide a powerful yet relatively straightforward way to inspect and manage wayward processes.
The main Activity Monitor window is shown below.
Each application on your Mac has an associated Process ID (a PID) and a user-friendly name. From here you can inspect or quit each process, but in this example we use Activity Monitor simply as a companion to Terminal.
The Apple Mail application is displayed in Activity Monitor with a PID number of 14649. Note that process ID’s are assigned by Mac OS, and therefore will not be the same on your computer as somebody else’s.
How to Use Terminal
The first step is to open Terminal either from the Applications -> Utilities folder or simply type Terminal into Spotlight. Terminal is always represented by the icon below.
Once it opens you’ll be presented with a standard Terminal window as below.
The first line shows the date and time when you last logged in. The second line is the command prompt which is where you enter the commands you wish to execute . The command prompt always begins with your computer name followed by your local Account Name.
The current directory (the “working directory”) when you open Terminal always defaults to your Home Folder.
Basic Terminal Commands
Before we describe how to check and terminate a process on your computer it’s worth knowing a few basic Terminal commands.
- To show the current folder name type pwd
- To list the files in the current folder type ls -l
- To move into another folder type cd <folder name>
Note that many commands in Terminal can accept various options (sometimes called switches) that can alter their effect. The simplest way to discover the available command-line options is to type the command into Terminal followed by -? such as ls -?
Another useful command is apropos. Enter apropos <command> into the Terminal window for a description of that command and its options.
To View All Processes
- Type ps -ax at Terminal’s command prompt to list every process running, along with additional details such as the PID, the elapsed time running, and the process name and location (shown in the CMD column)
- Running Processes Displayed
To Find a Specific Process
The process list displayed using ps -ax may include a hundred or more processes, but it’s quite simple to identify a process based on the name in the CMD column (for example Skype is listed as /Applications/Skype.app/Contents/MacOS/Skype), or even by the PID if you already know it.
As shown in Activity Monitor earlier, the Mail application on my Mac had the PID 14649, so it’s simple to scroll down the Terminal window until the relevant process is found.
One very useful command to help find a process by name or PID is grep which can filter out the desired information. It can be used in conjunction with the ps -ax command to list only the process that you are interested in.
For example:
- At the command prompt type ps -ax | grep <application name>. For example ps -ax | grep Skype
The “pipe” function (“|”) simply uses the output from the process list as an input to grep, to filter out the desired process name.
Assuming that Skype is actually running, you may see a result something like this:
Roland-Bankss-MacBook-Pro:~ roly$ ps -ax | grep Skype 14530 ?? 0:56.32 /Applications/Skype.app/Contents/MacOS/Skype -psn_0_9218250 14947 ttys000 0:00.00 grep Skype
This example shows that Skype has a PID of 14530 and also the folder where Skype was launched from. The last line is just the process ID of the grep command itself, which can be safely ignored.
Repeating the command with the Skype process ID instead i.e. ps -ax | grep Skype yields the same result.
To Terminate (Kill) a Process
Once you know the process ID, killing it using Terminal is very simple. Be cautious however because forcing a process to suddenly exit can have unforeseen consequences, so it’s advisable to check carefully that the process you are about to kill is the correct one. There are essentially two easy ways to kill a process:
- By PID: the simplest way is with the kill command followed by the PID, which causes the selected process to terminate immediately. In the Sky example, kill 14530 does the job and causes the process to exit immediately
- By name: this method uses the killall command to kill all the processes that contain that name. For example killall Skype will terminate all the processes that have Skype in their name
Caution: killall should be used sparingly to avoid accidentally terminating the wrong processes. There is no confirmation prompt to ask if you really do wish to kill the processes, so check carefully beforehand.