cp is a utility command to copy file or directory. It’s invoked at least with two arguments; the source file or directory, and the destination to copy the source. It can be used to copy one or multiple source of files and directories.
General syntax for
$ cp [OPTIONS] [SOURCE...] [DESTINATION...]
Duplicating a file
cp foo.txt bar.txt
This command duplicates the file foo.txt, into a new file call bar.txt.
Duplicating a directory (and its contents)
cp -R foo-folder bar-folder
-R option, this command will duplicate the folder foo-folder and its content to bar-folder.
Show the copying progress
cp -v foo.txt bar.txt
-v parameter shows us what the
cp command is doing (progress) in the background. This paramater is usually used when copying large number of files is involved.
Confirmation to overwrite a file
cp -i foo.txt bar.txt
Explaination: If a file bar exist when you attempt to copy foo to a new file call bar, the
-i parameter will ask you for confirmation if you want to overwrite the existing file. Enter
y to overwrite said file, or
n to cancel.
Copying multiple files to a directory
cp foo.txt bar.txt baz
This command will duplicate a copy of foo.txt and bar.txt in, into the baz directory. The baz directory must first exist in order for command to work.