How to Copy a File or Directory via Mac Terminal

Mac Terminal is a powerful tool that lets us perform essential tasks from a command line interface. There are many reasons to use it, and if you are new to Terminal or even an experienced user, you will find that the ability to copy files and directories is important.

You can do basic file and directory copying on Mac Terminal with the cp command. It’s relatively straightforward to use, but you must remember that copying files are slightly different than copying directories, which we will discuss below.

My name is Eric, and I have been a software engineer for over 25 years. I’ve spent a large amount of that time working from command line interfaces like Mac’s Terminal application. I often copy files and directories, so I can show you how to do this as well.

If you want to see how to copy items in Mac Terminal, keep reading below. I will cover some of the basics and give you some helpful tips to make using the cp command easier.

Copy with Terminal

As you use Mac’s Terminal application, you will often find the need to copy a file or directory. This is a very common action that is needed in many different situations. Copying a file can be quite simple in most cases, but it can get a little complicated, depending on what you do with it.

The first thing to note is that using the cp command to copy a file is slightly different than using it to copy a directory. Unlike the mv (move) command, the cp command will not automatically copy everything under the directory to the new location. You must specifically tell it to do so.

Since the two actions require slightly different parameters, we will look at copying files and copying directories separately so that you can see the difference between the two. So let’s get started with a simple file copy below.

Simple File Copy

If you are just doing a simple file copy, for example, copying a file in a directory to a new name in the same directory, the command will look as shown below.

cp <source file name> <new file name>

If I have a file called test.txt and I want to make a copy of it in the same directory called newtest.txt. I would run the following command.

cp test.txt newfile.txt

Simple Directory Copy

Copying a directory is slightly different because you must ensure that you also copy the files within that directory. If you try to copy a directory using the same command above, you will see an error message stating that the name you are trying to copy is a directory.

To copy a directory, you need to specify the -R parameter, which stands for recursive. This tells the command to copy everything under the directory to the new name. The command should be like the one shown below.

cp -R <source dir name> <new dir name>

For our example.

cp -R testdir newtestdir

As you can see, besides the -R parameter, the command is the same. For the rest of our examples, I will just be copying files. Just remember, if you wish to copy a directory to include the -R parameter.

Copy to a Different Path

There are many situations where you will want to copy a file from one location or path to a new one. The format for this type of command is shown below.

cp <source file name> </path>

The above command assumes that I am already in the directory with the file <source file name> and also that I want the copy of the file to have the same name. For the example below, I will copy a file called test.txt to a directory with a path /Users/ericwinkler/Test3.

cp test.txt /Users/ericwinkler/Test3

If I wanted, I could give the copy of the file in the new location a new name instead of keeping it the same. All I need to do is specify the new file name at the end of the destination path.

cp <source file name> </path/newfilename>

For our example. I might want the copy in the new location to be called newtest.txt.

cp test.txt /Users/ericwinkler/Test3/newtest.txt

I also don’t have to be in the directory of the original source file. I can specify the path of the original source file with its name and copy it to another location with the command below.

cp </sourcepath/sourcefile> </destinationpath>

To continue with our example, the command would be as shown below.

cp /Users/ericwinkler/Test2/test.txt /Users/ericwinkler/Test3

For one more example, I will show how to copy a file from a location specified by a path to my current location. The path of my current location can be specified with the . symbol. So if I want to copy a file to my current path, I could use the following command.

cp </sourcepath/sourcefile> .

Using our example files.

cp /Users/ericwinkler/Test2/test.txt .

As you can see, there is what seems to be an endless combination of ways that you can use the cp command to copy a file from one location to another. If you are trying to copy a directory, all you need to do is add the -R parameter after the cp command, which will work the same way.

You can use some other options and parameters with the cp command. If you want to see a full list of them, you can use the man command to list them. Just type it in as shown below and hit the return key.

man cp


Below are a few common questions about copying files via Mac Terminal.

What is the difference between copy and move?

The copy or cp command only makes a copy of the source file, and the original source file remains untouched and unaffected. The move or mv command moves the file’s location or moves it to a new name. The original file will no longer exist.

Will the copy of the file have the same date and permissions?

Using just a simple copy command, as shown above, will create a copy with a new date, owner, and file permissions. If you wish to preserve those attributes, you can use the -p parameter with the cp command, as shown below.

cp -p <source file name> </path/newfilename>

Can I use wildcards to specify files to copy?

Yes, you certainly can. Wildcard characters are often an important part of using the copy command when you want to copy a specific set of files that you can define with a wildcard. The use of wildcard characters could probably be a whole article itself but below is a simple example.

If I wanted to copy all of the text or .txt files from my directory to a new directory called textfiles, I would use the command below.

cp *.txt textfiles


Copying files or directories in Mac Terminal can be straightforward using the cp command along with the source and destination of the files. Remember to use the -R parameter when copying directories so that the contents underneath the directory will get copied recursively.

I hope the information above can help you learn how to copy files and directories. 

Let me know if you have any questions or comments. I would love to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *