BASH Programming

Mastering the Linux Copy File Command With Examples

On any operating system, copying of files and directories is one of the most common actions a user makes. On the Linux command line, you can learn the proper usage of the cp command to customize and master file-copying according to your needs. In this article, we will explain the usage of the Linux cp command by presenting some useful examples.

We have run the commands and examples mentioned in this article on a Debian 10 Buster system but you can replicate them on almost all Linux distros.

The Linux command line, the Terminal, can be easily accessed through the Application.  Launcher search as follows:

Example 1: Copying a single file to a target directory

The simplest use of the cp command is to copy a single source file to a target directory. Here is the syntax you can use to do so:

$ cp sourcefile /target/directory

Example:

In this example, I am copying a file named sample_file.txt to my Documents folder:

Later, I verified the presence of the file in the target folder through the ls command.

Example 2: Copying multiple files to a target directory

With the cp command, you can copy multiple files to a target directory by using the following syntax:

$ cp sourcefile1 sourcefile2 sourcefile3...  /target/directory

Example:

In this example, I am copying two sample files to my Documents folder through the cp command:

Later, I verified the presence of these files in the target folder through the ls command.

Example 3: Copying of files in an interactive manner

If a file with the same name exists in your target folder, you can use the cp command to prompt you before overwriting the target file. You can use the -i switch in the following manner:

$ cp -i sourcefile /target/directory

Example:

In this example, a file named sample_file.txt already exists in my Documents folder, therefore the cp command, with the -i flag, prompts me if I want to overwrite it. The file will be overwritten if i enter y at this prompt.

Example 4: Copying files along with a Verbose output

A verbose output prints what the command is doing. This can be incorporated in the cp command with the -v switch as follows:

$ cp -v sourcefile /target/directory

Example:

You can see in the following example how the cp command prints a verbose output while copying a command:

Example 5: Copying a directory recursively

Using the -r option with the cp command recursively copies all files and folders in a directory to another location. This is how you can use the cp command in this scenario:

$ cp -r directory1 directory2

Example:

In the following example, the entire folder 1, with all its files, will be copied to folder2.

I later verified the contents of folder2 through the ls command. The folder2 contains a copy of the source folder now.

Example 6: Copying a directory recursively while keeping an archive

By using the -a switch with the cp command, you can do two things at once:

  • Copy files of a directory recursively to another directory
  • Keep the permissions, time stamp, symbolic links, and all such properties intact while copying files.

This is how you can use the cp command in this scenario:

$ cp -a directory1 directory2

Example:

In the following example, all entire folder 1, with all its files, will be copied to folder2. Also, the files will be archived as verified later through the ‘ls -l command’.

Example 7: Copy a file in case it is newer than the target file

Sometimes you want to copy a file to the target directory only if it is newer than the target file. This can be done by using the -u switch with the cp command:

$ cp -u sourcefileORsourcedirectory /target/directory

Example:

In this example, I edited a file sample_file.txt from my source folder. These files were already copied to the target folder before i edited the file. This can be seen in the following output of the ‘ls -l’ command:

Now, when I copied the source folder to the target folder, the verbose output verified that only that file will be copied that was edited by me.

Example 8: Copy a file but do not overwrite if a target file already exists

You can tell the cp command to not overwrite the file if it already exists in the target location. This can be done through the -n switch as follows:

$ cp -n sourcefile /target/directory

Example:

In this example, I first tried copying a file to the target location where it already existed. The -i switch prompted me if I want to overwrite it.

In the second command, I added the -n switch. It ignored the -i switch but did not overwrite the already existing file in the target directory.

Example 9: Create a symbolic link to a file in target directory

With the -s switch in the cp command, you can create a symbolic link to the source file in the target folder instead of copying the source file.

$ cp -s sourcefile /target/directory

Example:

In the following example, I created a symbolic link to the file sample_file.txt in the target folder.

I later verified the presence of the symbolic link in the target folder through the ‘ls -l’ command.

Example 10: Create a hard link to a file in target directory

When you create a hard link to a file in another folder, the file is not copied to the target folder; instead, a hard link to the source file is created. The inode number of both the source file and the copied hard link is the same. This is how to create a hard link:

$ cp -l sourcefile /target/directory

Example:

In this example, I created a hard link to the source file in the Documents folder.

Through the ‘ls -il’ command, I verified that both the original file and the hard link have the same inode numbers.

This was all you needed to know in order to master the cp command in Linux. By using a combination of these switched, you can make file copying much more customizable.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.