Linux cp command with Examples

We offer you a brighter future with FREE online courses - Start Now!!

In this article, you will learn about the cp command or copy command in Linux. We will go through what the cp command is, its operation modes, and the different options used with the copy command. In the end, we shall also be looking into some practical examples of how to use the copy command.

What is ‘cp’ command in Linux?

Cp is an abbreviation for “copy”. This tool or utility is one of the easiest to understand. As the name suggests, this tool helps in copying files. The most basic way to use this tool is to copy single or multiple files.

By definition, the cp is a command-line-based utility that helps in copying files and directories in UNIX and Linux-based operating systems. Though the command can get slightly more intimidating than this. It is also capable of copying files and directories in various special ways.

The working of the copy command is very simple, cp simply creates an image of a file on a disk with a different file name. Cp on its own doesn’t enclose a wide variety of administration scenarios. You can combine cp with many other commands to make the best out of it.

As a Linux user (or any other operating system) copying files and folders is a very common day-to-day task. Just to make things slightly more efficient and simple, the cp command helps in doing the same.

Most Linux users prefer the cp command in the terminal that the GUI!

Operation modes of Linux cp command

The cp command has 3 major principle modes of operation. These modes are expressed by the type of arguments given to the program to copy files. These 3 modes are:

1. Copying a file to another file

2. Copying one or more files to a directory

3. Copying entire directories into a directory

These modes depend on the number and type of arguments passed in the cp command. Let us look into each of the modes, this time based on the arguments:

1. 2 file names (Copying a file to another file)

If you enter 2 file names in the cp command, the tool copies the contents from the 1st file to the 2nd file (never vice versa). If at all the 2nd file you enter does not exist on your computer, the tool will automatically create the second folder and copy the contents of the 1st folder (the first folder must exist always, else it will print an error)

2. More than one argument (Copying one or more files to a directory)

If you enter one or more than 1 argument and also specify the file names followed by a name of a directory, then the cp utility will copy the contents of each of the source files into the destination directory you specify. If at all the destination directory you specify does not exist on your computer, the tool will automatically create a directory with that name.

3. 2 directory names (Copying entire directories into a directory)

If you enter 2 directory names in the command, the cp tool will copy the contents of the first (source) directory into the second (destination) directory. This mode of operation needs an extra option “-R”. This is to indicate the recursive copying of directories. If at all the destination directory you specify does not exist on your computer, the tool will automatically create a directory with that name.

Note: You have seen that if a destination file or directory does not exist, the tool will automatically create it for you. However, if it already exists, then it simply overwrites it without any warning, so choose your destination file or directory with care.

Syntax of cp/copy command in Linux

As we have seen, the cp tool has 2 modes of operation. We have also seen that the modes of operation depend on the arguments and count of files we give, hence it is important to know the syntax of each mode of operation. The syntax automatically determines the mode of operation of the cp tool.

1. Copying a file to another file (2 file names)

As we have seen, to copy the contents of 1 source file to a destination file we need only 2 files names:

cp <options> <source file> <destination file>

2. Copying one or more files to a directory (More than one argument)

to copy the contents of multiple source files to a destination file, we need to enter the names of the multiple files followed by the destination directory:

cp <options><source file1><source file2> < destination directory>

3. Copying entire directories into a directory (2 directory names)

to copy the contents of a source directory to a destination directory, we need to enter the names of the source and destination directories:

cp -R <source directory> <destination directory>

Options used with cp command in Linux

Before we look at how to use the cp command, let’s get a better picture of the options used with the cp command.

1. –attributes-only

This option copies only the attributes and not the file data.

2. –backup <control>

This option makes a backup of each existing destination file.

3. -b

This option is very similar to the “–backup” option, except this option does not accept any arguments.

4. -copy-contents

This option copies contents of special files when recursive.

5. -f / –force

This option removes and tries again if an existing destination file cannot be opened.

6. -i / –interactive

This option prompts before overwriting.

7. -H

This option follows command-line symbolic links in the source.

8. -l / –link

This option hard links files instead of copying them.

9. -L / –dereference

This option always follows symbolic links in the source.

10. -n

This option does not overwrite an existing file.

11. -p

This option never follows symbolic links in the source. This option is the complete reverse of the option “-L”.

12. –parents

This option uses the full source file name under the directory.

13. –remove-destination

This option deletes or remove each file before trying to open it.

14. –sparse = <when>

This option controls the creation of sparse filters.

15. –strip-trailing-slashes

This option removes any trailing slashes from each source argument.

16. -s

This option makes symbolic links instead of copying.

17. -S

This option overrides the usual backup suffix.

18. -t

This option copies all source arguments into a directory.

19. -T

This option treats the destination as a normal file.

20. -u

This option copies only when the source file is newer than the destination file or when the destination file is missing.

21. -v

This option provides a verbose for a better understanding of what is being done.

22. -x

This option stays on this file system.

23. – Z

This option sets the SELinux security context of the destination file to the default type

24. –help

This option displays all the options, syntax, and use of the cp command to provide a better understanding to the user.

25. –version

This option outputs the version information of the cp tool you are currently using.

PHEW! Those are a lot of options for a command that does a simple task of copying files. If at all you need any help on the syntax or option, you can always use the “–help” option to get a better understanding

cp help

Examples of cp command

Now that you have seen the operational modes syntaxes and the different options used with the cp command, let us do a few practical commands that help us copy files and directories.

Copying a file to a directory

To copy a file to a directory, you can your the syntax:

cp -R <filename> <directory name>

copying a file to a directory

Copying a directory to a directory

To copy a file to a directory, you can your the syntax:

cp <source> <directory name>

copying a directory to a directory

Copying multiple files to a directory

To copy a file to a directory, you can your the syntax:

cp <options><source file1><source file2> < destination directory>

copying multiple files to a directory

Overwriting with prompt

You can overwrite the files with the same syntax as copying files, however, the names of the files you are copying must already exist in the destination file. If you want cp to prompt when you are overwriting, use the option “-i”.

overwriting with prompt

Providing verbose while copying

To provide verbose while copying files or directories use the option “-v”.

providing verbose while copying

Avoid overwriting existing files

If you don’t want to overwrite the existing files, use the “-n” option

avoid overwriting existing files

Summary

As you have seen, the cp command is a really helpful and efficient tool that helps in copying files. You have now learned the uses of the cp command, its operation modes along with the syntax, the various option used with the cp command, and also some examples of how to use the cp command in overwriting, copying multiple files, providing verbose, prompting and many more.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

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