{"id":12385,"date":"2019-03-26T22:03:50","date_gmt":"2019-03-26T22:03:50","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12385"},"modified":"2019-03-26T22:03:50","modified_gmt":"2019-03-26T22:03:50","slug":"how-to-copy-a-file-to-multiple-directories-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/how-to-copy-a-file-to-multiple-directories-in-linux\/","title":{"rendered":"How to Copy a File to Multiple Directories in Linux"},"content":{"rendered":"<p><a href=\"https:\/\/www.tecmint.com\/free-online-linux-learning-guide-for-beginners\/\" target=\"_blank\" rel=\"noopener\">While learning Linux<\/a>, it is always the norm for newbies to keep on typing several commands to accomplish a simple task. This is understandable especially when one is just getting accustomed to using the terminal.<\/p>\n<p>However, as you look forward to becoming a Linux power user, learning what I would refer to as \u201cshortcut commands\u201d can significantly reduce time wasting tendencies.<\/p>\n<p>In this article, we will explain an easy way, using a single command to copy a file into multiple directories in Linux.<\/p>\n<p>In Linux, the\u00a0<a href=\"https:\/\/www.tecmint.com\/advanced-copy-command-shows-progress-bar-while-copying-files\/\" target=\"_blank\" rel=\"noopener\">cp command<\/a>\u00a0is used to copy files from one directory to another, the easiest syntax for using it is as follows:<\/p>\n<pre># cp [options\u2026.] source(s) destination<\/pre>\n<p>Alternatively, you can also use the\u00a0<a href=\"https:\/\/www.tecmint.com\/advanced-copy-command-shows-progress-bar-while-copying-files\/\" target=\"_blank\" rel=\"noopener\">advanced-copy command<\/a>, which shows a progress bar while copying\u00a0<a href=\"https:\/\/www.tecmint.com\/find-top-large-directories-and-files-sizes-in-linux\/\" target=\"_blank\" rel=\"noopener\">large files\/folders in Linux<\/a>.<\/p>\n<p>Consider the commands below, normally, you would type two different commands to copy the same file into two separate directories as follows:<\/p>\n<pre># cp -v \/home\/aaronkilik\/bin\/sys_info.sh \/home\/aaronkilik\/test\r\n# cp -v \/home\/aaronkilik\/bin\/sys_info.sh \/home\/aaronkilik\/tmp\r\n<\/pre>\n<div id=\"attachment_23786\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23786\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories.png\" sizes=\"auto, (max-width: 845px) 100vw, 845px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories.png 845w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories-768x123.png 768w\" alt=\"Copy Files to Multiple Directories\" width=\"845\" height=\"135\" aria-describedby=\"caption-attachment-23786\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23786\" class=\"wp-caption-text\">Copy Files to Multiple Directories<\/p>\n<\/div>\n<p>Assuming that you want to copy a particular file into up to five or more directories, this means you would have to type five or more\u00a0<strong>cp commands<\/strong>?<\/p>\n<p>To do away with this problem, you can employ the\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>, a\u00a0<strong>pipe<\/strong>,\u00a0<strong>xargs<\/strong>\u00a0command together with the\u00a0<strong>cp<\/strong>command in the form below:<\/p>\n<pre># echo \/home\/aaronkilik\/test\/ \/home\/aaronkilik\/tmp | xargs -n 1 cp -v \/home\/aaronkilik\/bin\/sys_info.sh\r\n<\/pre>\n<p>In the form above, the paths to the directories (dir1,dir2,dir3\u2026..dirN) are echoed and piped as input to the\u00a0<strong>xargs<\/strong>command where:<\/p>\n<ol>\n<li><code>-n 1<\/code>\u00a0\u2013 tells xargs to use at most one argument per command line and send to the cp command.<\/li>\n<li><code>cp<\/code>\u00a0\u2013 used to copying a file.<\/li>\n<li><code>-v<\/code>\u00a0\u2013 enables verbose mode to show details of the copy operation.<\/li>\n<\/ol>\n<div id=\"attachment_23785\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23785\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/12\/Copy-Files-to-Multiple-Directories-in-Linux.png\" alt=\"Copy File to Multiple Locations in Linux\" width=\"735\" height=\"97\" aria-describedby=\"caption-attachment-23785\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23785\" class=\"wp-caption-text\">Copy File to Multiple Locations in Linux<\/p>\n<\/div>\n<p>Try to read through the man pages of\u00a0<code>cp<\/code>,\u00a0<code>echo<\/code>\u00a0and\u00a0<code>xargs<\/code>\u00a0commands to find useful and advanced usage information:<\/p>\n<pre>$ man cp\r\n$ man echo\r\n$ man xargs\r\n<\/pre>\n<p>That\u2019s all, you can send us questions in relation to the topic or any feedback through the comment form below. You may also want to read about the\u00a0<a href=\"https:\/\/www.tecmint.com\/progress-monitor-check-progress-of-linux-commands\/\" target=\"_blank\" rel=\"noopener\">progress command<\/a>\u00a0which helps to monitor the progress of (<strong>cp<\/strong>,\u00a0<strong>mv<\/strong>,\u00a0<strong>dd<\/strong>,\u00a0<a href=\"https:\/\/www.tecmint.com\/18-tar-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">tar<\/a>, etc.) commands that are presently running in Linux.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/copy-file-to-multiple-directories-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While learning Linux, it is always the norm for newbies to keep on typing several commands to accomplish a simple task. This is understandable especially when one is just getting accustomed to using the terminal. However, as you look forward to becoming a Linux power user, learning what I would refer to as \u201cshortcut commands\u201d &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/how-to-copy-a-file-to-multiple-directories-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Copy a File to Multiple Directories in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12385","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12385"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12385\/revisions"}],"predecessor-version":[{"id":12386,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12385\/revisions\/12386"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}