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.

Syntax:

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

Example:

[[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

Post navigation

Arunlal Ashok

Operations Engineer at Endurance International Group. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com
We like to travel. Our travelogues are published at Trip Mentor

Leave a Reply

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