{"id":2676,"date":"2023-03-25T19:54:39","date_gmt":"2023-03-25T14:24:39","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2676"},"modified":"2023-03-25T19:58:53","modified_gmt":"2023-03-25T14:28:53","slug":"copy-command-cp-in-linux-unix","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/copy-command-cp-in-linux-unix\/","title":{"rendered":"Copy Command cp in Linux\/Unix"},"content":{"rendered":"\n
Copy command Copy command To copy Example<\/strong>: To replace the contents of If you use a directory name at the place of \ud83d\udcdd Note<\/strong>: To refer to the current directory, use To get a verbose output from the To get interactive output, use \ud83d\udcdd Note:<\/strong> You can use both \ud83d\ude42 Note:<\/strong> Both The For example, when I copy a symlink file To copy the actual target file, you need to use the flag You can also copy one directory into another.<\/p>\n\n\n\n For example, The following commands create a Please note that I am using the flag If you don’t want to create the directory Example:<\/p>\n\n\n\n \ud83d\udcdd Note:<\/strong> The command Example 2: That was a basic introduction to the Here, I will talk about how you can use copy command cp to copy any files or directory in Linux or Unix based distros.<\/p>\n","protected":false},"author":2,"featured_media":2690,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[17],"tags":[18,35],"class_list":["post-2676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line-tools","tag-command-line-tools","tag-core-linux-utilities"],"yoast_head":"\ncp<\/code> in Linux is used to copy the contents of a file or directory into another<\/strong> file or directory. Here, I will talk about how you can use copy command cp to copy any files or directory in Linux or Unix-based distros.<\/p>\n\n\n\n
cp<\/code> is one of the most important commands in the shell scripting<\/a>.<\/p>\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
\n
Basic syntax in copy command cp in Linux<\/h2>\n\n\n\n
file1<\/code>‘s content into
file2<\/code>, execute the following command. If
file2<\/code> exists, its contents are replaced.<\/p>\n\n\n\n
[ajay@legion ~]$ cp file1 file2<\/code><\/pre>\n\n\n\n
replace.txt<\/code> with that of
example.txt<\/code>, run the following command:<\/p>\n\n\n\n
[ajay@legion ~]$ cp ~\/example.txt ~\/Downloads\/replace.txt<\/code><\/pre>\n\n\n\n
file2<\/code>,
file1<\/code> will be copied to that directory. For example, the following command copies the file
~\/example.txt<\/code> to the directory
~\/Downloads<\/code><\/p>\n\n\n\n
[ajay@legion ~]$ cp ~\/example.txt ~\/Downloads<\/code><\/pre>\n\n\n\n
.<\/code> in bash. This applies to all commands, not just the
cp<\/code> command. For example, to copy
~\/example.txt<\/code> into the current directory viz.
~<\/code>, execute the following:<\/p>\n\n\n\n
[ajay@legion ~]$ pwd\n\/home\/ajay\n\n[ajay@legion ~]$ cp ~\/Downloads\/10th.pdf .<\/code><\/pre>\n\n\n\n
Options in the cp command<\/h2>\n\n\n\n
How to get verbose output in the cp command<\/h3>\n\n\n\n
cp<\/code>, use
-v<\/code> or
--verbose<\/code> – it shows what it is doing. Example:<\/p>\n\n\n\n
[ajay@legion ~]$ cp -v ~\/Downloads\/10th.pdf .\n'\/home\/ajay\/Downloads\/10th.pdf' -> '.\/10th.pdf'<\/code><\/pre>\n\n\n\n
The interactive mode in the cp command<\/h3>\n\n\n\n
-i<\/code> or
--interactive<\/code> flag: if you write ‘y’ it copies, and for anything else, it does not. Example:<\/p>\n\n\n\n
[ajay@legion ~]$ cp -v ~\/Downloads\/10th.pdf .\n'\/home\/ajay\/Downloads\/10th.pdf' -> '.\/10th.pdf'\n\n[ajay@legion ~]$ cp -i ~\/Downloads\/10th.pdf .\ncp: overwrite '.\/10th.pdf'? y<\/code><\/pre>\n\n\n\n
-v<\/code> and
-i<\/code> together. For the above example:<\/p>\n\n\n\n
[ajay@legion ~]$ cp -vi ~\/Downloads\/10th.pdf .\n'\/home\/ajay\/Downloads\/10th.pdf' -> '.\/10th.pdf'\n\n[ajay@legion ~]$ cp -vi ~\/Downloads\/10th.pdf .\ncp: overwrite '.\/10th.pdf'? y\n'\/home\/ajay\/Downloads\/10th.pdf' -> '.\/10th.pdf'<\/code><\/pre>\n\n\n\n
-v<\/code> and
-i<\/code> options are available in the commands
mv<\/code> and
rm<\/code> as well.<\/p>\n\n\n\n
How to copy symlinks in Linux using the cp command<\/h3>\n\n\n\n
cp<\/code> command copies the symlinks like any other file. But it does not create copies of the actual target file.<\/p>\n\n\n\n
~\/.Xdefaults<\/code>, only a symlink is created at another place.<\/p>\n\n\n\n
[ajay@legion ~]$ ls -al ~\/.Xdefaults\nlrwxrwxrwx 1 ajay ajay 22 Mar 4 2021 \/home\/ajay\/.Xdefaults -> \/home\/ajay\/.Xresources<\/code><\/pre>\n\n\n\n
[ajay@legion ~]$ cp ~\/.Xdefaults \/tmp\n\n[ajay@legion ~]$ ls -al \/tmp\/.Xdefaults \nlrwxrwxrwx 1 ajay ajay 22 Mar 25 17:47 \/tmp\/.Xdefaults -> \/home\/ajay\/.Xresources<\/code><\/pre>\n\n\n\n
-L<\/code> or
--dereference<\/code>. For,
~\/.Xdefaults<\/code>, the actual target file is
~\/.Xresources<\/code>.<\/p>\n\n\n\n
[ajay@legion ~]$ cp -L ~\/.Xdefaults \/tmp\/file2.txt<\/code><\/pre>\n\n\n\n
How to copy a directory in Linux\/Unix using the cp command<\/h2>\n\n\n\n
form<\/code> directory into
\/tmp\/to<\/code> and copies all content of
\/tmp\/from<\/code>.<\/p>\n\n\n\n
[ajay@legion ~]$ cp -r \/tmp\/from\/ \/tmp\/to\/<\/code><\/pre>\n\n\n\n
-r<\/code> or –recursive in the above command. That is to tell the
cp<\/code> command to copy all the contents of the
from<\/code> directory recursively. All directories, files, and everything else inside the directory
form<\/code> will be copied. This
-r<\/code> flag is available with many basic linux commands such as
mv<\/code>,
rm<\/code>.<\/p>\n\n\n\n
from<\/code> inside the target directory
\/tmp\/to<\/code>, use
*<\/code> just after the
\/<\/code>. It is a special glob in bash. And don’t use quotes around it.<\/p>\n\n\n\n
[ajay@legion ~]$ du --all \/tmp\/from \/tmp\/to\n4 \/tmp\/from\/dir1\/file.txt\n4 \/tmp\/from\/dir1\n4 \/tmp\/from\n0 \/tmp\/to\n\n[ajay@legion ~]$ cp -r \/tmp\/from\/* \/tmp\/to\/\n\n[ajay@legion ~]$ du --all \/tmp\/from \/tmp\/to\n4 \/tmp\/from\/dir1\/file.txt\n4 \/tmp\/from\/dir1\n4 \/tmp\/from\n4 \/tmp\/to\/dir1\/file.txt\n4 \/tmp\/to\/dir1\n4 \/tmp\/to<\/code><\/pre>\n\n\n\n
du<\/code> in the above example is used to list the contents of one or more directories.<\/p>\n\n\n\n
cp<\/code> copies all contents of the given
...default<\/code> folder into the new
...default<\/code> folder without creating
<oldrandomnumber>.default<\/code> in the new directory.<\/p>\n\n\n\n
[ajay@legion ~]$ cp -R \/to\/backup\/folder\/.thunderbird\/<oldrandomnumber>.default\/* ~\/.thunderbird\/<newrandomnumber>.default\/<\/code><\/pre>\n\n\n\n
Conclusion<\/h2>\n\n\n\n
cp<\/code> command. If you want to learn more, read its manual pages
man cp<\/code>,
info cp<\/code> and the official cp documentation<\/a>. To get a more rigorous copy command, use rsync<\/a> which preserves all metadata of a file (file permissions, ownerships, extended attributes, etc.).<\/p>\n","protected":false},"excerpt":{"rendered":"