8 Best Examples of cp Command in Linux

In the world of Linux, there are several essential Linux Terminal commands that can make your workflow effortless. One such command is the cp command, which helps you quickly copy files and directories on your Linux system. In this article, we will discuss some examples of how to use the cp command in Linux.

cp Command in Linux: Syntax and Options

In Linux, cp stands for “copy” and is used to copy both files and directories, making it an absolute necessity for file management. The cp command can be used by users of all types, thanks to its simple syntax:

cp <options> <source_file> <destination_file>

Some of the common options to pair with the cp command in Linux are:

OptionsDescription
-iPrompts the user before the copy operation takes place
-bCreates a backup of the destination_file in the same folder with a different name
-fUsed to forcefully create a copy of the source file when it does not have write permission
-rRecursively copies a directory and its contents
-lCreates hardlinks instead of copying the files
-sCreates softlinks instead of copying the files
-uCopies files only if the source is newer than the destination
-vShows information about the current operation taking place

Best Examples of cp Command in Linux

Now that you know the syntax of the cp command, let’s look at some examples of how to use this command to copy files and directories in Linux.

1. Copy a Single File to the Destination

This is the most common use case for the cp command, i.e. to make a copy of a single file. The syntax to make a copy of a single file using the cp command is as follows:

cp <options> <source_file> <destination_directory>

For example, to make a copy of the file “test.txt” to “destination_directory/”, use the cp command as:

cp -v test.txt dest_directory/
using the cp command to copy a file in Linux

2. Copy Multiple Files to the Destination

Just like you can make a copy of a single file, you can even use the cp command to copy multiple files to a destination directory:

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

For example, to copy the files file1.txt, file2.txt, file3.txt to the “dest_directory” directory:

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

3. Copy a Single Directory to a Destination Directory

With the -r flag, you can use the cp command to copy the entire directory to a new destination:

cp <options> <source_directory> <destination_directory>

For example, to copy the “source_dir” directory with its contents:

cp -v -r source_dir/ dest_directory

4. Copy Multiple Directories to a Destination Directory

Just like you copy multiple files, much the same way you can copy multiple directories. The syntax to copy multiple directories:

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

For example:

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

5. Preserve File Permissions for Copied File

Normally when you copy a file using the cp command, the copied file will have the file permissions as per the default permissions set for every new file that’s created. To preserve the original file’s permission, use the -p flag:

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

For example, to preserve the permissions of “test.txt”, use the cp command as:

cp -p -v test.txt file.txt

6. Avoid Overwriting the Destination File

By default, the cp command overwrites any pre-existing file in the destination with the same name. To avoid overwriting the filename, use the -n:

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

For example, to avoid overwriting the file “test.txt” while copying:

cp -v -n test.txt file.txt

7. Force Copy of the Source File

Sometimes the source file does not have write permissions to make a copy. In such a case, you can use the -f flag to forcefully make a copy of the source file:

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

For example:

cp -f -v file1.txt file2.txt

Sometimes, you may need to create a hard link or a symbolic link of the source file instead of actually copying the file. To create a hard link, use the -l flag and to create a symbolic link use the -s flag:

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

OR

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

For example, to create a symbolic link for the file “test.txt”:

cp -v -s test.txt test1.txt
Comments 1
Leave a Reply

Loading comments...