To copy a file in the terminal, use the cp command followed by the source file path and the destination path (e.g., cp source.txt destination.txt). This command creates a duplicate of a specified file or directory at a new location while keeping the original intact.
The cp command is a fundamental utility available in Unix-like operating systems, including Linux and macOS. Whether you are backing up a configuration file or organizing project folders, understanding the syntax of this command allows you to manage your filesystem efficiently without needing a graphical user interface.
What You'll Need
- A computer running a Unix-based operating system (Linux, macOS, or WSL on Windows).
- Access to a Terminal application.
- The necessary permissions to read the source file and write to the destination directory.
Step-by-Step
- Open your Terminal: Launch your terminal application. On macOS, you can find this in Applications > Utilities > Terminal. On most Linux distributions, you can use the keyboard shortcut Ctrl + Alt + T.
- Locate your source file: Use the
lscommand to list the files in your current directory to ensure the file you want to copy is present, or usepwdto confirm your current working directory. - Copy a single file: To copy a file to a new name in the same folder, type
cp, followed by the original filename, then the new filename.
Example:cp notes.txt notes_backup.txt - Copy a file to another directory: To move a duplicate of a file into a different folder, type
cp, the filename, and the path to the destination folder.
Example:cp photo.jpg ~/Pictures/ - Copy multiple files to a directory: You can list multiple source files followed by a single destination directory.
Example:cp file1.txt file2.txt file3.txt ~/Documents/backup/ - Copy an entire directory: To copy a folder and all the files and subfolders inside it, you must use the
-r(recursive) flag.
Example:cp -r /home/user/my_folder /home/user/backup_folder - Verify the copy: Use the
lscommand on the destination directory to confirm that the file or folder has been successfully duplicated.
Common Mistakes
- Forgetting the -r flag for folders: If you try to copy a directory using
cp folder1 folder2without the-rflag, the terminal will return an error stating that the source is a directory and cannot be copied. Always use-rfor folders. - Overwriting existing files: By default,
cpwill overwrite a file at the destination if it has the same name, without asking for confirmation. This can lead to accidental data loss. - Incorrect paths: Using a relative path when you are not in the expected directory. If the terminal says "No such file or directory," double-check your current location with
pwd. - Permission denied: Attempting to copy files into system directories (like
/etcor/var) without administrative privileges.
Tips
- Use the Interactive Flag: To prevent accidental overwriting, use the
-iflag. This forces the terminal to ask for your permission before replacing an existing file.
Example:cp -i source.txt destination.txt - Preserve File Attributes: If you need to keep the original timestamps, ownership, and permissions of the file, use the
-pflag.
Example:cp -p archive.zip /backup/archive.zip - Use Tab Completion: You do not have to type long file paths manually. Type the first few letters of a file or folder and press the
Tabkey; the terminal will automatically complete the name for you. - Wildcards for Batch Copying: Use the asterisk (
*) to copy all files of a certain type. For example,cp *.jpg ~/Pictures/copies every JPEG file in the current folder to the Pictures directory.
Troubleshooting
If the copy command does not go as planned, try these steps in order:
- Check for typos: Ensure there are no spelling errors in the filenames or paths. Remember that Linux and macOS file systems are case-sensitive (e.g.,
File.txtis different fromfile.txt). - Verify permissions: If you see "Permission denied," you may not have the right to access the source or write to the destination. If you are an administrator, you can prefix the command with
sudoto run it with root privileges.
Example:sudo cp config.conf /etc/config.conf - Check disk space: If the copy process stops midway or returns an "No space left on device" error, check your available storage using the
df -hcommand. - Check destination path: Ensure the destination folder actually exists. The
cpcommand cannot create a new directory on the fly; you must create the folder first usingmkdirbefore copying files into it.
Frequently Asked Questions
Can I use cp to move a file instead of copying it?
No, cp only creates a duplicate. To move a file (which removes it from the source and places it in the destination), use the mv command.
What is the difference between cp -r and cp -a?
While -r copies directories recursively, -a (archive) is more comprehensive. It preserves permissions, ownership, and symbolic links while copying recursively, making it better for full system backups.
How do I copy a file to a remote server?
The cp command only works for local files. To copy files between different computers over a network, use scp (Secure Copy Protocol) or rsync.
Does the cp command work on Windows Command Prompt?
No, the cp command is specific to Unix-like shells. In the Windows Command Prompt (CMD), the equivalent command is copy for files and xcopy or robocopy for directories.
Can I copy a file and rename it at the same time?
Yes. If you specify a new filename in the destination path, the terminal will copy the file and assign it that new name. For example, cp old.txt /home/user/new.txt copies the file and renames it to new.txt.