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.
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.
Figure 2.
Command | Result |
---|---|
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 |