What is cp Linux command?
The Linux cp command is used to copy files and directories. It allows you to create exact copies of files while preserving their attributes like permissions and timestamps. With cp, you can also copy multiple files at once, copy directories recursively, and specify a different destination for the copied files.
cp Syntax:
cp Options:
Option | Description |
---|---|
-a | Preserve ownership and mode |
-i | Prompt before overwrite |
-r | Copy directories recursively |
-u | Copy only when the source file is newer than the destination file |
-v | Verbose mode |
Parameters:
Parameter | Description |
---|---|
source | The file or directory to be copied |
destination | The destination path for the copied file/directory |
cp Command Usage Examples:
Copy a File to a Specific Destination
Copies the file “file.txt” to the specified destination “/home/user/Documents/“.
Copy Multiple Files to a Directory
Copies multiple files (file1.txt, file2.txt, file3.txt) to the directory “/home/user/Documents/“.
Recursive Copy of a Directory
Recursively copies all files and subdirectories from “source_directory” to “destination_directory”.
Copy a File and Preserve Timestamps
Copies the file “file.txt” to “new_file.txt” while preserving the original file’s timestamps.
Quiet Copy (Suppress Output)
Copies the file “file.txt” to “/home/user/Documents/” quietly without displaying the copied file’s details.
How do I copy a file in Linux?
To copy a file in Linux using the cp command, use the following syntax:
How do I copy a directory in Linux?
To copy a directory in Linux with all its contents using cp, you can use the following command:
How do I force the copy operation in Linux?
To force the copy operation in Linux, use the -f
option with the cp command. This will overwrite any existing destination file without prompting for confirmation. For example:
How do I preserve file attributes when copying in Linux?
To preserve file attributes like permissions, ownership, and timestamps when copying in Linux, use the -p
option with the cp command. Here’s an example:
How do I copy files interactively in Linux?
To copy files interactively in Linux, you can use the -i
option with the cp command. This will prompt you before overwriting any existing destination files. For example:
How do I copy multiple files to a directory in Linux?
To copy multiple files to a directory in Linux, specify the destination directory at the end of the cp command. Here’s an example:
How do I display the copy progress in Linux?
To display the copy progress in Linux while using the cp command, you can use the pv
command in combination with cp. Here’s an example:
How do I copy files excluding certain patterns in Linux?
To copy files while excluding certain patterns in Linux, you can use the rsync
command with the --exclude
option. Here’s an example:
Applications of the cp command
- Copy files
- Copy directories
- Copy multiple files into a directory
- Make backups of files
- Preserve file attributes such as ownership and timestamps
- Update existing files with newer versions
- Copy files while renaming them
- Copy files across different file systems