≑ Menu

How to Copy Files in Linux and Unix? 10 cp Command Examples

cp is one of the basic command in Unix. You already know that it is used to copy one or more files or directories from source to destination.

While this tutorial is for beginners, it is also helpful for everybody to quickly review various cp command options using some practical examples.

Even if you are using cp command all the times, probably one or more examples explained below might be new to you.

The general form of copy command:

cp [option] source destination

1. Copy a file or directory from source to destination

To copy a file, you need to pass source and destination to the copy command. The following example copies the file from project/readme.txt to projectbackup/readme-new.txt

$ cp project/readme.txt projectbackup/readme-new.txt

$ cd projectbackup/

$ ls
readme-new.txt

If you want to copy a file from one folder to another with the same name, just the destination directory name is good enough as shown below.

$ cp project/readme.txt projectbackup/

$ cd projectbackup/

$ ls
readme.txt

A directory (and all its content) can be copied from source to destination with the recursive option -r as shown below:

$ ls project
src/  bin/  doc/  lib/  test/  readme.txt  LICENSE

$ cp -r project/ backup/

$ ls backup
src/  bin/  doc/  lib/  test/  readme.txt  LICENSE

2. Copy multiple files or directories

You can copy more than one file from source to destination as shown below:

$ cd src/
$ cp global.c main.c parse.c /home/thegeekstuff/projectbackup/src/

If the source files has a common pattern, use wild-cards as shown below. In this example, all c extension files gets copied to /home/thegeekstuff/projectbackup/src/ directory.

$ cp *.c /home/thegeekstuff/projectbackup/src/

Copy multiple directories as shown below.

$ cd project/

$ cp -r src/ bin/ /home/thegeekstuff/projectbackup/

3. Backup before copying into a destination

In case if the destination file is already present with the same name, then cp allows you to backup the destination file before overwriting it.

In this example, the readme.txt exists in both project/ and projectbackup/ directory, and while copying it from project/ to projectbackup/, the existing readme.txt is backed up as shown below:

$ cd projectbackup

$ ls -l readme.txt
-rw-r--r-- 1 bala geek 1038 Jan  8 13:15 readme.txt

$ cd ../project 

$ ls -l readme.txt
-rw-r--r-- 1 bala geek 1020 Jan  8 12:25 readme.txt

$ cp --backup readme.txt  /home/thegeekstuff/projectbackup/

The existing file has been moved to readme.txt~ and the new file copied as readme.txt as shown below.

$ cd /home/thegeekstuff/projectbackup/
$ ls -l
-rw-r--r-- 1 bala geek 1020 Jan  8 13:36 readme.txt
-rw-r--r-- 1 bala geek 1038 Jan  8 13:15 readme.txt~

Talking about backup, it is important for you to understand how rsync command works to backup files effectively.

4. Preserve the links while copying

When you execute the cp command, if the source is a link file, then the actual file gets copied and not the link file. In case if you only want to copy the link as it is, specify option -d as shown below:

The following shows that without option -d, it will copy the file (and not the link):

$ cd project/bin

$ ls -l startup.sh
lrwxrwxrwx 1 root root 18 Jan  8 13:59 startup.sh -> ../test/startup.sh

$ cp startup.sh /home/thegeekstuff/projectbackup/bin/

$ cd /home/thegeekstuff/projectbackup/bin/

$ ls -l
-rw-r--r--  1 root root       102 Jan  8 14:02 startup.sh

To preserve the link while copying, do the following:

$ cd project/bin

$ cp -d startup.sh /home/thegeekstuff/projectbackup/bin/

$ ls -l startup.sh
lrwxrwxrwx 1 root root 18 Jan  8 14:10 startup.sh -> ../test/startup.sh

5. Don’t overwrite an existing file

If you want to copy only when the destination file doesn’t exist, use option -n as shown below. This won’t overwrite the existing file, and cp command will return with success exit code as shown below:

$ cd projectbackup

$ ls -l readme.txt
-rw-r--r-- 1 bala geek 1038 Jan  8 13:15 readme.txt

$ cd ../project 

$ ls -l readme.txt
-rw-r--r-- 1 bala geek 1020 Jan  8 12:25 readme.txt

$ cp -n readme.txt /home/thegeekstuff/projectbackup/bin/

$ echo $?
0

As you see below, the destination file didn’t get overwritten.

$ cd projectbackup

$ ls -l readme.txt
-rw-r--r-- 1 bala geek 1038 Jan  8 13:15 readme.txt

6. Confirm before overwriting (interactive mode)

When you use -i option, it will ask for confirmation before overwriting a file as shown below.

$ cp -i readme.txt /home/thegeekstuff/projectbackup/
cp: overwrite `/home/thegeekstuff/projectbackup/readme.txt'? y

7. Create hard link to a file (instead of copying)

When you execute cp command, it is possible to create a hard link of the file (instead of copying the file). The following example creates the hard link for sample.txt file into directory test/,

$ ls -li sample.txt
10883362 -rw-r--r-- 2 bala geek   1038 Jan  9 18:40 sample.txt

$ cp -l sample.txt test/ 

$ ls -li test/sample.txt
10883362 -rw-r--r-- 2 bala geek   1038 Jan  9 18:40 test/sample.txt

As seen above, the test/sample.txt is a hard linked file to sample.txt file and the inode of both files are the same.

8. Create Soft link to a file or directory (instead of copying)

When you execute cp command, it is possible to create a soft link to a file or directory. In the following example, a symbolic link gets created for libFS.so.6.0.0 as libFS.so,

# cd /usr/lib/

# ls -l libFS.so.6.0.0
-rw-r--r-- 1 root root 42808 Nov 19  2010 libFS.so.6.0.0

# cp -s libFS.so.6.0.0 libFS.so

# ls -l libFS.so
lrwxrwxrwx 1 root root 14 Jan  9 20:18 libFS.so -> libFS.so.6.0.0

9. Preserve attributes of file or directory while copying

Using -p option, you can preserve the properties of a file or directory as shown below:

$ ls -l sample.txt
-rw-r--r-- 2 bala geek   1038 Jan  9 18:40 sample.txt

$ cp -p sample.txt test/ 

$ ls -l test/sample.txt
-rw-r--r-- 2 bala geek   1038 Jan  9 18:40 test/sample.txt

It is also possible to preserve only the required properties like mode, ownership, timestamps, etc.,

The following example preserves the mode of a file while copying it:

$ cp --preserve=mode sample.txt test/

10. Copy only when source file is newer than the destination or missing

Copy doesn’t take much time for a small file, but it may take considerable amount of time when a huge file is copied. So, while copying a big file, you may want to make sure you do it only when the source file is newer than the destination file, or when the destination file is missing using the option -u as shown below.

In this example, the two files LICENSE and readme.txt will be copied from project/ to projectbackup/. However, the LICENSE file already exists in projectbackup/ directory and that is newer than the one in the project/ directory.

$ cd project/

$ ls -l LICENSE readme.txt 
-rw-r--r-- 1 bala geek 108 Jan  8 13:14 LICENSE
-rw-r--r-- 1 bala geek 32 Jan  8 13:16 readme.txt

$ cd /home/thegeekstuff/projectbackup/

$ ls -l LICENSE readme.txt 
ls: cannot access readme.txt: No such file or directory
-rw-r--r-- 1 root root 112 Jan  9 20:31 LICENSE

So, in this example, there is no need to copy LICENSE file again to projectbackup/ directory. This is automatically taken care by cp command, if you use -u option as shown below. In the below example, only readme.txt file got copied as indicated by the time-stamp on the file.

$ cp -u -v LICENSE readme.txt  /home/thegeekstuff/projectbackup/
`readme.txt' -> `/home/thegeekstuff/projectbackup/readme.txt'

$ cd /home/thegeekstuff/projectbackup/

$ ls -l LICENSE readme.txt 
-rw-r--r-- 1 bala geek  112 Jan  9 20:31 LICENSE
-rw-r--r-- 1 bala geek   32 Jan  9 22:17 readme.txt
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Ehan Chang March 6, 2013, 8:39 am

    very clear, and instructive to those laisons.

  • Jalal Hajigholamali March 6, 2013, 9:58 am

    Hi,
    very simple and nice
    thanks

  • Oleksandr March 6, 2013, 10:33 am

    2. Copy multiple files or directories into different location
    i guess it should be ” Copy multiple files or directories from different location” πŸ˜‰

    And thank you for your articles – always clear and comprehensive enough !

  • rodica gall March 6, 2013, 10:48 am

    I think this link is a quick efficient way to recall some commands you once knew and refresh them

  • Jason March 6, 2013, 10:54 am

    Very nice — I never knew that cp could create links. That is very handy to know!

  • Dani March 6, 2013, 10:55 am

    Excellent way to explain

  • Chris F.A. Johnson March 6, 2013, 12:48 pm

    Some of the examples do not work in Linux *and* Unix.

    The examples using the GNU long options (e.g. –preserve=mode) do not work in standard Unix versions of cp.

    Nor do the -r, -s, -u, -v or -l options.

  • Roger March 6, 2013, 1:55 pm

    You might want to check Item 10 where you may have made a typing error. I would think, as confirmed by the man page, that the copy is executed when the SOURCE file is newer than the DESTINATION. Otherwise, I found it a good refresher.

  • Doug March 6, 2013, 2:50 pm

    Hey!, New to the command prompt world. Wow!
    Where can i get the newbs newbie newb tutorial? Because for me, at this point, I find it hard to know where things go and what they do in GUI. I know, a horrible thing to admit but thats where i am and trying to soak it up as fast as i can! btw i have been able to put linux mint on an older craptop and it now is nice and well, minty fresh!
    tnx.

  • iAmAbEgInNer March 6, 2013, 3:31 pm

    Thanks for the article that will definitely come in handy.

  • Chris F.A. Johnson March 6, 2013, 9:14 pm

    Doug, there is a page of links to shell resources on my site: here.

    The POSIX spec for the cp command is here

  • jalil March 6, 2013, 10:46 pm

    Hi
    Excellent and …. thanks
    zare

  • Ramesh Natarajan March 6, 2013, 11:51 pm

    @Oleksandr, @Roger,

    Thanks for pointing out the issue. It is fixed now.

  • Peter March 7, 2013, 1:19 am

    Great examples as usual πŸ™‚ Thanks!

    At item 1: You are not copying the directory but only its content.

    I know that different distributions have different behaviour so this might not always be true. When copying a directory and its content you shouldn’t use the slash (/). When using the slash, it’s the content of the directory you are copying.

    Copy the content of a directory:
    $ cp -r project/ backup/
    $ ls backup/
    src/ bin/ doc/ lib/ test/ readme.txt LICENSE

    Copy the directory and its content:
    $ cp -r project backup/
    $ ls backup/
    project/
    $ tree backup
    backup
    └── project
    β”œβ”€β”€ LICENSE
    β”œβ”€β”€ bin
    β”œβ”€β”€ doc
    β”œβ”€β”€ readme.txt
    β”œβ”€β”€ src
    └── test

  • kumar March 7, 2013, 3:13 am

    Great!! cp command’s prowess is explained in a well versed instructions, worthy shot for a permanent bookmark.

  • Houman Hakami March 7, 2013, 4:11 am

    Very useful .. thanks

  • Doug March 7, 2013, 4:45 am

    Chris,
    Thanks, I will check them both out.

  • devi March 7, 2013, 5:40 am

    Good examples…will be a quick revision

  • Mohammed AL Jeaid March 7, 2013, 10:14 am

    Very nice

    Tnx.

  • DMW March 8, 2013, 12:43 pm

    Nice tidbit. Reminds that it good to read the man pages for such commonly used commands. You’ll be surprised with helpful options.

    Some others commands most users (and admins) regularly use but only take advantage of one, two, three, … options are:
    “ps” (Linux supports both the Berkley (BSD) and AT&T System V options nicely)
    “ls” ( -h, –hide/–ignore, -m, -Q, -S, -X, -1)
    “grep” (-r, -o -A, -B; -G (BRE), -E (ERE), -P (PerlRegex), -m, -x …)
    “sed” ( love the -i option, no more redirections and moves. )
    “find” ( -iname, -iregex, -delete, -print0, and -fprint0 -ok)
    and on and on

  • bhavya March 10, 2013, 2:20 am

    good examples………..and i want c pointers.

  • Γ–mer Faruk March 14, 2013, 12:44 am

    Thanks for your post. But when i copy a file with -p option as a normal user, it doesn’t preserve the owner attributes, while it preserve them copying it as root . Any idea? Thanks in advance…

  • Chris F.A. Johnson March 14, 2013, 10:39 pm

    Γ–mer, you are making the copy so you are its owner.

  • Γ–mer Faruk March 15, 2013, 1:56 am

    Chris, it says in man that: “-p same as –preserve=mode,ownership,timestamps”, So i understand that when i use -p option for a file that is owned to a different user, it should preserve the owner attibutes. Right?

  • Chris F.A. Johnson March 15, 2013, 2:32 am

    When you copy a file *you* are performing the operation.

    *You* read the file that is to be copied.

    *You* save the file to the new location, therefore *you* own the file.

    *You* do not have the authority to change the ownership of that file, only root can do that.

    Therefore, the ownership of the original file cannot be preserved.

  • David March 15, 2013, 10:58 am

    Hey, thanks for the tutorial. I use copy alot. However, i usually, i want to appy the copy command while piping it with other commands. For instance, i want to search for only text files and then cp them to a certain folder like

    ls /home/david/Documents/ | grep “txt” | cp /home/david/text_files

    How do i do such an operation? Thanks alot…

  • Chris F.A. Johnson March 15, 2013, 12:31 pm

    David, cp does not read its stdin, and ls and grep are unnecessary:

    cp /home/david/Documents/*txt* /home/david/text_files

  • Jason March 15, 2013, 1:31 pm

    I actually used this site and another reference to use the “find” command to do what you want.

    Example:
    cd /dir/with/text/files
    find . -name “*txt*” -exec cp {} /home/david/txt_files \;

    That should do the trick for you.

  • Jason March 15, 2013, 1:32 pm

    Note: The above will work recursively.

  • Pranav March 21, 2013, 10:08 pm

    Nice article, very useful, thanks a lot

  • Bilal Ali March 22, 2013, 3:17 pm

    Is there any option to show the progress and copied files on console.

  • David March 23, 2013, 4:58 am

    Thanks Jason for the reply.

    Bilal ALi, you can use rsync with the “show progress” option like
    rsync -P

  • Charandeep April 15, 2013, 3:29 am

    Hi

    Is there any command with we can check how much it has been copied?
    i.e id we have to copy a folder of 1gb from one location to another, is there any command that shows the percent of the it has been copied.

    Thanks,
    Charandeep

  • pankaj July 1, 2013, 4:11 am

    Hi ,
    when we create a soft link through ln -s command we can create the across the directory also, but in the same case when we try to create the soft link through cp -s across the directory we are getting error …can you please explain me…here through cp command is totally against the ln command

  • Chris F.A. Johnson July 2, 2013, 2:06 am

    Charandeep, look at the –progress option to rsync.

  • Anonymous September 3, 2013, 4:41 am

    Find multiple files of different kinds e.g mp4, txt, avi, c, cpp etc and copy them in one place .

    for i in `find . -name file1.txt -o -name file2.avi -o -name -o -name file3.c -o -name file4.cpp` ; do cp $i ; done

  • Chris F.A. Johnson September 5, 2013, 9:54 pm

    Anonymous, that’s a good example of how *not* to deal with files (have you been reading the Advanced Bash Scripting Guide? It has such awful examples).

    That will fail if any files contain whitespace or any other characters special to the shell.

  • ANUJ November 24, 2014, 11:46 pm

    If i have 100 files to copy from one directory to other excluding one particular.
    can we exclude that file in cp command .????

  • Govind March 17, 2017, 11:51 am

    How can I copy multiple files in to multiple directories in the same home directory ? with a single command.