
The cp command in Linux is used to copy files and directories from one location to another. It's also known as copy cp and used in file management, data backup, directories, and configuration files in a file system.
This article explains how to use the cp command in Linux to copy and manage files.
cp Command SyntaxBelow is the basic cp command syntax:
Within the above command, source is the file or directory to copy, and destination is the target location.
cp Command Optionscp CommandCopy a file in your working directory to another directory.
The above command copies file1.txt to the /home/user/backup/ directory.
Output:
Copy multiple files to a specific directory.
The above command copies file1.txt, file2.txt, and file3.txt to the /home/user/backup/ directory.
Output:
Recursively copy a directory and its contents.
The above command copies the documents directory and all included contents to the backup directory.
Output:
Copy a file and output detailed information about the command's processes.
The above command uses the verbose option -v to display detailed information about the file1.txt copying process.
Output:
Copy a file and include a confirmation prompt before overwriting the existing file.
The above command outputs a confirmation prompt before overwriting an existing file to the destination directory.
Output:
Copy a file if the source is different from the destination or does not exist.
The above command copies file1.txt only if it's newer or the destination file does not exist.
Output:
Forcefully copy a file.
The above command overwrites the existing file in the destination without any confirmation prompt.
Output:
Copy a file and preserve the original file attributes.
The above command copies file1.txt while preserving its mode, ownership, and timestamps.
Output:
Enable archive mode while copying.
The above command recursively copies the /home/user/new to /home/user/backup/ and preserves the original directory attributes.
Output:
Copy all files.
The above command copies all files with a .txt extension to the /home/user/new directory.
Output:
Interactively copy files and display the command processes.
The above command combines interactive and verbose modes to enable confirmation prompts before overwriting the destination file and displaying detailed information.
Output:
Copy a file and preserve all existing attributes.
The above command preserves all file attributes and provides a verbose output about the copy process.
Output:
Copy newer files with a verbose output about the copy process.
The above command copies newer files file1.txt file2.txt to the /home/user/backup directory with a verbose output about the copy command processes.
Output:
Combine and copy files using the find and xargs commands.
The above command finds all .txt files in the active working directory and copies the results to the /home/user/backup/ directory.
Output:
Copy files and create a backup of the existing files.
The above command copies file1.txt to the /home/user/backup/ directory and creates a backup of any existing files.
Output:
Use rsync to enable efficient copying to compare the source and destination files.
The above command uses rsync to compare and copy the source directory /home/user/documents/ files with the destination directory /home/user/backup/. rsync is a powerful tool that works similar to the cp command and enables the copying and verification of files while preserving the source attributes.
Output:
You have used the cp command in Linux to copy files, and directories while preserving the original file attributes. For more command options, run the man cp command to view the cp manual page.
0 Comments
Be the first to comment and share your perspective with the community.