The cp command
Yaser Rahmati | یاسر رحمتی
The cp
is a command-line utility for copying files and directory. cp
stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name. The cp
command requires at least two filenames in its arguments.
Examples:
To copy the contents of the source file to the destination file.
If the destination file doesn't exist then the file is created and the content is copied to it. If it exists then the file is overwritten.
To copy a file to another directory specify the absolute or the relative path to the destination directory.
To copy a directory, including all its files and subdirectories
The command above creates the destination directory and recursively copies all files and subdirectories from the source to the destination directory.
If the destination directory already exists, the source directory itself and its content are copied inside the destination directory.
To copy only the files and subdirectories but not the source directory
Exercise
Syntax:
The general syntax for the cp
command is as follows:
The first and second syntax is used to copy Source file to Destination file or Directory. The third syntax is used to copy multiple Sources(files) to Directory.
Some useful options
-i
(interactive)i
stands for Interactive copying. With this option system first warns the user before overwriting the destination file.cp
prompts for a response, if you pressy
then it overwrites the file and with any other option leave it uncopied.
-b
(backup) : With this option cp command creates the backup of the destination file in the same folder with the different name and in different format.
Additional Flags and their Functionalities:
Short Flag | Long Flag | Description |
---|---|---|
-i | --interactive | prompt before overwrite |
-f | --force | If an existing destination file cannot be opened, remove it and try again |
-b | - | Creates the backup of the destination file in the same folder with the different name and in different format. |
-r or -R | --recursive |
|
-n | --no-clobber | do not overwrite an existing file (overrides a previous |
-p | - | preserve the specified attributes (default: mode, ownership, timestamps), if possible additional attributes: context, links, xattr, all |
Last updated