cp command in Linux with examples

One of the most basic commands in Linux is the cp or copy command. The most basic way to use this command is to copy a file or multiple files. The cp command is one of the first commands you should learn as a newcomer to Linux, as copying files and directories is something you’ll do often.

Understanding how the command does this is easier than you think. The cp command simply creates an image of a file on a disk with a different file name. Though the cp command can get get a bit more complex than this, as it’s capable of copying files and directories in various special ways that we’ll get into later. As the cp command is so fundamental, it exists in both Linux and Unix alike.

Like many other essential Linux commands, the cp command has many different options that you can use to copy files and directories. You’ll find some of these options particularly useful, although that doesn’t mean that cp on its own doesn’t already encompass a wide variety of administration scenarios.

In this tutorial, you’ll learn how to use the cp command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the cp command on Linux
cp command in Linux with examples
cp command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software cp
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The cp command copies files and directories from the current working directory, or some other directory if one is specified. The options you can use with cp are mostly just to copy files and directories with various nuances to cover a wide range of specific scenarios.

cp command in Linux Basic Examples

  1. Running the cp command by itself will just copy a file or directory in your present working directory. The present working directory is the directory in which a user is working at a given moment. The present working directory also refers to the current working directory and is interchangeable.
    $ cp file01 file01-copy
    
    Creating a copy of a file by typing the cp command followed by the original file name and the name of the copy
    Creating a copy of a file by typing the cp command followed by the original file name and the name of the copy

    As you can see in the screenshot above, the output will create a new copy of the file01.txt file. This output will always be the same; the same output where cp command quietly creates copies in the background, if you run cp without additional command line options. You can change this result by using one of the many different options with the cp command. We’ll go over that in the next example.

    Note that, as we’ve seen in the example above, typing two file names (or directories) following the cp command will create a copy of that file with a different name. That is only if the second file doesn’t already exist. cp will copy the contents of the first file to the second file instead.

  2. Using the -v option, we can get the cp command to tell us what it is doing in the background so we can see it in action. In the example below, we’ll show you how to use the syntax in your Linux terminal to get this output.
    $ cp -v file02 file02-copy
    
    Using the -v option to view what the cp command is doing in the background
    Using the -v option to view what the cp command is doing in the background

    As you can see in the screenshot provided above, the output from cp -v creates a copy of a file02 and tells us what it did in the background.

  3. By default, the cp command will create a copy of a file or directory without asking for confirmation if you do not use additional command line options. But if you use the -i option, the cp command will prompt you to confirm that you want to overwrite the specified file. You should use this to avoid accidentally erasing files, as the cp command cannot be undone.
    $ cp -i file03 file03-copy
    
    Using the -i option to copy files interactively
    Using the -i option to copy files interactively

    This command will ask if you wish to proceed with overwriting the file in question. Typing “y” instructs the command to continue the procedure while typing “n” instructs it to abort.

  4. In the examples above, we’ve shown how the cp command can be used to create copies of a single file or directory. But as you’ll recall from the introduction of this tutorial, the cp command can create copies of multiple files and directories. We can achieve this by typing the filenames before the destination after the cp command. To clarify, the destination must be a directory when copying multiple files. An example of this is shown below.
    $ cp file05 file06 Documents
    
    Using cp to copy multiple files into a directory
    Using cp to copy multiple files into a directory

    As you can see in the screenshot above, copying multiple files can be achieved by typing each file name after the command.

  5. Up until now, we have only shown how to create copies of files. But like we mentioned throughout this tutorial, the cp command can be used to create copies of directories as well. In the following example, we’ll show you how to copy a directory using the -r option. Using this command to create copies of directories is just as simple as creating file copies.
    $ cp -R Downloads Downloads-copy
    

    As you can see in the screenshot below, creating a copy of a directory can be achieved by using the -r option following the cp command.

    Using the -r option to create copies of directories
    Using the -r option to create copies of directories

Advanced Usage

The cp command is quite straightforward. But as you’ve seen in the examples part of this tutorial, there are many other command line options we can use with it. Many of these options are often missed, and even experienced system administrators may be unaware of them. However, they can come in handy in many different situations. In this section, we’ll show you a few of the lesser known options of the cp command that we think are useful.

cp command in Linux Advanced Examples

  1. Another great way to use the cp command is with the -rt option. This will copy all the contents of a directory. This is useful because it’s a very efficient way to copy many files at once.
    $ cp -rt Desktop Templates
    

    Using the -rt option to copy the contents of the Desktop directory
    Using the -rt option to copy the contents of the Desktop directory

Closing Thoughts

In this tutorial, we learned all about the cp command on Linux which is useful for copying directories and files for personal use and system administration.