cp
A copy of a file might be needed for backup purposes or the user may need to use an existing file as the basis for a new document. It can be restored from the backup if the original file is accidentally removed. These tasks are complete with the cp command.
Syntax
First Form
cp file1 file2
in the syntax file1 is the source file and file2 is the target file.
Second Form
cp file1 file2 file3 .... d1
in the syntax file1, file2, file3.. are the source files and d1 is the target directory.
Third Form
cp -R s1 d1
in the syntax s1 is the source directory and d1 is the target directory here a -R copies all files and subdirectories.
Practice 1
Given example display the cp command is used to copy the contents of one file into another.
# cp newfile file3
#
The given syntax will cause the file named newfile to be copied into the file named file3. The # sign after the cp command denotes in which the command has been executed successfully.
Practice 2
The subsequent example display how the cp command can be used to copy files into a directory.
# cp file1 file2 file3 personal
# ls personal
file1 file2 file3 salary
From the given above example it can be seen in which the files file1, file2, file3 have been copied to the directory personal.
Note: its contents are erased and are overwritten with the contents of the source file if the target file is an ordinary file and it already exists. The source file is copied to that directory with the similar name as the source if the target file is a directory. Within one cp command, only one file can be copied to another file but one or more files can be copied to a directory at the similar time.