Home > News Tips

How to Copy a File/Directory in macOS Terminal with the cp command?

Updated on Friday, November 22, 2024

Written by

Jenny Zeng

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

While it's easy to copy a file or directory in Finder, executing the cp command in Terminal allows you to copy files faster, bash copy all files in a directory and subdirectories without opening windows in Finder, and interact with hidden files.

This article will teach you how to copy files or directories in macOS Terminal with the cp command.

cp command: Shortened for "copy," the cp command on Mac is used to copy files and directories locally.

If you want to learn all the usages of the cp command, run "man cp" in Terminal. In the following sections, we'll discuss its common usage.

How to quickly get the path to the file or folder you want to copy?

The easiest way to get the file path on Mac is by dragging and dropping the file or folder into the Terminal window. For instance, if you want to get the file path of the Documents folder, simply drag and drop it into the Terminal window.

How to copy a file or directory on Mac through Terminal?

 Note: Make sure you enter the correct command with the needed empty spaces, as each part of the command is separated by a space. Otherwise, the command won't work.

When copying a file or directory locally on Mac:

To copy a single file in Terminal:

cp file_path destination_directory_path

E.g., To copy a file named test.png from the desktop to the Documents folder: cp /Users/jenny/Desktop/test.png /Users/jenny/Documents

To copy and rename a file on Mac with Terminal:

cp file_path renamed_file_path

To copy and rename test.png to test-copy.png: cp /Users/jenny/Desktop/test.png /Users/jenny/Desktop/test-copy.png.

To copy a folder and its contents in Terminal:

cp -R folder_path destination_directory_path

E.g., To copy a folder named test and its directories and sub-directories in macOS Terminal to the Documents folder: cp -R /Users/jenny/Desktop/test /Users/jenny/Documents

 Note: The R flag tells the cp command to copy everything inside the designated folder.

How to copy multiple files from one directory to another in Terminal?

To copy contents of one directory to another in Terminal rather than the directory itself:

cp -R source_directory/ destination_directory_path

E.g., To copy files in the test folder on your desktop to the Documents folder: cp -R /Users/jenny/Desktop/test/ /Users/jenny/Documents (Notice the forward slash (/) used at the end of the source directory? This is the key to copying only contents, not the directory.)

To copy a specific file type to another directory in Terminal:

cp *.file_type destination_directory_path

E.g., To copy all txt files to your desktop: cp *.txt /Users/jenny/Desktop/

To copy selected files to another directory in Terminal:

cp file_path1 file_path2 destination_directory_path

E.g., To copy 2 files from the test folder on your desktop to the Documents folder: cp /Users/jenny/Desktop/test/picture1.png /Users/jenny/Desktop/test/picture2.png /Users/jenny/Documents

To bash copy all files in directories and subdirectories:

cp -R folder_path1 folder_path2 destination_directory_path

E.g., To bash copy all files in the test1 and test2 folders on your desktop to the Documents folder: cp -R /Users/jenny/Desktop/test1 /Users/jenny/Desktop/test2 /Users/jenny/Documents

 Tips: The Mac Terminal commands used above can also be used to copy files to an external hard drive on Mac.

Share this post to help others copy files in macOS Terminal!

 

How to copy hidden files in Mac Terminal?

To copy a specific hidden file:

cp hidden_file_path destination_directory_path

E.g., To copy a hidden file named .config to the backup folder on your desktop: cp /Users/jenny/.config /Users/jenny/Desktop/backup/

To copy all files including hidden ones, from one directory to another:

cp -R source_directory_path/{.*,*} destination_directory_path

E.g., To copy all files, including hidden items from the test folder to the backup folder: cp -R /Users/jenny/Desktop/test/{.*,*} /Users/jenny/Desktop/backup/

macOS script to copy files on Mac with the cp command

If you're looking to create a script on macOS to copy files using the cp command, take the following steps:

  1. Open Terminal.
  2. Create a script with a text editor like Nano. (for instance, copy_files.sh)nano copy_files.sh
  3. Copy and paste the following script into the file. (Ensure you replace the source_directory_path and destination_directory_path.)
  4. Press Ctrl + o to write the changes, then press Enter, and then Ctrl + X to exit.
  5. Give the script execution permissions.chmod +x copy_files.sh
  6. Now you can run the script../copy_files.sh

If you find this post helpful, share it to benefit others who want to learn how to copy files or directories in Mac Terminal.

Also Read:

How to Rename a File or Multiple Files in macOS Terminal?