How to Copy Files in Linux: cp Command Explained with Examples
By
Liz Fujiwara
•
Aug 12, 2025
Need to copy files or directories in Linux efficiently and reliably? The cp command is your go-to tool for all copying tasks in the Linux environment. Whether you’re moving a single file, duplicating entire directories, or managing backups, mastering cp will streamline your workflow and save you valuable time. In this article, we’ll break down the command’s syntax, walk you through practical examples, and highlight essential options that simplify even the most complex copy operations. By the end, you’ll be confidently handling file copying like a Linux pro.
Key Takeaways
The cp command in Linux is essential for copying files and directories, offering customizable options for efficient file management.
You can copy single files, multiple files, or entire directories using simple commands, with options to preserve attributes and prevent overwrites.
Advanced features of the cp command include interactive mode, verbose output, and support for wildcards, enhancing its functionality for a variety of copying tasks.
Understanding the cp Command in Linux

The cp command is a fundamental Linux utility used to duplicate files and directories from one location to another without altering the original content. Its basic syntax requires specifying the source files or directories followed by the destination location, with at least two arguments provided: the source and the destination.
The cp command is versatile, offering multiple options that customize the copying process and manage file attributes. These features make it more than a simple copying tool and are essential for efficient advanced file management. This foundational knowledge prepares you to explore the more complex operations and options available with cp.
Copying Files in Linux Using the cp Command

Copy a Single File
To copy a single file using the cp command, specify the source and destination paths to copy a file from one location to another. Start by opening a terminal window. The basic syntax to copy a file to a different directory while keeping the same name is:
cp my_file.txt /destination_directory/
If you want to copy a file within the same directory but under a different name, use:
cp original_file.txt new_file_name.txt
Be aware that the cp command will overwrite destination files without warning unless specified otherwise. To avoid this, you can rename the target file or use options that prevent overwriting.
After copying, you can verify the operation by listing the directory contents with the ls command.
Copy Multiple Files
The cp command can also handle copying multiple files at once. To do this, list each source file followed by the destination directory. For example, to copy file1.txt and file2.txt to the /backup/ directory, use:
cp file1.txt file2.txt /backup/
This approach allows efficient management of multiple files in a single command within the file system.
Wildcards such as * can be used to select and copy multiple files that match specific patterns. For example, to copy all *.txt files from one directory to another, use:
cp *.txt /destination_directory/
This method is particularly useful for batch operations, saving time and effort when dealing with numerous files.
Copying Directories in Linux

Copying directories in Linux requires a slightly different approach. The cp command can copy entire directories and their contents by using the -r (or -R) option. This option ensures that all files and subdirectories within the source directory are included in the copy operation.
When copying directories, if the destination directory does not exist, the cp command will create it automatically and copy the contents recursively. If the destination directory exists, the source directory will be copied as a subdirectory within the destination. Using these options correctly is crucial for successful directory copying.
Recursive Copying with -R Option
The -R option is essential for recursively copying directories and their contents. By using cp -R source_directory/ destination_directory/, all nested files and directories within the source directory are included in the copy operation. This preserves the directory structure and ensures a comprehensive copy.
You can use either the -R or -r option to enable recursive copying with the cp command. This flexibility allows you to copy entire directories along with their subdirectories and files, maintaining the original structure. Mastering this option is key to efficient directory management.
Preserving Attributes with -p Option
The -p option is crucial for preserving original file attributes such as modification time, access time, ownership, file permissions, and extended attributes during the copy process. This is particularly important when maintaining the integrity of file metadata.
In addition to the -p option, the -a option can be used to retain the original file’s metadata, including its permissions and creation date. Using these options ensures that copied files keep their original attributes, which can be critical for certain applications and compliance requirements.
Advanced Options for cp Command
The cp command offers various advanced options that enhance its functionality, allowing users to perform more sophisticated file copying tasks. These options include preserving file attributes, interactive mode, verbose mode, and preventing overwrites.
We’ll explore these options in detail, starting with interactive mode, followed by verbose mode, and finally preventing overwrites. Understanding these advanced features will help you customize the cp command to fit your specific needs and avoid common pitfalls.
Interactive Mode with -i Option
The -i option in the cp command prompts users for confirmation before overwriting existing files. This interactive mode helps avoid unintentional overwriting by asking for confirmation before replacing files. If you do not use the -i option, existing files in the destination are silently overwritten.
To use interactive mode, include the -i flag in your command, such as cp -i file1.txt /destination_directory/. This ensures you have control over the overwrite behavior.
Verbose Mode with -v Option
The -v option enables verbose output, providing real-time updates on the copying process. This option displays progress during file copying and echoes each operation to the standard output.
Using the -v option helps you monitor progress and gives additional information about each file being copied. For example, you can use cp -v file1.txt /destination_directory/ to see detailed output as the file is copied.
Preventing Overwrites with -n Option
The -n option ensures that existing files at the destination are not overwritten during the copy process. It silently skips copying files that already exist, preventing any data from being overwritten.
To use this option, include the -n flag in your command, such as cp -n file1.txt /destination_directory/. This is especially useful when you want to preserve existing files and avoid accidental data loss.
Secure Copying with scp Command

The scp command is used to securely transfer files between machines over a network using SSH encryption. It is ideal for copying files between local and remote machines while ensuring secure data transfer.
The basic syntax for the scp command involves specifying the source and target paths, including user and host information if the files are remote. For example, to copy a file from a local machine to a remote server, you can use:
scp myfile.txt user@remote_host:/path/to/destination/
This command includes:
myfile.txt: the source file on the local machine
user@remote_host: the username and remote host where the file will be copied
/path/to/destination/: the destination path on the remote server
This setup ensures a secure and efficient file transfer process.
Practical Examples of Copying Files and Directories
Practical examples help solidify theoretical knowledge. The cp command is often used to create backups by copying files to a designated backup directory. For instance, you can copy a document from your current directory to another directory using the command: cp myfile.txt /backup/myfile.txt.
We’ll explore specific examples of copying files based on patterns and between directories. These examples will provide hands-on experience and demonstrate the versatility of the cp command in various scenarios.
Copying Files Based on Patterns
Using wildcard characters with the cp command allows you to copy multiple files that match a specific naming pattern. Wildcards are special characters in shell commands that represent one or more characters in filenames.
To copy all .txt files from one directory to another, use the command cp *.txt destination_directory/. This method is highly efficient for managing groups of files based on patterns and significantly streamlines the copying process.
Copying Files Between Directories
When copying files to a directory, if the destination directory does not exist, the cp command will create it automatically. To copy directories and all their contents, the -r option is required.
Use ls to list files in the directory and confirm a successful copy operation. For example, to copy the contents of one directory to another, use:
cp -r source_directory/ destination_directory/
How to Copy Files in Linux: cp Command Explained with Examples

The cp command can take multiple source files and copy them to a single target directory. To consolidate our learning, let’s look at a table summarizing the cp command options and examples discussed:
Command | Description | Example |
cp file1.txt /backup/ | Copy a single file to a different directory | cp myfile.txt /backup/ |
cp original_file.txt new_file_name.txt | Copy a file in the same directory with a different name | cp original_file.txt new_file_name.txt |
cp file1.txt file2.txt /backup/ | Copy multiple files to a directory | cp file1.txt file2.txt /backup/ |
cp *.txt /backup/ | Copy all .txt files to a directory | cp *.txt /backup/ |
cp -R source_directory/ destination_directory/ | Recursively copy a directory | cp -R source_directory/ destination_directory/ |
cp -p file1.txt /backup/ | Copy a file and preserve attributes | cp -p file1.txt /backup/ |
cp -i file1.txt /backup/ | Interactive mode to prevent overwriting | cp -i file1.txt /backup/ |
cp -v file1.txt /backup/ | Verbose mode to show progress | cp -v file1.txt /backup/ |
cp -n file1.txt /backup/ | Prevent overwriting existing files | cp -n file1.txt /backup/ |
Understanding these commands and options will improve your ability to manage files in Linux effectively on UNIX-like operating systems.
Summary
Mastering the cp command in Linux is essential for efficient file and directory management. From basic file copying to advanced options like preserving attributes and preventing overwrites, cp offers a versatile toolset for handling various tasks.
Throughout this article, we’ve explored practical examples of copying single and multiple files, directories, and even secure copying with the scp command. These examples, along with the summary table of cp command options, provide a comprehensive understanding you can apply in real-world scenarios. As you continue practicing and exploring these commands, managing files in Linux will become more intuitive and efficient.