How to use cp command in Linux

The cp command came from the word copy. This command is used to copy files and directories on Unix and Linux systems. As a Linux user, you will use the cp command almost every time you log into your machine.

In this article, I will explain how to use the cp command with multiple examples.

How to use cp Command

The general syntax for the cp command is given below –

cp [OPTIONS] SOURCE DESTINATION

The SOURCE can be single or multiple files or directories whereas the DESTINATION can be a single file or directory. It is also obvious that if the SOURCE is a directory then the DESTINATION should be a directory. The OPTIONS use some flags to enhance the functionality of the cp command.

There are some basic rules you have to remember when using the cp commands –

  • When the SOURCE and DESTINATION both arguments are files then it copies the SOURCE file to the DESTINATION file. And if the file already existed in the DESTINATION folder then it overwrites the DESTINATION file with the SOURCE file.
  • when the SOURCE have multiple files or directories or both file and directories as its arguments then the DESTINATION must be a directory. In this case the SOURCE files and directories are copied to the DESTINATION directory.
  • when the SOURCE and DESTINATION both are directories then the cp command copied the SOURCE directories and all its contents to the DESTINATION directories.

One important thing to note here is that in order to copy a file or folder you need to have read permission for the SOURCE and write permission for the DESTINATION.

cp – Copy Files is Linux

You will have three different scenarios when copying a file – copying a file within the current directory you are in, copying a file from the current directory to another directory, and copying a file from a different directory to another directory.

You can copy files within your current directory by simply typing a command like below –

cp file.txt file_backup.txt

If you want to copy a file from the current directory to another directory you have to type the full path of the DESTINATION like below –

cp file.txt /backup/file.txt

You can also change the SOURCE filename on the DESTINATION folder like below –

cp file.txt /backup/file_01.txt

If you want to copy a file from a different directory to another directory then you have to type the full path of the SOURCE directory and the DESTINATION directory.

cp /Downloads/Despacito.mp3 /Musics/Despacito.mp3

If the SOURCE and DESTINATION have some files with the same name then the cp command overwrites the files in the DESTINATION folder with the SOURCE files. You can stop the files from being overwritten by the SOURCE file using the flag -n

cp -n file.txt /backup/file1.txt

Use the flag -i to prompt for confirmation before overwriting.

cp -i file.txt /backup/file1.txt

If you want to copy only when the SOURCE file is newer than the DESTINATION file or when the DESTINATION doesn’t contain the SOURCE file, then use the flag -u

cp -u file.txt /backup/file.txt

cp – Copy Directories in Linux

When the SOURCE is a directory you need to use the flag -r or -R to copy the SOURCE to the DESTINATION. The general syntax to copy a directory is as follows:

cp -r copy/from/directory copy/to/directory

For example, the below command will copy the Pictures directory including all its files and subdirectories to the Pictures_backup directory.

cp -r Pictures Pictures_backup

If you want to copy only the files and subdirectories but not the SOURCE directory then use the flag -T with -r or -R.

cp -rT Pictures Pictures_backup

Copy Multiple Files and Directories in Linux

You can use multiple arguments as a SOURCE to copy multiple files and directories to the DESTINATION directory. The below example will copy all three text files to the backup directory.

cp file1.txt file2.txt file3.txt /backup/

If you include any directory in the SOURCE then you have to use the -r or -R as a flag.

cp -r file1.txt file2.txt dir1 dir2 /backup/

Leave a Comment