How to Use the Linux Command cp with 7 Options

In this post, we will take a detailed look at the basic usage of the Linux command cp, as well as various options and usage examples. In the Linux environment, the cp command is a representative command used to copy files and directories.

What is the Linux Command cp?

cp stands for “copy” and is used to copy files or directories. Using this command, you can create a file or directory with the same contents as the original in a different location.

Basic Usage

The basic usage of the Linux cp command is as follows:

cp [options] source_file target_file
ShellScript

For example, to copy file1.txt to file2.txt, you would enter:

cp file1.txt file2.txt
ShellScript

Useful Options

The cp command has various options. Here are some frequently used ones:

-r Option (recursive): Copy Subdirectories

The -r option is arguably the most commonly used option in the cp command. To copy all subdirectories and files, you must use the -r (recursive) option. The following command copies the linux directory to the linux2 directory:

cp -r linux linux2
ShellScript

If you do not use the -r option, the directory itself will not be copied, and as shown below, it kindly notifies you with the message “cp: -r not specified; omitting directory ‘linux'”.

Figure 1. Linux command cp fails to copy the directory
Figure 1. Linux command cp fails to copy the directory

-i Option (interactive): Overwrite Warning

By default, the cp command overwrites the target file without any warning if it already exists. To prevent accidentally overwriting important files, you can use the -i (interactive) option to display a confirmation message.

cp -i file1.txt file2.txt
ShellScript

As shown below, when trying to copy a5.txt to a2.txt, it asks whether to overwrite since a2.txt already exists. Press y to overwrite and n to not copy.

Figure 2. Linux command cp -i option overwrite confirmation
Figure 2. Linux command cp -i option overwrite confirmation

-v Option (verbose): Detailed Output

Using the -v option, you can display detailed information during the copying process. This helps to easily identify any issues that occur during copying.

cp -v file1.txt file2.txt
ShellScript

The result of copying files with the .txt extension from ~/linux to the current directory is shown below. It indicates that the c.txt file was not copied because it lacked read permissions. Even without the -v option, you can know that c.txt was not copied, but it does not show how other files were copied.

Figure 3. Linux command cp -v option showing detailed copy output
Figure 3. Linux command cp -v option showing detailed copy output

-u Option (update): Copy Only Newer Files

The -u option only copies files if the source file is newer than the target file. This can reduce unnecessary copying. When used with the -v option, you can see which files were updated.

cp -u file1.txt file2.txt
ShellScript

This time, we used the -u option along with the -v option. Because the cp command does not show which files were copied by default, using the -v option lets us know whether a file was updated or not.

Below, you can see that h.txt was copied to j.txt because it was older, but a.txt was not copied because it was not older than j.txt.

Figure 4. Linux command cp -u option only copying newer files
Figure 4. Linux command cp -u option only copying newer files

-p Option (preserve): Copy Permissions/Ownership

When copying files in Linux, sometimes the file’s permissions and ownership are important. By default, the cp command only copies the contents, applying default permissions and ownership. However, using the -p option allows you to preserve the original file’s permissions and ownership during copying.

sudo cp -p file1.txt file2.txt
ShellScript

The result of copying the /usr/bin/mount file without options, with the -p option, and with the sudo command and -p option is shown below.

As with setting file ownership with the chown command, you need to use sudo along with cp -p to copy the file’s permissions and ownership.

Figure 5. Linux command cp -p option copying permissions/ownership
Figure 5. Linux command cp -p option copying permissions/ownership

By default, the cp command copies symbolic links as symbolic links. To copy the actual content of the directory instead of the symbolic link, you can use the -L option. Since this function copies subdirectories, you need to use it with the -r option.

cp -rL sym_link real_dir
ShellScript

In the example below, the actual files in the symbolic link shots are copied to the shots2 directory.

Figure 6. Linux command cp -L option copying the actual content of a symbolic link
Figure 6. Linux command cp -L option copying the actual content of a symbolic link

In the image below, it is confirmed that the directory was copied as an actual directory instead of a symbolic link.

Figure 7. Linux command cp -L option confirming the actual content copied
Figure 7. Linux command cp -L option confirming the actual content copied

–backup Option: Creating Backup Files

Using the --backup option, a backup file is created before overwriting if a file with the same name exists.

cp --backup file1.txt file2.txt
ShellScript

When using the backup option, as shown below, the existing file is backed up with a tilde (~) appended to its name, and the new file is copied as a.txt.

Figure 8. Linux command cp --backup option backing up files with the same name
Figure 8. Linux command cp –backup option backing up files with the same name

Precautions

File Names with Spaces

When specifying file names for copying that include special characters or spaces, you should enclose them in quotes. Otherwise, unexpected results may occur.

The following example illustrates this. The command copies a.txt and My file to the Home directory, but because there is no file named My, it shows an error message.

Figure 9. Linux command cp without quoting file names with spaces
Figure 9. Linux command cp without quoting file names with spaces

Therefore, by enclosing the file name in quotes as shown below, you can copy it as desired to the My Home file.

Figure 10. Linux command cp handling file names with spaces by quoting
Figure 10. Linux command cp handling file names with spaces by quoting

File Permission Issues

Sometimes you may need to copy files for which you do not have permission. In such cases, use the sudo command to copy them.

Below, the c.txt file is readable only by the root user. Therefore, the ito user cannot copy the file. In such cases, the sudo command can be used to copy it.

Figure 11. Linux command cp using sudo to copy files without permission
Figure 11. Linux command cp using sudo to copy files without permission

Multiple Options

When you need to apply several options simultaneously, it is convenient to combine them. For example, when using the -r and -L options together, you can write -rL as seen earlier. Of course, you can also write them separately as -r -L.

Summary

The Linux cp command is an essential tool for copying files and directories. From basic usage to various options, you can use it effectively and combine it with other tools to build an efficient work environment. If you have any additional questions or want to know more, feel free to ask!

I hope this guide helps you make the most of your Linux experience.

Reference

Leave a Comment