Linux/Unix’s cp
command copies files and directories from a source to a destination.
$ cp [options] source destination
The source
keyword refers to a source file, and the destination
keyword refers to a destination file or directory.
If the user needs to copy multiple files to a directory, then the syntax for that is as follows:
$ cp [options] source1 source2 source2 destination
The files referred to by source1
, source2
, and source3
will be copied to the directory referred to by destination
.
Multiple options come with the cp
command. Here we will discuss the more important ones, but running the following command on the terminal will display all the options with their explanations:
$ cp --help
This option warns the user if the destination file already exists and asks for a yes or no option. The destination file is only replaced if the user responds with yes.
The following example creates two files through the touch
command and copies the first file to the second:
This option creates another file as a backup of the destination file if the destination file already exists. The backup file is created in the same folder with a different name and format.
The following example creates copies the first file views the present file through the ls
command. The backup file generates with the name second.txt~
:
This option recursively copies the entire directory to another directory.
The following example creates a directory example
using the mkdir
command and puts two files in it. The terminal shows that copying example
to example2
does not work without the -r
option. The ls
command displays the present files and folder in the current directory.
This command preserves the last modified time, last accessed time, ownership, and the file permission bits of the source file in the destination file.
The following example creates a file and copies it to a second file. The terminal displays that the ownership of both files is identical using the ls -l
command.
Free Resources