cp
cp linux command cheatsheet by Thamizhiniyan C S
Introduction
The cp
command is used to copy files and directories.
Syntax
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
Important Flags
Flag | Description |
---|---|
| Preserve the source's metadata, such as creation date, permissions, and extended attributes. |
| Create backups for destination files. The backup file has the (~) suffix unless --suffix is used. |
| If the destination file/directory already exists, replace it with the source. |
| Ask before overwriting the destination with the source. |
| Create hard links instead of new files. The destination file will have the same inode attribute as the source. |
| Do not overwrite the destination file if it exists. |
| Copy the source directory recursively. |
| Provide a custom suffix for the backup file. |
| Specify a directory for the source files. |
| Replace files only if they satisfy the update condition. The short option replaces files if the destination is older than the source. The long option lets the user specify the condition (all, none, or older). |
| Output information about the copying process. |
| Preserves file characteristics (modification time, access time, ownership, permission-bits). |
| Uses the * wildcard to represent all files and directories matching a pattern. |
| Show help. |
| Show version information. |
Examples
Command | Description |
---|---|
Copy file-1.txt, file-2.txt, and file-3.txt to the test-dir directory. | |
Copy all files starting with file- to test-dir using the * wildcard. | |
Copy file-1.txt to test-dir with verbose output. Prompt to overwrite if the file exists. | |
Copy file-1.txt to test-dir, creating a backup file with .bak extension. Show verbose output. | |
Copy contents of Src_file to Dest_file. Overwrites Dest_file if it exists. | |
Copy contents of a.txt to b.txt. Overwrites b.txt if it exists. | |
Copy contents of a.txt to c.txt. Overwrites c.txt if it exists. | |
Copy Src_file1, Src_file2, and Src_file3 to Dest_directory. Overwrites existing files in Dest_directory. | |
Copy a.txt, b.txt, and c.txt to new/ directory. Overwrites existing files in new/. | |
Recursively copy all files from Src_directory to Dest_directory. | |
Interactive copying: prompts before overwriting Destination_file. Press 'y' to confirm overwrite. | |
Interactive copy of a.txt to b.txt. Prompts to confirm overwrite of b.txt. | |
Force copying: deletes Destination_file if unable to write, then copies Source_file. | |
Forceful copy of a.txt to b.txt. Overwrites b.txt without prompting. | |
Recursive copy of Directory_name1 contents to Directory_name2. | |
Recursively copy contents of source_directory to dest_directory. | |
Preserve copying: retains original file characteristics (timestamps, ownership, permissions) in Destination_file. | |
Preserves characteristics of a.txt when copying to c.txt. | |
Copy all .txt files to Destination Directory or file using * wildcard. | |
Copy all .txt files to Folder1 using * wildcard. |
Last updated