cp Command: 10 Examples with DetailsThe 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
mkdir to create it.ls -l to check permissions and timestamps.sudo if copying to system directories or files with restricted permissions.cp with other commands like chown or chmod to manage ownership and permissions after copying.