Summary
To copy a file from one location to another using your terminal, use the cp
command:
cp file-name folder1
To copy a folder from one location to another using your terminal, add the -R
option (this will make folder1 a subfolder inside folder2):
cp -R folder1 folder2
To copy the contents of one folder to another folder, use the following format:
cp -R folder1/ folder2
Warning
If there is already a file named file-name
in the destination folder, the above command will overwrite it with the contents of the first file-name
, and you will lose what was inside the file-name
in the destination folder. The same is true for folders: if folder2
contains a file with the same name as a file from folder1
, its contents will be overwritten by the file in folder1
. Use the cp
command with caution.
Details
Copy multiple files or folders
To copy multiple files, pass in the file names as arguments in between the cp
command and the destination folder:
cp index.html about.html other-folder
To copy multiple folders, pass in the folder names as arguments in between the cp
command and the destination folder:
cp folder-1 folder-2 other-folder
Options
You can use the -i
flag to add a prompt asking if you want to overwrite an existing file. For example, let’s say you want to copy index.html
into folder-1
, which already has a index.html
file inside it. With the -i
flag, you’ll see the following prompt:
overwrite folder-1/index.html? (y/n [n])
You can type the letter y or n and press Enter to either overwrite the file or not.
You can also use the -v
flag to get verbose output. For example, if you use the flag and overwrite the file in the example above, you would see the following output:
index.html -> folder-1/index.html
You can combine the 2 flags, like this:
cp -iv index.html folder-1
Exercises
Try the following command in your project folder:
cp css/style.css .
References
Command Line Primer on the Apple Developer Archive (see the section called Frequently Used Commands)