The Linux copy command (cp)

Linux file copy FAQ: How do I copy Linux files and directories? (Or, Can you share some cp command examples?)

You use the cp command to copy files and directories on Linux systems. Let's look at some copy examples to see how this works.

Using Linux cp to copy files

At its most basic, here's how you copy a Linux file:

cp Chapter1 Chapter1.bak

This cp command copies the original file named Chapter1 to the new file named Chapter1.bak. After issuing this command both your original file and the new file will be in the current directory.

It's also easy to copy a Linux file to a different directory. Here's how you copy the same file to the /tmp directory:

cp Chapter1 /tmp

and here's how you copy a file one level up in the directory hierarchy:

cp Chapter1 ..

As a final note about copying files, you can also copy multiple files at one time, like this:

cp file1 file2 file3 /tmp

That command copies those three files to the /tmp directory.

How to copy directories with the Linux cp command

You can also copy directories with the Linux cp command, and when you do this, you just need to remember to use the -r option. For example, this command copies the directory named Foo to the /tmp directory:

cp -r Foo /tmp

If you try to copy a directory without using the -r argument you'll get an error like this:

$ cp Foo /tmp
cp: Foo is a directory (not copied).

so it's not too hard to remember to use that option.

Linux cp command examples - Summary

I hope these Linux cp commands examples have been helpful. If you have any questions, or other uses of the copy command, just leave a note in the Comments section below.