How to Copy Files and Folders in Linux

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 command:

Duplicating a file
cp foo.txt bar.txt

This command duplicates the file foo.txt, into a new file call bar.txt.

duplicate file
Duplicating a directory (and its contents)
cp -R foo-folder bar-folder

Use the -R option, this command will duplicate the folder foo-folder and its content to bar-folder.

duplicate dir
Show the copying progress
cp -v foo.txt bar.txt

Including the -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.

show process
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.

confirm overwrite
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.

files to dir