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

By -

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.

Syntax:

cp [OPTION]... SOURCE DEST
Note : Use -r switch to copy a directory.

Example:

[root@localhost ~]# touch copytest
[root@localhost ~]# cp copytest /root/Test/
---
[root@localhost ~]# 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

Myself Arunlal, working as an administrator for both Linux and Windows servers. I am interested in Blogging, learning new things and technologies etc. I'm posting some interesting technical stuffs from my working experience. Let me know your suggestions as comment.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current day month ye@r *