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:

[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 commands, tar, rpm, rkhunter, Rsync, grep, netstat command switches, netstat command’s practical usages, OpenVZ commands, lsof command

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

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