Linux cp command examples

The Linux cp command lets you copy one or more files or directories. To be clear, the cp command makes a duplicate copy of your information, whereas the mv command moves your information from one location to another.

Let's take a look at some Linux cp command examples.

Simple file copying

Using a Linux or Unix system, to copy a file named "foo" to a new file named "bar" just type:

cp foo bar

To copy a file named "foo" to a new file named "bar" in the /tmp directory type:

cp foo /tmp/bar

Or, if you don't want to rename the file but just want to copy it to the /tmp directory just type:

cp foo /tmp

After this command you'll have one copy of the file "foo" in the current directory, and another in the /tmp directory, assuming that you didn't get an error message.

Conversely, if the file "foo" is in the /tmp directory, and you want to copy it to the current directory you'd type this:

cp /tmp/foo .

No matter where you are in the filesystem, if you want to copy the same file to your home directory you can use this cp command syntax:

cp /tmp/foo ~

The ~ character is a shortcut character that refers to your home directory (and works with all shell commands, not just the cp command). To copy the same file to a directory named "dir1" in your home directory you could type this:

cp /tmp/foo ~/dir1

More-complicated file copying

To copy a file "foo" to a directory named "dir1" type:

cp foo dir1

To copy several files named "foo1", "foo2", and "foo3" to a directory named "dir1" type:

cp foo1 foo2 foo3 dir1

That's the long way to type it out. This command does this same thing:

cp foo[123] dir1

And if you don't have any other file beginning with the string "foo" you can just take this shortcut:

cp foo* dir1

Copying directories with cp

If you try to copy a directory using the exact same command you'll get an error message ("cp: dir1 is a directory (not copied)"), like this:

cp dir1 dir2
cp: dir1 is a directory (not copied).

To copy a directory you need to use the -r option, like this:

cp -r dir1 dir2

If "dir2" already exists, this command recursively copies the directory "dir1" into the directory named "dir2". If "dir2" did not exist, the system creates a new directory named "dir2" that is a copy of "dir1".

(To create an even closer copy you may want to add the -p option, which attempts to preserve "modification time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions". Usually I only do this when I'm acting as a system administrator.)

Don't clobber existing files

The cp command has several options to keep you from clobbering existing files during copy operations. The -i option prompts you before performing a copy operation that would overwrite an existing file. Assuming that "bar" is a file that already exists, the interaction looks something like this:

/Users/al/yada> cp -i foo bar 
overwrite bar? (y/n [n]) n
not overwritten

The system prompts me with the overwrite bar? (y/n) prompt, and I respond with n.

Instead of using -i you can use -n, which just doesn't allow this to happen at all. Unfortunately it doesn't give you any output, unless you also use the -v option, like this:

/Users/al/yada> cp -nv foo bar
bar not overwritten
foo -> bar

cp aliases

Because of the potential danger of clobbering existing files a lot of people create an alias for the cp command, like this:

alias cp="cp -i"

There's not any harm in doing this, and you can always undo it, so I highly recommend it.