Last Updated on July 6, 2022 by srinivas
The command line—that hidden world of code behind your Mac’s pretty face—sometimes provides a faster way to perform mundane tasks, and it’s also just a cool way to build your tech reputation. You learned how to navigate and delete files and folders using 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 folders (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 may contain settings for certain apps or parts of the Mac, contain a period (.) before their names, and the Finder won’t show them.
- You can copy or move multiple files using wildcards.
- You can quickly rename a file.
- If you can’t access the Finder because your Mac is blinking, you may be able to use the command line to fix 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 another folder on the same drive or volume, you’re moving the file. The file is no longer on the desktop and can only be found in the Documents folder. However, if you drag a file from your desktop to an external hard drive, you will 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 drive, by holding down the Option key as you drag it.)
The same is the case from the command line. There are two commands for move and copy: mv
and cp
† The first does the same as dragging a file to a new location on the same hard drive. The second does what an Option drag does, or what happens when you drag a file to another drive or volume.
How to copy files and folders
Copy files with the cp
command is simple. First, launch Terminal (located in the /Applications/Utilities folder). Then use the following syntax to create your command:
cp source destination
For example, to copy a file called MyFile.rtf from your desktop folder to your Documents folder, type the following command in Terminal, then press return:
cp ~/Desktop/MyFile.rtf ~/Documents
You now have a file called MyFile.rtf on your desktop and a copy of that file in your Documents folder.
You remember from “Manage the Command Line: Navigating Files and Folders” that the tilde symbol (~) is a shortcut to your home folder, which contains your Documents folder. This command takes the file to the exact path you specify as the source argument and moves it to the folder (folder), which is the destination. Note that if there is no file, or if you mistype the name, Terminal will give you a “No file or folder” error.
If you mistype a file path, Terminal will notify you with the error message “No such file or folder”.
IDG
You can also copy folders, including any 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 extra letter — always preceded by a hyphen (-) — tells the command to do something else. The recursive option tells the cp
command to copy every item in the folder: every subfolder, every file and folder in every subfolder, and thus one, all the way down, to the new location. So here’s how to copy a folder from your desktop to your Documents folder:
cp -R ~/Desktop/MyFolder /Documents
How to move files
You probably guessed that the mv
command works the same way. But there are two ways you can use the mv
order. The first moves a file to another disk or volume; remember, just like in the Finder, copying a file to another volume will not delete the original, while moving it will. So you can run this command to move a file from your desktop to a folder on a backup disk:
mv ~/Desktop/MyFile.rtf /Volumes/Backup/MyFolder
You can also move folders using the mv
order. The syntax is the same and you don’t need the -R
flag as you do with the cp order.:
mv ~/Desktop/MyFolder /Volumes/Backup
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 .rtf (Rich Text Files) files from your desktop to your Documents folder, you can use the asterisk
cp ~/Desktop/*.rtf ~/Documents
wild card: mv
You can use the same wildcard with the
command to move multiple files.
Rename files mv
The mv
command also lets you quickly rename files. What you are doing is essentially moving a file to the same location, but renaming it. If you enter a name for the destination, the
mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf
command changes the name of the file when it moves the file. You can change a file name as follows:
This is a valuable troubleshooting tool; you can use this to back up a file, such as a preferences file, in case you need it again. But you can also use this renaming method simply because you want to rename a file. cp
You can also copy a file with
cp ~/Desktop/MyFile.rtf ~/Documents/MyFile1.rtf
and change the name. In this case, you need to specify not only a destination folder, but also a name for the file:
How to Create Folders (aka Folders) mkdir
Here’s one last command that may come in handy: the create folder cd
order. This is very useful if you need to create a lot of folders in one fell swoop, for example for a new project you are starting. First use the†change folders DirectoryName
) command to go to the folder where you want to create a new folder. Once there, run this command (in the example below DirectoryName
is the name you want. To replace
mkdir DirectoryName
with your desired name):
mkdir MyDirectory1 MyDirectory2 MyDirectory3
You can use any folder name (for example, “Hot Project” or “TPS Reports”), and you can create multiple folders with a single command:mv
With these three simple commands— cp
† mkdir
and
— you can copy and move files, and create folders to keep files anywhere in your Mac’s file system. As you become proficient with these commands, you will see how easy they are to use.