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>

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

Copy multiple files from one location to other.

[[email protected] Linux]# cp script1.sh mail.sh /root/backupdir/
[[email protected] 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
[[email protected] Linux]#

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

[[email protected] Linux]# ls 
file1.txt  file3.txt  file5.txt  mail.sh
file2.txt  file4.txt  indexhtml  script1.sh
[[email protected] Linux]# cp *.txt /root/backupdir/
[[email protected] Linux]# ls /root/backupdir/
file2.txt  file3.txt  file4.txt  file5.txt
[[email protected] 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>

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

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>

[[email protected] Linux]# ls
file1.txt  file3.txt  file5.txt  mail.sh
file2.txt  file4.txt  indexhtml  script1.sh
[[email protected] 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’
[[email protected] Linux]#

4) Ask before overwrite the file.

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

[[email protected] ~]# ls Linux/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  indexhtml  mail.sh  script1.sh
[[email protected] ~]# ls /root/backupdir/Linux/
file1.txt  file3.txt  file4.txt  file5.txt
[[email protected] ~]# 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’
[[email protected] ~]#

5) Make a backup of each existing destination file.

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

[[email protected] 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’
[[email protected] Linux]# ls /root/backupdir/
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[[email protected] 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.

[[email protected] 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~’)
[[email protected] Linux]# ls /root/backupdir/
file1.txt  file1.txt~  file2.txt  file2.txt~  file3.txt  file3.txt~  file4.txt  file4.txt~  file5.txt  file5.txt~
[[email protected] 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.

[[email protected] ~]# 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
[[email protected] ~]# cp -rp /home/jack/Linux/ /var/
[[email protected] ~]# 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
[[email protected] ~]#

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.

[[email protected] 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’
[[email protected] 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”.

[[email protected] ~]# 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
[[email protected] ~]# cp -a /home/jack/Linux/ /opt/office/
[[email protected] ~]# ll /opt/office/
total 0
drwxr-xr-x. 2 jack jack 86 Oct 16 03:04 Linux
[[email protected] ~]# 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
[[email protected] ~]#

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

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

[[email protected] Linux]# ls -lvi file1.txt 
569096 -rw-r--r--. 2 root root 41 Oct 16 12:57 file1.txt
[[email protected] Linux]# cp -l file1.txt /root/openfire/gaurav
[[email protected] Linux]# ls -li /root/openfire/gaurav
569096 -rw-r--r--. 2 root root 41 Oct 16 12:57 /root/openfire/gaurav
[[email protected] 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.

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

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.

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

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.

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

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.

[[email protected] ~]# 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.

[[email protected] 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.
[[email protected] openfire]# cat file1.txt 
Copy the data when source file is newer1
[[email protected] openfire]# cp -n file1.txt file2.txt 
[[email protected] 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.
[[email protected] 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

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of