14 Examples of cp Command

Copy Command in Linux/Unix

The “cp” command copies one or more files / or directories from source to destination location. It is one of basic command that are used day to day of activities in every operating system(Linux/Unix).

cp command syntax;
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

cp command options:

“-a”      Used for archive files, same as same as -pR
“-b”      Make a backup of each existing destination file, but does not accept an argument
“-f”      Force copying
“-i”      Ask before overwrite
“-l”      Hard link files instead of copying
“-L”      Always follow symbolic links in source
“-n”      Do not overwrite an  existing  file
“-P”      Never follow symbolic links in SOURCE
“-p”      Preserve the specified attributes
“-R”or”-r” Copy directories recursively
“-s”      Make symbolic links instead of copying
“-u”      Copy only when the SOURCE file is newer than the dest. file or when the destination file is missing
“-v”      Verbose
“-x”      Stay on this file system.

In this article we will cover some useful tricks examples using cp command with all above options.

1) Perform cp command without any option.

To copy a files or directories from source to destination location. We can type like this;

#cp <source location> <Destination Location>

[root@client1 Linux]# cp indexhtml /root/backupdir/
[root@client1 Linux]# ll /root/backupdir/
total 0
-rw-r--r--. 1 root root 0 Oct 15 23:51 indexhtml
[root@client1 Linux]#

Copy multiple files from one location to other.

[root@client1 Linux]# cp script1.sh mail.sh /root/backupdir/
[root@client1 Linux]# ll /root/backupdir/
total 0
-rw-r--r--. 1 root root 0 Oct 15 23:51 indexhtml
-rw-r--r--. 1 root root 0 Oct 15 23:52 mail.sh
-rw-r--r--. 1 root root 0 Oct 15 23:52 script1.sh
[root@client1 Linux]#

Copy all *.txt files in current directory to subdirectory(i.e backupdir)

[root@client1 Linux]# ls 
file1.txt  file3.txt  file5.txt  mail.sh
file2.txt  file4.txt  indexhtml  script1.sh
[root@client1 Linux]# cp *.txt /root/backupdir/
[root@client1 Linux]# ls /root/backupdir/
file2.txt  file3.txt  file4.txt  file5.txt
[root@client1 Linux]#

2) Perform cp command to copy a directory.

As seen above example, Similar for directory only we need to add “-r” or “-R” option to copy the directory from one location to other location.

#cp -r <source location> <Destination Location>

[root@client1 ~]# cp -r  Linux/ /root/backupdir/
[root@client1 ~]# ls /root/backupdir/
file1.txt  file3.txt  file5.txt  Linux    script1.sh
file2.txt  file4.txt  indexhtml  mail.sh
[root@client1 ~]#

As above example, Linux Directory is copied to /root/backupdir/  location.

3) Print informative messages.

Using “-v” option you can find out what happen during the copying file.

#cp -v <Source> <Destination>

[root@client1 Linux]# ls
file1.txt  file3.txt  file5.txt  mail.sh
file2.txt  file4.txt  indexhtml  script1.sh
[root@client1 Linux]# cp -v *.txt /root/backupdir/
‘file1.txt’ -> ‘/root/backupdir/file1.txt’
‘file2.txt’ -> ‘/root/backupdir/file2.txt’
‘file3.txt’ -> ‘/root/backupdir/file3.txt’
‘file4.txt’ -> ‘/root/backupdir/file4.txt’
‘file5.txt’ -> ‘/root/backupdir/file5.txt’
[root@client1 Linux]#

4) Ask before overwrite the file.

Using “i” (i.e Interactive mode),It will ask if the destination location have already the file.

[root@client1 ~]# ls Linux/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  indexhtml  mail.sh  script1.sh
[root@client1 ~]# ls /root/backupdir/Linux/
file1.txt  file3.txt  file4.txt  file5.txt
[root@client1 ~]# cp -irv Linux/ /root/backupdir/
cp: overwrite ‘/root/backupdir/Linux/file3.txt’? y
‘Linux/file3.txt’ -> ‘/root/backupdir/Linux/file3.txt’
cp: overwrite ‘/root/backupdir/Linux/file4.txt’? y
‘Linux/file4.txt’ -> ‘/root/backupdir/Linux/file4.txt’
cp: overwrite ‘/root/backupdir/Linux/file5.txt’? y
‘Linux/file5.txt’ -> ‘/root/backupdir/Linux/file5.txt’
‘Linux/indexhtml’ -> ‘/root/backupdir/Linux/indexhtml’
‘Linux/script1.sh’ -> ‘/root/backupdir/Linux/script1.sh’
cp: overwrite ‘/root/backupdir/Linux/file1.txt’? y
‘Linux/file1.txt’ -> ‘/root/backupdir/Linux/file1.txt’
‘Linux/file2.txt’ -> ‘/root/backupdir/Linux/file2.txt’
‘Linux/mail.sh’ -> ‘/root/backupdir/Linux/mail.sh’
[root@client1 ~]#

5) Make a backup of each existing destination file.

Using “-b” option, It will make a backup of each existing destination file.

[root@client1 Linux]# cp -bv *.txt /root/backupdir/
‘file1.txt’ -> ‘/root/backupdir/file1.txt’
‘file2.txt’ -> ‘/root/backupdir/file2.txt’
‘file3.txt’ -> ‘/root/backupdir/file3.txt’
‘file4.txt’ -> ‘/root/backupdir/file4.txt’
‘file5.txt’ -> ‘/root/backupdir/file5.txt’
[root@client1 Linux]# ls /root/backupdir/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[root@client1 Linux]#

Note: “-b” option does not accept an argument of backup.

cp command also provide the “–backup” option, It will make a backup of each existing destination file with accept an argument of backup.

As below example, Using –backup=simple option will create a backup files and that marked by a tilde sign (~) at the end of the file.

[root@client1 Linux]# cp --backup=simple -v *.txt /root/backupdir/
cp: overwrite ‘/root/backupdir/file1.txt’? y
‘file1.txt’ -> ‘/root/backupdir/file1.txt’ (backup: ‘/root/backupdir/file1.txt~’)
cp: overwrite ‘/root/backupdir/file2.txt’? y
‘file2.txt’ -> ‘/root/backupdir/file2.txt’ (backup: ‘/root/backupdir/file2.txt~’)
cp: overwrite ‘/root/backupdir/file3.txt’? y
‘file3.txt’ -> ‘/root/backupdir/file3.txt’ (backup: ‘/root/backupdir/file3.txt~’)
cp: overwrite ‘/root/backupdir/file4.txt’? y
‘file4.txt’ -> ‘/root/backupdir/file4.txt’ (backup: ‘/root/backupdir/file4.txt~’)
cp: overwrite ‘/root/backupdir/file5.txt’? y
‘file5.txt’ -> ‘/root/backupdir/file5.txt’ (backup: ‘/root/backupdir/file5.txt~’)
[root@client1 Linux]# ls /root/backupdir/
file1.txt  file1.txt~  file2.txt  file2.txt~  file3.txt  file3.txt~  file4.txt  file4.txt~  file5.txt  file5.txt~
[root@client1 Linux]#

“–backup” option can be manage using below options;

  • simple, never : Always make simple backups.
  • none, off : Never backups.
  • existing, nil : Numbered if numbered backup exist, simple otherwise.
  • numbered, t : Make numbered backups.

6) Copy a file or directory keeping owners and permissions intact.

Using “-p” option, You can keep the ownership and permissions from the original file or directory using cp command, refer below example.

[root@client1 ~]# ll /home/jack/Linux/
total 0
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file1.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file2.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file3.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file4.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file5.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 indexhtml
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 mail.sh
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 script1.sh
[root@client1 ~]# cp -rp /home/jack/Linux/ /var/
[root@client1 ~]# ll /var/Linux/
total 0
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file1.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file2.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file3.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file4.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file5.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 indexhtml
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 mail.sh
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 script1.sh
[root@client1 ~]#

Note: “cp” command preserve the following attributes of each source file in the copy: modification time,file mode, ACL, access time, file flags,   user ID, and group ID, as allowed by permissions.

7) Force copying using “-f” option.

Using “-f” option used to force the copy of files or directories.

[root@client1 openfire]# cp -fv *.txt ../Linux/
‘file1.txt’ -> ‘../Linux/file1.txt’
‘file2.txt’ -> ‘../Linux/file2.txt’
‘file3.txt’ -> ‘../Linux/file3.txt’
‘file4.txt’ -> ‘../Linux/file4.txt’
‘file5.txt’ -> ‘../Linux/file5.txt’
[root@client1 openfire]#

Note: “-f” is used when destination files cannot be opened.

8) Copy a files using “-a” option.

The “-a” option means “-R” and “-p”.

[root@client1 ~]# ll /home/jack/Linux/
total 0
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file1.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file2.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file3.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file4.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file5.txt
[root@client1 ~]# cp -a /home/jack/Linux/ /opt/office/
[root@client1 ~]# ll /opt/office/
total 0
drwxr-xr-x. 2 jack jack 86 Oct 16 03:04 Linux
[root@client1 ~]# ll /opt/office/Linux/
total 0
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file1.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file2.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file3.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file4.txt
-rw-r--r--. 1 jack jack 0 Oct 16 02:36 file5.txt
[root@client1 ~]#

“cp” command also provide us with “–attributes-only” option, This option only copy a file name with copy the file data, refer below example.

[root@client1 Linux]# cat file1.txt 
cp is a Linux shell command to copy files and directories.
[root@client1 Linux]# ll file1.txt
-rw-r--r--. 1 jack jack 59 Oct 16 03:11 file1.txt
[root@client1 Linux]# cp --attributes-only -v file1.txt /root/
‘file1.txt’ -> ‘/root/file1.txt’
[root@client1 Linux]# cat /root/file1.txt 
[root@client1 Linux]# ll /root/file1.txt 
-rw-r--r--. 1 root root 0 Oct 16 03:12 /root/file1.txt
[root@client1 Linux]#

9) Make Hard link files instead of copying.

Using “-l” option, you can make a shortcut or links to the files instead of copying them.

[root@client1 Linux]# ls -lvi file1.txt 
569096 -rw-r--r--. 2 root root 41 Oct 16 12:57 file1.txt
[root@client1 Linux]# cp -l file1.txt /root/openfire/gaurav
[root@client1 Linux]# ls -li /root/openfire/gaurav
569096 -rw-r--r--. 2 root root 41 Oct 16 12:57 /root/openfire/gaurav
[root@client1 Linux]#

As above example, You can see that a hardlink of “file1.txt” was copied into /root/openfire/gaurav and it marked by the same inode (i.e 569096). But please note, hardlinks cannot be created into directories.

10) Make symbolic links instead of copying.

Using “-s” option you can create a symlinks only can be done in current directory, refer below example.

[root@client1 ~]# cp -s .bash_history /root/gaurav
[root@client1 ~]# ls -l gaurav 
lrwxrwxrwx. 1 root root 13 Oct 16 13:19 gaurav -> .bash_history
[root@client1 ~]#

11) Never follow symbolic links in source.

On above example, we have created symbolic file named “gaurav”. When “cp” command found a file with symbolic links, it will copy the as it is, using “-P” option, refer the example.

[root@client1 ~]# ll gaurav 
lrwxrwxrwx. 1 root root 13 Oct 16 13:19 gaurav -> .bash_history
[root@client1 ~]# cp -P gaurav openfire/gaurav 
[root@client1 ~]# ll openfire/gaurav 
lrwxrwxrwx. 1 root root 13 Oct 16 13:24 openfire/gaurav -> .bash_history
[root@client1 ~]#

12) Always follow symbolic links in source.

Using “-L” option, the copied file is the same file with the source file, let take a example.

[root@client1 ~]# ll  gaurav 
lrwxrwxrwx. 1 root root 13 Oct 16 13:19 gaurav -> .bash_history
[root@client1 ~]# ls -lh .bash_history 
-rw-------. 1 root root 6.3K Oct 13 10:03 .bash_history
[root@client1 ~]# cp -L gaurav Linux/historybak
[root@client1 ~]# ls -lh Linux/historybak 
-rw-------. 1 root root 6.3K Oct 16 13:44 Linux/historybak
[root@client1 ~]#

As above example, With “-L” option the copied file has 6.3k bytes file size while the .bash_history file has same size.

13) Stay on this file system.

It means not to cross file system boundaries. A boundary between file systems is a mount point. If you run “df -a” command, you will see all files and mount points.

[root@client1 ~]# df -a
Filesystem              1K-blocks    Used Available Use% Mounted on
rootfs                          -       -         -    - /
sysfs                           0       0         0    - /sys
proc                            0       0         0    - /proc
devtmpfs                   369400       0    369400   0% /dev
securityfs                      0       0         0    - /sys/kernel/security
tmpfs                      379404       0    379404   0% /dev/shm
devpts                          0       0         0    - /dev/pts
tmpfs                      379404    5060    374344   2% /run
tmpfs                      379404       0    379404   0% /sys/fs/cgroup

So if you issue a recursive copy on your filesystem cp -ax /, it won’t copy /sys directory. because if you run it on /sys  df /sys you will see that the mount point is different.You could add -v option if you want to see exactly what is discarded and what is being copied. As others pointed out, it’s used with recursive and people generally use it for backup.

14) Do not overwrite an  existing  file.

Sometimes we need to copy a file but not to overwrite the previous one if it exists In this case you can use the “-n” option, refer the example.

[root@client1 openfire]# cat file2.txt 
Linux is  a Unix-like and mostly POSIX-compliant computer operating system (OS) assembled under the model of free and open-source software development and distribution.
[root@client1 openfire]# cat file1.txt 
Copy the data when source file is newer1
[root@client1 openfire]# cp -n file1.txt file2.txt 
[root@client1 openfire]# cat file2.txt 
Linux is  a Unix-like and mostly POSIX-compliant computer operating system (OS) assembled under the model of free and open-source software development and distribution.
[root@client1 openfire]#

As above example, the existing file is not overwrite.

We hope that you like the post if you find any difficulties using this article then please do comment your queries, till then connected with us at ittroubleshooter.in for more such valuable article.!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz