How/Command to create a copy of a file/directory – The ‘cp’ command usages and switches with example under Linux

In some situations, it is necessary to create a copy of particular file or directory before going to make any changes on it. In Linux we can use the command “cp” to create a copy of Files/Directories.


Note : Use -r switch to copy a directory.


[[email protected] ~]# touch copytest
[[email protected] ~]# cp copytest /root/Test/
[[email protected] ~]# ll /root/Test/
total 8
drwxr-xr-x. 2 root root 4096 Mar 14 21:20 arun1
-rw-r--r--. 1 root root    0 Mar 14 23:59 copytest
-rw-r--r--. 1 root root    0 Feb  1 05:01 crybit
drwxr-xr-x. 2 root root 4096 Mar 14 21:29 Dir
-rw-r--r--. 1 root root    0 Mar 14 21:26 file2

Switches of the cp command:

1, -f, –force
do not prompt before overwriting

2, -i, –interactive
prompt before overwrite

3, -n, –no-clobber
do not overwrite an existing file

4, –preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

5, -p
same as –preserve=mode,ownership,timestamps

6, -a, –archive
same as -dR –preserve=all

7, -d
same as –no-dereference –preserve=links

8, -l, –link
link files instead of copying

9, -s, –symbolic-link
make symbolic links instead of copying

10, -R, -r, –recursive
copy directories recursively

11, -u, –update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing

12, -v, –verbose
explain what is being done

13, -x, –one-file-system
stay on this file system

14, -Z, –context=CONTEXT
set security context of copy to CONTEXT

15, –help
display this help and exit

16, –version
output version information and exit

That’s it 🙂


Related posts:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab, ftp commandstar, rpm, rkhunter, Rsync, grep, netstat command switches, netstat command’s practical usages, OpenVZ commands, lsof command

Arunlal Ashok

Linux Server Administrator. I'm managing Linux servers since 2012. I started this blog to share and discuss my ideas. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

Leave a Reply

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