The Linux cp command: with practical examples

In this tutorial, I will teach you how to copy files and folders from one place to another, by using the cp command.

Syntax1: cp [options] file filecopy
Syntax2: cp [options] -T filecopy fileĀ 

I always use the first syntax because I find it easier.

1. Copying files with cp:

I create the file f1 and the directories d1/d2. Next, I will copy f1 with the name newf1, in the current directory.

$ touch f1
$ mkdir -p d1/d2
$ cp f1 newf1
$ ls -l | grep ^-
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 02:58 f1
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:01 newf1

You can observe in the displayed output that my copy of the f1 file was created. By default, if file2 exists and you want to make a copy of file1 named file2, the cp will replace your old file2 with the new file2(with your file1 copy). You can use cp -n if you don’t want to overwrite existing files.

$ touch file1 file2
$ ls -l
total 0
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:12 file1
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:12 file2
[wait for two minutes]
$ cp file1 file2
$ ls -l
total 0
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:12 file1
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:14 file2

At first, the two file’s timestamp was 03:12 (because they war created at the same time) but after the cp command, file2’s timestamp has changed to 03:14, meaning that the old file2 was replaced by the new file2.

An empty folder can pe copied from one place to another by using the same cp xyz copy_of_xyz, but it’s easier to create new folders instead to copy them.

2. I recommend using the -ivĀ  options with the cp command, because it makes the command interactive and verbose.

(the -v option displays the changes that have been made and the -i option asks you if you want to overwrite file or not)

$ cp -iv file1 file2
cp: overwrite `file2'? y
`file1' -> `file2'
$ cp -iv file1 file2
cp: overwrite `file2'? n

[y if for yes and n is for no ; if y the cp will overwrite and display the result ; if n the cp will not overwrite and nothing will be displayed]

The cp -iv command is so good, it should be aliased.

3. Copying folders with cp -r or cp -R:

$ ls -l | grep anakin
drwxrwxr-x 2 razvan razvan 4096 2012-05-22 22:33 anakin
$ cp anakin padawan
cp: omitting directory `anakin'

If you use cp without any options on a non empty directory, you will get an error. You need to use the -r (recursive) option for copying the directory with all the files and dirs contained inside.

$ cp -r anakin padawan
drwxrwxr-x 2 razvan razvan 4096 2012-05-22 22:33 anakin
drwxrwxr-x 2 razvan razvan 4096 2012-05-24 03:44 padawan

You can also use the -iv options, like this: cp -iv -r dir dircopy or cp -ivr dir dircopy.

4. Copying files and folders with the same timestamp as the originals with: cp -p for files and cp -pr for dirs

If you have both files and non empty folders , use cp -pr .

$ ls -l
total 4
drwxrwxr-x 3 razvan razvan 4096 2012-05-24 03:53 dir1
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:52 file1
-rw-rw-r-- 1 razvan razvan 0 2012-05-24 03:52 file2
[wait a few minutes]
$ su ana
$ cp -pr * ~
$ ls -l ~
total 4
drwxrwxr-x 3 ana ana 4096 2012-05-24 03:53 dir1
-rw-rw-r-- 1 ana ana 0 2012-05-24 03:52 file1
-rw-rw-r-- 1 ana ana 0 2012-05-24 03:52 file2

You can observe that the timestamps of the copies ar the same as the original ones.

5. Copy links from one place to another without unlinking them: with cp -l

$ ls -l
lrwxrwxrwx 1 razvan razvan 11 2012-05-24 04:12 anakin -> /etc/passwd
$ cp anakin ~/newdir
$ ls -l ~/newdir
-rw-r--r-- 1 razvan razvan 1785 2012-05-24 04:20 anakin

My file has been copied to ~/newdir, but my link was broken. The -l option will not copy my file unlinked.

$ rm ~/newdir/anakin
$ cp -l anakin ~/newdir
$ ls -l ~/newdir
lrwxrwxrwx 2 razvan razvan 11 2012-05-24 04:12 anakin -> /etc/passwd

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

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

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!