Linux cp Command: 10 Examples with Details

The cp (copy) command in Linux is used to copy files and directories from one location to another. Below are 10 practical examples demonstrating various uses of the cp command, complete with explanations and scenarios.

Example 1: Copy a Single File

Copy a file to a new location.

cp document.txt /home/user/backup/

Explanation: This command copies document.txt from the current directory to /home/user/backup/. The destination must exist, or an error occurs.

Use Case: Backing up an important file to a backup directory.

Example 2: Copy Multiple Files

Copy multiple files to a directory using a wildcard.

cp *.txt /home/user/text_files/

Explanation: The *.txt wildcard matches all files with a .txt extension in the current directory and copies them to /home/user/text_files/.

Use Case: Moving all text files to a dedicated folder for organization.

Example 3: Copy a Directory Recursively

Copy a directory and all its contents.

cp -r projects /home/user/backup/

Explanation: The -r (recursive) flag copies the projects directory and all its contents, including subdirectories, to /home/user/backup/.

Use Case: Backing up an entire project directory with all its files and subfolders.

Example 4: Copy with a New Name

Copy a file to a new location with a different name.

cp document.txt /home/user/backup/document_copy.txt

Explanation: This command copies document.txt to /home/user/backup/ and renames it to document_copy.txt in the destination.

Use Case: Creating a copy of a file with a modified name for version control.

Example 5: Preserve File Attributes

Copy a file while preserving its permissions, timestamps, and other attributes.

cp -p document.txt /home/user/backup/

Explanation: The -p (preserve) flag ensures that the copied file retains its original permissions, timestamps, and ownership.

Use Case: Copying configuration files where metadata like permissions is critical.

Example 6: Force Overwrite Without Prompt

Copy a file and overwrite the destination without confirmation.

cp -f document.txt /home/user/backup/

Explanation: The -f (force) flag overwrites the destination file if it exists without prompting the user.

Use Case: Automating backups in scripts where user interaction is not possible.

Example 7: Interactive Copy with Prompt

Copy files and prompt before overwriting existing files.

cp -i *.txt /home/user/backup/

Explanation: The -i (interactive) flag prompts the user for confirmation before overwriting any existing files in the destination.

Use Case: Safely copying files to avoid accidentally overwriting important data.

Example 8: Copy Symbolic Links

Copy a symbolic link itself, not the target file.

cp -P symlink_to_file /home/user/backup/

Explanation: The -P flag ensures that the symbolic link symlink_to_file is copied as a link, not the file it points to.

Use Case: Maintaining symbolic links in a backup directory without copying the target files.

Example 9: Update Only Newer Files

Copy files only if they are newer than the destination files.

cp -u *.txt /home/user/backup/

Explanation: The -u (update) flag copies files only if they are newer than the destination files or if the destination files do not exist.

Use Case: Synchronizing files to a backup location without copying unchanged files.

Example 10: Verbose Output for Copy Operations

Display detailed output during the copy process.

cp -v document.txt /home/user/backup/

Explanation: The -v (verbose) flag shows the names of files as they are copied, confirming the operation.

Use Case: Monitoring the progress of a copy operation in scripts or manual tasks.

Additional Notes