cp command – How to Copy Files and Directories in Linux

In this tutorial, we’re going to show you how to use the cp command on Linux.

The cp command is one of the basic commands you should know how to use when starting to use Linux.

Basic syntax and a simple example

Let’s start with the basic syntax of the cp command:

cp [OPTIONS] SOURCE DESTINATION

Where [OPTIONS] are the flags you can use for the command, SOURCE is the file you want to copy (the original), and DESTINATION is the new file you want to create as a copy of the original.

Here’s a simple and basic example of copying a .txt file.

If you have a directory (and you’re currently in it) with only the “plan1.txt” file in it, after you run this command:

cp plan1.txt plan2.txt

You’ll have plan1.txt and plan2.txt in your directory, where plan2.txt is a copy of plan1.txt.

Here’s a before and after with the cp command:

before and after

If you want to copy a file from one directory to another directory, you need to include the path of the files. Here’s an example:

cp /directory1/plan1.txt /directory2/plan1.txt

If a “plan2.txt” file already exists, and you’re trying to copy a file to “plan2.txt”, the cp command will override the old file with the new copy.

Now we’ll move on to useful, real-world use cases and examples of the cp command.

How to copy directories

By default, the cp command only copies files. If you try to copy a directory with the cp command you’ll get an error.

To copy directories, use the -r (recursive) option/flag:

cp -r directory1 directory2

This command will copy the directory1 and all of its contents to directory2.

How to copy multiple files at once

If you want to copy multiple files at once, you just need to write them out in the cp command. Here’s an example:

cp plan1.txt plan2.txt plan3.txt directory1

This will copy the files “plan1.txt, plan2.txt, plan3.txt” to the “directory1” directory.

In this case, you can write as many files as you want, but the last argument must be a directory.

How to copy all files in a directory at once

If you don’t want to manually write out each file, and you just need to copy all files that are in a directory, you can use the * wildcard.

Here’s an example:

cp /home/directory1/* /opt/directory2

This will copy all files in “directory1” to “directory2”.

If you want to only the .txt files, you can again use the * wildcard:

cp /home/directory1/*.txt /opt/directory2

This command will copy only all of the .txt files (not the other extensions). You can change out the wildcard here with other file extensions.

How to copy files from a different directory to the current directory

If you’re currently working in a directory (for example, /home/test/), and you want to copy a file from /opt/example/, you can just add a dot at the end of the command. Example:

cp /opt/example/1.txt .

So if your “pwd” is /home/test/, meaning you’re currently in the /home/test/ directory, this command will copy the 1.txt file from the /opt/example/ directory to the /home/test/ directory.

This example is quite useful and it’s something that I often use.

How to preserve file permissions and other file (meta)data

By default, when you copy a file with the cp command, the destination file (the copy) doesn’t keep all the metadata like dates, permissions, etc. To keep them, you just need to use the “-p” (preserve) flag. Example:

cp -p file1.txt file2.txt

This command will make a copy of file1.txt and it will have the same attributes like dates for when you created the file, when you accessed it, when you modified it, the file permissions, the user ID, and the group IP. The fact that it copies over file permissions is the important note here.

How to avoid overwriting existing files

It often happens. You’re trying to copy a file and a file with that name already exists. By default, the cp command will override the old file and you’ll lose it. To avoid this, you can use the “-i” (interactive) flag. Example:

cp -i file1.txt file2.txt

If “file2.txt” already exists, the command will prompt you before running.

Pro-tip: always make a backup of a file before editing it

This is the most used use case of the cp command in my case. Before editing important files (like any config file), it’s smart to create a backup of the original file, and it only takes a couple of seconds.

So, for example, if you plan on editing the “.bash_profile” file, quickly make a copy before editing it:

cp .bash_profile .bash_profile.bak

The cp command also has other flags/options we didn’t talk about. Like “-u” to overwrite the destination file only in the case where the source file is newer than the destination file and the “-v” flag that displays info about the copying process. I often use the “-v” flag when I’m copying multiple files at once.

The cp command shouldn’t be confused with the mv command. The mv command moves (cut -> paste) files, it doesn’t make a copy of them.

Do you have any useful examples or use cases of the cp command? Leave a comment below.

Run this command to get more details, help, and examples of the cp command:

cp --help

Leave a comment

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