Practical Examples of cp Command in Linux to Copy File

Use cp Command in Linux to Copy File Easily

Copying files and directories is one of the routine tasks that a Unix/Linux user undertakes every day. cp command in Linux is a command-line tool that is used to copy files or groups of files or directories. cp is one of the essential Linux commands that you must know. It is actually short for copy and is known as the copy command in Linux. In this article, we will explain how to use the Linux cp command with the help of practical examples.

The cp command is included in both Linux and Unix due to its essential nature. All the cp command does is make a disk image of a file with a different name. The cp command, like many other fundamental Linux commands, has a wide range of options for copying files and directories which is used on all Linux distributions.

If you are going to buy Linux VPS, you need to know how to use basic Linux commands. Copying files/directories from one location to your considered one, copying multiple files and directories, or copying the entire directories into a new location are the most common tasks in Linux that you must know how to run.

Say goodbye to right-clicking on the files you want to copy and going to the target directory to paste them. Let’s get started to review cp command usage in Linux through examples and get ready to use various options with cp command.

How to Use cp Command in Linux Ubuntu, Debian, and CentOS

While working on a Unix/Linux system, you would certainly ask How to copy a file in Linux. cp is the copying files in Linux command that enables users to copy files from one directory to another in Linux. Using cp command in Linux, you can copy/paste your required operations without leaving the terminal. There options you can use with cp command to copy files and directories will be explained in this tutorial.

Linux cp Command Syntax

The cp command has the same syntax as other Linux commands. It is separated, broadly speaking, into two sections: options and arguments.

$ cp [OPTIONS] <SOURCE> <DEST>
$ cp [OPTIONS] <SOURCE-1> <SOURCE-2> ... <DIRECTORY>

You need to have write permission on the destination directory and at least read permission on the source file in order to copy files and directories. If not, an error about permission denied appears.

To understand better the syntax of Copy file content in Linux, take a look at the below explanations:

  • A single file or directory can be the DESTINATION argument, and one or more files or directories can be the SOURCE argument’s arguments.
  • The cp command copies the first file to the second when both the SOURCE and DESTINATION parameters are files. The command creates the file if it doesn’t already exist.
  • The DESTINATION option needs to be a directory when the SOURCE has several files or folders as parameters. The SOURCE directories and files are transferred to the DESTINATION directory in this case.
  • When both the SOURCE and DESTINATION parameters are directories, the first directory is copied into the second directory using the cp command.

Linux cp Command Options

The below table includes the most useful options of cp command in Linux.

OptionDescription
-iGives the user a warning prior to the copy process.
-bMakes a different-named backup of the destination file in the same folder.
-fUsed in situations when the source file lacks write permission to compel the creation of a copy.
-rA directory's contents are replicated recursively.
-lMakes use of hard links rather than file copies.
-sRather than duplicating the files, creates soft links.
-uCopies files only in cases where the destination is older than the source.
-vDisplays details about the procedure that is currently underway.

14 Practical Examples for cp Command in Linux

Stay with us on this tutorial to review the best Linux cp command examples. This helps you get skilled in working with the cp command in Linux and understand its syntax better. In the end, you know How to copy a file in Linux and use the cp command options.

Example 1. Copy a File

Let’s see how to use cp command in Linux to copy a file to your preferred destination. As you guessed, making a copy of a single file is the most popular use case for the cp command.

The following is the syntax for using the cp command to create a copy of a single file:

cp <options> <source_file> <destination_directory>

So, to copy of the file ”opera.txt” to ”destination_directory”, you must run the example copy command as below:

cp -v opera.txt dest_directory/

Example 2. Display Copy Command Advancement

In the previous example, you learned how to copy a File in Linux. To view the progress of the copy command, you must enable the verbose mode. To do this, use -v option which offers diagnostics for each file it processes.

For example, to make a copy of the file-1.txt file, type:

cp -v file-1.txt file-3.txt

Example 3. Copy Multiple Files to the Destination

Multiple files can be copied to a destination to make tasks easier using cp command in Linux. The basic syntax to copy multiple files to a destination directory at the same time is shown below:

cp <options> <file_1> <file_2> <file_3> <destination_directory>

Run the command below when you need to copy multiple files to your considered directory:

cp file1.txt file2.txt file3.txt dest_directory/

Example 4. Copy a Directory to a Destination Directory

To use cp command in Linux to copy a single directory to a new destination, you can use the -r flag.

cp <options> <source_directory> <destination_directory>

you need to use Linux copy file to location to achieve the purpose of this example. Use the following command to copy the ”source_dir” directory including its content.

cp -v -r source_dir/ dest_directory

Example 5. Copy Multiple Directories to a Destination Directory

Similar to how you can transfer multiple files, you can copy multiple directories as well. The following syntax shows how you can copy multiple directories:

cp -r <option> <directory1> <directory2> <directory3> <destination_directory>

The example below explains more:

cp -r -v dir1/ dir2/ dir3/ dest_directory/

Example 6. Maintain File Permissions for the Copy

Typically, when you use the Linux cp command to duplicate a file, the copied file inherits the default permissions that are set for each newly created file. Use the -p flag to maintain the permissions of the original file:

cp <options> -p <source_file> <destination_file>

In this way, you can use cp command to maintain the permissions of for example ”opera.txt” by running:

cp -p -v opera.txt file.txt

Example 7. Create Links to Duplicate a File

Instead of actually copying the file, you might occasionally need to construct a hard link or a symbolic link to the original file. Use the -l flag to establish a hard link and the -s flag to create a symbolic link:

cp <options> -l <source_file> <destination_file>

OR

cp <options> -s <source_file> <destination_file>

To use the cp command in Linux to create a symbolic link for a file ”opera.txt’‘, run:

cp -v -s opera.txt opera1.txt

In this way, you can create a soft link instead of creating a new copy using the -s option.

Example 8. Refrain from Overwriting the Destination File

By default, The cp command in Linux replaces any existing file with the same name in the destination. Use the -n flag to prevent the filename from being overwritten.

cp <options> -n <source_file> <destination_file>

Run the command below to avoid overwriting the file ”opera.txt” while copying:

cp -v -n opera.txt file.txt

Example 9. Overwrite the File with Confirmation

You learned how to prevent the destination file from being overwritten in the previous example. On occasion, though, you would prefer to replace the file destination in a more secure manner. In these situations, you can make the copy operation interactive by using the command’s -i option. This option waits for the user’s approval before overwriting the file and displays a warning message.

To demonstrate this, let’s attempt to replace the current file:

cp -i opera.txt file.txt

When the output asks you for confirmation, you can use ‘y’ to continue or ‘n’ to abort just like other Linux commands and operations.

The cp command’s default non-interactive behavior is not particularly secure. There’s a potential that the user unintentionally overwrites a crucial configuration. Therefore, some Linux versions use the alias command to impose interactive behavior by default:

$ alias cp='cp -i'

Example 10. If the Source is Newer, Overwrite the File.

You learned how to use the interactive mode in the previous example. But occasionally, a user might unintentionally overwrite the more recent file. You can use the -u option, which only tries copy operation if the destination file is missing or the source file is older than the destination, to prevent such error-prone scenarios.

First, make sure the source file’s timestamp is updated:

$ touch -t 10101010 file-1.txt
$ ls -l file-1.txt

In the example above, we have set the file’s timestamp to 10-Oct-2010 by using the touch command‘s -t argument. Now let’s change the destination file’s timestamp to the present time:

$ touch file-2.txt

Let’s now attempt to use the -u option to do a copy operation:

$ cp -u -v file-1.txt file-2.txt

Because the destination file is more recent than the source, no copy operation can be attempted in this instance. Lastly, let’s execute the copy operation by switching the source and destination arguments:

$ cp -u -v file-2.txt file-1.txt

The copy operation will be successful if the source file is newer than the destination.

Example 11. Force Copy of the Source File

Occasionally, the original file lacks the write rights necessary to be copied. In this scenario, you can compel the source file to be copied by using the -f flag:

cp <option> -f <source_file> <destination_file>

To use cp command in Linux for this case, type:

cp -f -v file1.txt file2.txt

Example 12. Remove the Destination File Before Copying

You saw in the last example how to remove the destination file in the event that an error occurs when using it. On occasion, nevertheless, the copy operation must be carried out after the destination file has been removed:

$ cp --remove-destination -v file-1.txt file-2.txt

In this way, you can use cp command in Linux to remove the destination file first and then perform the copy operation.

Example 13. Backup File Before Overwriting

To ensure that the destination file is backed up before being overwritten, you can use the cp command in Linux. Utilize the --backup option, which creates automated backups:

$ cp --backup=numbered -v file-1.txt file-2.txt

Example 14. Rename the File while Copying it

While copying the file to a different location, you can also change its name. This is comparable to text editor “save as” choices. You need to include the path and the new file name in this.

cp source_file destination_directory/new_filename

As you reviewed, using the cp command in Linux to copy files and directories is an easy process. Enter man cp on your terminal to learn more about the possible cp options. Also, you can use the rsync and scp programs to copy files over a network.

Conclusion

In this article, you learned about cp command, and you learned How to use cp command in Linux to copy files and directories. We tried to discuss the most important cp command usages through examples. The examples are the most typical ones that a Linux user, system administrator, or software developer will encounter. Practicing with the examples in this guide is a fast way to learn how to work with cp command in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked.