cp command in Linux with options and example

We will learn cp command in Linux with options and examples. This command is mainly used to copy the files and folder into another directory.

We can take the Backup, make the command line interactive while using the cp command in Linux or Unix Terminals.

We can also preserve a file and folders permission while using the cp command in Linux

Q. How to COPY a SINGLE FILE into ANOTHER DIRECTORY.

So, as we know the cp command is used to copy the files and directory in Linux/UNIX. Let’s see how to copy the single file.

Command:
# cp single_file tastethelinux/ 

So, we have copied the single_file into the tastethelinux directory, Let’s have a look at how we can move multiple files.

Q. How to COPY a MULTIPLE FILE into ANOTHER DIRECTORY.

Command:
# cp file1 file2 file3 tastethelinux/ 

So, we have copied three files “file1, file2, and file3 into the tastethelinux directory.

Q. How to COPY the SAME PREFIX AND SUFFIX in a DIRECTORY.

So, for the same name like file1, file2, file3, and so on. And we have to copy this into the exam folder.

So, we will use the wildcard because as you see that file word is common, let’s see the example.

Command:
# cp -v file* exam/ 

So, we have used file* and this will move all prefix which starts with a file in the exam directory.

Q. How to COPY a DIRECTORY into ANOTHER DIRECTORY.

So, we get an error while copying the directory tastethelinux into exam directory “cp: -r not specified; omitting directory ‘tastethelinux’ “.

That means it will not copy the directory and we have to use -r option to copy the folder. So, let’s try this.

Command:
# cp -vr tastethelinux exam/ 

So, -r option is used to copy the directories recursively.

Also, we can see that we have used -v option, it is a verbose option which is used to print the message.

Q. How to take BACKUP while COPYING a FILE into DIRECTORY.

So, we will now see how to take the Backup of the file this will only work when we have the two files with the content. Let’s look at the example.

Command:
# cp -b first_file second_file 

So, there are two files in the BACKUP folder, the first_file and second_file, you can see the content into the above image.

Now we have to copy all the content of first_file into the second_file but the condition is we want a backup of second_file.

So, let’s use -b option to take backup and copy your content into the second_file, also you can check the yellow highlighted part in the image “second_file~”.

So, you can see the content the second_file it is same as the first_file.

But what if we didn’t like the tilde(~) sign of the backup file. So let’s use -S option is it used for Suffix.

Command:
# cp -S _bkp -b first_file second_file 

So, we can see that we have used a -S option with Suffix _bkp, and instead of using tilde(~) sign now, our file has the name single_file_bkp.

Suppose we don’t want to copy the file or a directory into another directory, so we can make a HARD link of that file or a folder by using -l option.

Command:
# cp -l single_file ~/BACKUP/ 

So, we have created a hard link of a single_file into a BACKUP directory.

HARD LINK will create the same file or folder and if you make the changes then it will automatically reflect the changes in your HARD LINK file or a folder.

So, we have added some content into BACKUP/single_file and it got reflected into the tastethelinux/single_file because we have created a Hard Link.

Q. How to create SOFT LINKS instead of COPYING a FILE/DIRECTORY into DIRECTORY.

SOFT LINK is like the shortcuts, but when we use the SOFT LINK with cp command we are only able to create in a CURRENT DIRECTORY.

Command:
# cp -s single_file linked_file 

So, we have used -s option to create a soft link of the file single_file.

Q. How to make OUTPUT INTERACTIVE while COPYING a FILE OR FOLDER into DIRECTORY.

This will make our command-line interactive for that we will use a -i option with cp command.

Command:
# cp -i first_file second_file 

So, as we can see that both files contain some content. when we will -i option, the terminal makes the prompt whether you want to overwrite the content in that file.

If you “type y” then it will overwrite the file and if you will “type n” then it will come out from the terminal.

Q. How to IGNORE EXISTING FILES in DESTINATION DIRECTORY.

Command:
# cp -v -n file3 single_file BACKUP/ 

So, we have used -n option and seen that only file3 file is copied into the BACKUP/ folder.

Because the single_file is already present into the BACKUP directory.

Q. How to UPDATE EXISTING FILE in DESTINATION DIRECTORY.

Command:
# cp -v -u single_file file3 BACKUP/ 

So, -u option will copy the file, if the file is missing or if the files are newer(timestamp) than the destination file.

On the above image, you can see the output, it is just copied the file3 into the BACKUP directory and not the single_file.

Because it is older than the destination file, you can see the timestamp of the file in the image.

Q. How to PRESERVE PERMISSION of FILE or FOLDER in DESTINATION DIRECTORY.

To preserve the permission of files and folder we will use -p option. Let’s see the example.

Command:
# cp -p permission_file BACKUP/ 

So, -p option is used to copy the permission_file into Backup Directory and the same -p option we can use for the folder also.

A Cheat Sheet of cp command in Linux
Q. How to COPY a SINGLE FILE into ANOTHER DIRECTORY.

Syntax:
# cp [source_file] [destination_folder]

Example:
#cp single_file tastethelinux/
Q. How to COPY a MULTIPLE FILE into ANOTHER DIRECTORY.

Syntax:
# cp [source_file1 source_file2 source_file3 ...] [destination_folder]

Example:
#cp file1 file2 file3 tastethelinux/
Q. How to COPY the SAME PREFIX AND SUFFIX in a DIRECTORY.

Syntax:
# cp [SUFFIX/PREFIX] [destination_folder]

Example:
# cp -v file* exam/
# cp -v *.txt exam/
# cp -v *tastethelinux* exam/
Q. How to COPY a DIRECTORY into ANOTHER DIRECTORY.

Syntax:
# cp -r [folder_to_copy] [destination_folder]

Example:
# cp -r tastethelinux exam/
Q. How to take BACKUP while COPYING a FILE into DIRECTORY.

Syntax:
# cp -b [file_to_copy] [file_to_be_copied]

Example:
# cp -b first_file second_file
Q. How to take BACKUP with SUFFIX while COPYING a FILE into DIRECTORY.

Syntax:
# cp -S [suffix] -b [file_to_copy] [file_to_be_copied]

Example:
# cp -S .bkp -b first_file second_file
Q. How to create HARD LINKS instead of COPYING a FILE/DIRECTORY into ANOTHER DIRECTORY.

Syntax:
# cp -l [file_to_create_a_HARD_LINK] [Destination_folder]

Example:
# cp -l single_file ~/BACKUP/
Q. How to create SOFT LINKS instead of COPYING a FILE/FOLDER.

Syntax:
# cp -s [file_to_create_a_Soft_link] [name_of_the_Linked_file]

Example:
# cp -s single_file linked_file
Q. How to make OUTPUT INTERACTIVE while COPYING a FILE OR FOLDER into DIRECTORY.

Syntax:
# cp -s [file_to_copy] [Destination_folder]

Example:
# cp -i single_file Backup/
Q. How to IGNORE EXISTING FILE/FOLDER in DESTINATION DIRECTORY.

Syntax:
# cp -n [Source_file] [Destination_folder]

Example:
# cp -n file3 single_file BACKUP/
Q. How to UPDATE EXISTING FILE in DESTINATION DIRECTORY.

Syntax:
# cp -u [Source_file] [Destination_folder]

Example:
# cp -u single_file file3 BACKUP/
Q. How to PRESERVE PERMISSION of FILE or FOLDER in DESTINATION DIRECTORY.

Syntax:
# cp -p [Source_file] [Destination_folder]

Example:
# cp -p sample_file BACKUP/

[wp_quiz id=”2288″]

Refer the link to practise, Refer the link to learn mv command in Linux.

3 Replies to “cp command in Linux with options and example

Give your valuable time