Hosting or Servers Logo

Copy a File Using Linux cp Command

Linux cp Command

The Linux command to copy files or directories is the two letters cp. In its simplest form to create a copy of a file you do the following:

cp Source_File Destination_File

If the destination file does not exist, then it will be created. The example in Figure 1. shows our first file test4.txt which we then copy into test5.txt. The file test5.txt did not exist so it was created during the copy process.

ls Directory to Show Contents
Linux cp Command File to File

Figure 1.

To create a copy of a file in a directory you do the following:

cp Source_File Destination_Directory

In Figure 2. we copy our file test4.txt into the directory bob then check it completed with the ls command.

Linuc cp Command File to Directory

Figure 2.

Some other cp command uses


cp Source_File1 Source_File2 Destination_Directory

Copy multiple files into a directory

cp -R Source_Directory Destination_Directory

Copy the contents of source directory recursively into destination directory.

cp -b Source_File Destination_File

If the destination file exists, then create a copy/backup of the source file with a different name

cp -f Source_File Destination_File

Will try to force a copy of the file by deleting the destination file of the same name.

cp - - help

Linux cp command help page

You Are Leaving Us

This link is being provided as a convenience and for informational purposes only it is not an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual. bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links.