
Introduction
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 Syntax
Below is the basic cp command syntax:
$ cp [options] source destination
Within the above command, source is the file or directory to copy, and destination is the target location.
cp Command Options
| Option | Description | 
|---|---|
| -a | Archive mode, copies files recursively and preserves attributes such as symbolic links, file permissions, and timestamps. | 
| -r | Recursive, copies directories and the included contents. | 
| -v | Verbose mode, displays detailed information about the copying process. | 
| -i | Interactive mode, enables confirmation prompts before overwriting files. | 
| -u | Copies files only when the source file is newer than the destination file or when the destination file is missing. | 
| -f | Forcefully overwrites existing files without a confirmation prompt. | 
| -p | Preserve the existing file attributes, such as mode, ownership, and timestamps. | 
Practical Examples of the cp Command
- Copy a file in your working directory to another directory. console- $ cp file1.txt /home/user/backup/ - The above command copies - file1.txtto the- /home/user/backup/directory.- Output:  
- Copy multiple files to a specific directory. console- $ cp file1.txt file2.txt file3.txt /home/user/backup/ - The above command copies - file1.txt,- file2.txt, and- file3.txtto the- /home/user/backup/directory.- Output:  
- Recursively copy a directory and its contents. console- $ cp -r /home/user/documents /home/user/backup/ - The above command copies the - documentsdirectory and all included contents to the- backupdirectory.- Output:  
- Copy a file and output detailed information about the command's processes. console- $ cp -v file1.txt /home/user/backup/ - The above command uses the verbose option - -vto display detailed information about the- file1.txtcopying process.- Output:  
- Copy a file and include a confirmation prompt before overwriting the existing file. console- $ cp -i file1.txt file2.txt - 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. console- $ cp -u file1.txt /home/user/backup/ - The above command copies - file1.txtonly if it's newer or the destination file does not exist.- Output:  
- Forcefully copy a file. console- $ cp -f file1.txt file2.txt - The above command overwrites the existing file in the destination without any confirmation prompt. - Output:  
- Copy a file and preserve the original file attributes. console- $ cp -p file1.txt file2.txt - The above command copies - file1.txtwhile preserving its mode, ownership, and timestamps.- Output:  
- Enable archive mode while copying. console- $ cp -a /home/user/new /home/user/backup/ - The above command recursively copies the - /home/user/newto- /home/user/backup/and preserves the original directory attributes.- Output:  
- Copy all files. console- $ cp *.txt /home/user/new - The above command copies all files with a - .txtextension to the- /home/user/newdirectory.- Output:  
Advanced Examples and Combined Uses
- Interactively copy files and display the command processes. console- $ cp -iv file1.txt /home/user/backup/ - 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. console- $ cp -pv file1.txt /home/user/backup/ - 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. console- $ cp -uv file1.txt file2.txt /home/user/backup/ - The above command copies newer files - file1.txt file2.txtto the- /home/user/backupdirectory with a verbose output about the copy command processes.- Output:  
- Combine and copy files using the - findand- xargscommands.console- $ find . -name "*.txt" -type f -print | xargs cp -t /home/user/backup/ - The above command finds all - .txtfiles 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. console- $ cp --backup file1.txt /home/user/backup/ - The above command copies - file1.txtto the- /home/user/backup/directory and creates a backup of any existing files.- Output:  
- Use - rsyncto enable efficient copying to compare the source and destination files.console- $ rsync -avh /home/user/documents/ /home/user/backup/ - The above command uses - rsyncto compare and copy the source directory- /home/user/documents/files with the destination directory- /home/user/backup/.- rsyncis a powerful tool that works similar to the- cpcommand and enables the copying and verification of files while preserving the source attributes.- Output:  
Conclusion
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.