The cp
command in Linux is used to copy files and directories from one location to another. It stands for “copy” and is an essential tool for managing and duplicating files. The basic syntax of the cp
command is as follows:
cp [options] source destination
Here, source
represents the file or directory you want to copy, and destination
is where you want to copy it to. Here’s a detailed explanation of the command and its options:
Options:
-r
or--recursive
: This option is used when you want to copy directories and their contents recursively. Without this option, thecp
command will not copy directories.-i
or--interactive
: This option prompts you before overwriting an existing destination file. It’s useful to prevent accidental overwrites.-u
or--update
: This option copies only when the source file is newer than the destination file or when the destination file is missing.-v
or--verbose
: This option displays detailed information about the files being copied, showing each file as it’s copied.-p
or--preserve
: This option preserves the original file attributes such as permissions, timestamps, and ownership when copying.-d
or--no-dereference
: This option is used to avoid dereferencing symbolic links; it copies symbolic links themselves instead of their targets.--parents
: This option preserves the directory structure when copying files, creating any necessary parent directories in the destination.--backup
: This option makes a backup copy of each existing destination file before overwriting it.
Source and Destination:
source
: This is the path to the file or directory you want to copy. It can be either an absolute path or a relative path.destination
: This is the path to the location where you want to copy the source. It can also be either an absolute path or a relative path. If the destination is a directory, the source file or directory will be copied into it.
Examples:
Copy a file to a different location:
Copy a directory and its contents recursively:
cp -r directory/ /path/to/destination/
Copy multiple files to a directory:
cp file1.txt file2.txt /path/to/destination/
Copy with preserving attributes and prompting for overwrite:
cp -i -p file.txt /path/to/destination/
Copy a file, preserving directory structure:
cp --parents file.txt /path/to/destination/
Copy and create backup copies of existing files:
cp --backup=numbered file.txt /path/to/destination/
Remember that improper use of the cp
command can lead to data loss, so be cautious, especially when using options like -i
and -u
. Always double-check your commands before pressing Enter.
Additional Examples:
Copy a File to a Different Location:
cp file.txt /path/to/destination/
Copy Multiple Files to a Directory:
cp file1.txt file2.txt /path/to/destination/
Copy Files and Preserve Attributes:
cp -p file.txt /path/to/destination/
Copy a File and Prompt Before Overwriting:
cp -i file.txt /path/to/destination/
Copy Only Newer Files:
cp -u newer.txt /path/to/destination/
Copy Files Verbosely (Display Detailed Information):
cp -v file1.txt file2.txt /path/to/destination/
Copy Files and Create Backup Copies:
cp --backup=numbered file.txt /path/to/destination/
Copy Symbolic Links (Not Dereferencing):
cp -d symlink.txt /path/to/destination/
Copy a File and Preserve Parent Directories:
cp --parents dir/file.txt /path/to/destination/
Copy a Directory and Its Contents to a New Directory:
cp -r directory/ new_directory/
Copy Files Using Wildcards:
cp *.txt /path/to/destination/
Copy Hidden Files:
cp -r source_directory/. destination_directory/
Copy Files Between Remote Servers (using SCP):
cp source_file remote_username@remote_host:/path/to/destination/
Copy Files Between Remote Servers (using SSH and tar):
tar cf - source_directory/ | ssh remote_host "cd /path/to/destination/ && tar xf -"
Copy Files Using Absolute Paths:
cp /absolute/path/to/source/file.txt /absolute/path/to/destination/
Copy Files with Progress Indicator (using rsync
):
rsync -av --progress source/ /path/to/destination/
These examples should provide you with a variety of scenarios where the cp
command can be used to copy files and directories in Linux. Remember to adjust the paths and options according to your specific needs.