Linux Commands Series 5 : Directories and Files 3 cp command


In the previous tutorials, we saw how to create and display the contents of files and directories. An other part of working with files and directories is moving them around, either cutting (moving them permanently from one destination to an other), or copying them. In this tutorial we will look at the command that makes copying files and directories possible : cp

cp : This command stands for "copy", it will make an exact copy of a file or directory into the destination directory/file:

  1. Copy a file into an other file :

cp file1 file2

cp command

we copied file1.txt into file2.txt by executing cp file1.txt file2.txt, and now file2.txt content has been overwritten by file1.txt content as is shown by the last cat command.

2. Copy a file into a directory

cp file destination_directory

cp file1.txt project1

cp file to directory

we copied file1.txt from Linux directory to project1 directory, notice that file1.txt still exists in the Linux directory after being copied successfully

3. Copy a directory :

cp -r directory destination_directory

cp copy a directory

we copied project1 directory inside project3 directory with cp -r project1 project3 (the r option tells the cp command to be recursive, i.e. it will copy everything inside it into the destination), while keeping it in the source directory which is Linux

4. Copy multiple files to a destination directory :

cp file1 file2 file3 destination_directory

cp copy multiple files

5. Copying files interactively :

Say you want to copy a file into a destination which contains a file with the same name, the default behaviour for the cp command is to overwrite the destination file, but that may be dangerous sometimes, in order to keep the destination file use :

cp -i file destination

cp in interactive mode
  1. Linux directory contains a file named file1.txt which was created at 22:31

  2. project1 directory contains a file named file1.txt which was created at 22:58

  3. we used cp -i file1.txt project1 and chose 'y' (for yes when the terminal prompted us, use 'n' if you want to keep the destination file)

  4. now if we execute ls -l project1/file1.txt we notice that file1.txt has been replaced, with a creation date which is the current one

6. Copy with backup option

in 1 we saw that it is possible to copy the content of one file into an other, but what if we want to keep the destination file ? cp makes this behaviour possible by providing the option b, which stands for 'backup'

cp -b file1 file2

cp with backup option

we copied file1 into file3 with cp -b file1.txt file3.txt, and file3 content was overwritten by file1 content, but you can notice that cp has created an other file named file3.txt~, which is the backup file for file3, when we show its content, it contains the text of the original file3 file, this option is thus safer then the bare cp command without b option

7. Copy with preservation of metadata

It is possible to copy a file with preservation of the metadata (time of creation/last access, permissions..) using the option p :

cp -p file1 file2

Let's see the difference with and without -p :

cp with preserve metadata option -p
  • when we copied file4.txt into file5.txt without p option, it copied the contents but without the metadata (notice the new creation datetime for example for file5.txt)

  • when we copied file4.txt into file6.txt with the p option, it copied the contents but with the same original metadata (notice file4.txt and file6.txt have the same creation datetime )

In the next tutorial we will see learn about the command that makes it possible to move a file or directory from one place in our system to an other.

If you found the commands presented here useful, don't forget to share the tutorial with your friends.

Happy Linux learning guys !