Linux Commands

Linux cp Command

“cp” is a built-in tool for all the UNIX-based platforms that are used to make copies of the file(s) and directory(s). As the name of the tool suggests, “cp” is an abbreviation of “copy”.

There are tons of copying and pasting options ongoing in the system, right? Almost all of them are performed with the help of “cp”. It’s a very simplistic tool with simplistic usage. However, there are a couple of interesting caveats that you need to know.

Without further ado, let’s check out the usage of “cp” on Linux. I’ll be demonstrating the usage on Manjaro Linux – an awesome Linux distro based on Arch Linux. Learn how to install Manjaro Linux.

For any Linux tool, I’d like to start my guide with the following one.

which cp

“which” shows the full path of the executables that would be in effect if the command was to run. In this case, whenever “cp” is run, it’ll launch from “/usr/bin” directory.

cp usage

“cp” uses the following command structure for all of its actions.

cp <source> <destination>

For example, let’s copy the demo zip files to “DestinationFolder” directory.

cp DestinationFolder/

To verify the result, check out the target directory.

cd DestinationFolder/

“cp” can also create a duplicate of the same file. However, the destination name must be different than the original one. Moreover, there must not be any other file with similar name. Otherwise, “cp” will attempt to overwrite the existing file.


Copying directory

“cp” can also copy directories. However, similar rules of copying files apply. The destination must have a unique name. Otherwise, the data will be overwritten.

cp -r DestinationFolder/ DestinationFolder_copy/

The “-r” flag ensures that if “cp” faces any directory, it will also be copied. Otherwise, “cp” won’t accept directory copying.

Again, in such a situation, there are a couple other rules apply. In the above example, the destination directory “DestinationFolder_copy” didn’t exist, so “cp” created it. However, if the source features 2 or more directories at the same time, the destination must exist. Otherwise, the copy won’t succeed.

Check out the result.

tree DestinationFolder_copy2/

Copying multiple files

Using “cp” you can also copy a number of files at once. However, the destination must be a directory.

cp *.zip DestinationFolder

Verbose mode

This is quite helpful if you’re working with a large number of files or the files are very big in size.

cp --verbose file.txt DestinationFolder/

Now, I’ll be copying a number of files in the verbose mode. This feature can also be stacked with other “cp” flags.

cp --verbose * DestinationFolder/

Interactive copying

If you’re not sure whether there’s any duplicate file or file with the same name, this option is exceptionally useful. Every time “cp” faces a conflict, it will ask for your prompt. It will overwrite the file only if you allow. Otherwise, the file will be skipped.

For example, “DestinationFolder” already holds all the demo files. Now, let’s try copying them again using interactive mode.

cp --verbose -i *.zip DestinationFolder

As shown in the example, there are 2 answers: “y” for Yes (commence overwrite) and “n” for No (skip the file).

Preserving file attributes

Every single file in the Linux system comes up with a bunch of additional information, for example, file permissions, last time the file was modified and accessed, and others. Most of the times, it really doesn’t matter. However, in some sensitive scenarios, this might matter a lot.

Whenever “cp” is copying a file, it only copies the data, not these “attributes”. Let’s have a live demo.

At first, let’s check out the file attribute of the “” file.

ls -l

Now, copy it to the “DestinationFolder” and check its attributes again.

cp --verbose DestinationFolder/

Check the attributes.

ls -l DestinationFolder/

It’s a normal file created, that’s why most of the attributes remain the same. The only noticeable change is the time of the file(s) of creation. In the case of other system-critical files, different attributes play a huge role. We’ll be seeing the demo as well.

To keep the attributes same, use the “-p” flag.

cp -p --verbose DestinationFolder/

Now, it’s time to see the demo with a system file. Does anyone remember Vim? It’s one of the finest text editors that everyone should learn. Despite being age-old, it can offer pretty much every “modern” feature of a text editor, thanks to the awesome vimrc. Learn more about vimrc.

Let’s check out the system vimrc. Its original attributes are as follows.

ls -l /etc/vimrc

Copy it to “DestinationFolder” and see the changes in attributes.

cp --verbose /etc/vimrc ~/Desktop/DestinationFolder/

Almost everything changed, right? Now, use the “-p” flag to preserve every attribute. This time, we need “sudo” access.

sudo cp --verbose -p /etc/vimrc ~/Desktop/DestinationFolder/

ls -l /etc/vimrc ~/Desktop/DestinationFolder/vimrc

Voila! Everything is the same now!

“cp” backup

This is a really handy feature. If you’re going to copy files with a similar name, the default behavior of “cp” is to overwrite the existing one. However, with this option, “cp” will make a backup copy of the conflicting file with a different name and complete the process.

For example, let’s copy the into “DestinationFolder” with backup enabled.

cp -b DestinationFolder/

I allowed “cp” to overwrite the existing file but instead, it made a backup of the already existing file with ~ at the end.

Force copy

In some situations, “cp” might have a problem writing the file to the destination because of the permission issue. In such scenario, “-f” flag should be used. This flag forces “cp” to delete the destination file first and copy the content from the source.

Be careful, though; if you’re performing this action on any critical file like important system configuration, it may cause a big issue. Use it with caution.

cp -f <source> <destination>

Final thoughts

There are numerous ways of using “cp”. These are not the only usage of “cp”. If you’re interested in further in-depth knowledge on, feel free to check out the man and info pages!

man cp

info cp


About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.