{"id":38928,"date":"2019-10-31T22:26:48","date_gmt":"2019-10-31T19:26:48","guid":{"rendered":"https:\/\/prohoster.info\/blog\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix\/"},"modified":"2019-10-31T22:26:48","modified_gmt":"2019-10-31T19:26:48","slug":"komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","title":{"rendered":"cp team: correct copying of folders with files in *nix","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><img decoding=\"async\" alt=\"cp team: correct copying of folders with files in *nix\" src=\"\/wp-content\/uploads\/2019\/10\/44fed4a64417950ae1f2307d2c9c92ca.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThis article will reveal some less obvious aspects related to the use of <noindex><a rel=\"nofollow\" href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A8%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D0%BE%D0%B8%D1%81%D0%BA%D0%B0\">wildcards<\/a><\/noindex> when copying, the ambiguous behavior of the command <code>cp<\/code> when copying, as well as methods to correctly copy a large number of files without omissions or crashes.<\/p>\n<p>Let's say we need to copy everything from the folder \/source to the folder \/target.<br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><br \/>\nThe first thing that comes to mind is:<\/p>\n<pre><code class=\"bash\">cp \/source\/* \/target<\/code><\/pre>\n<p>\nLet's immediately correct this command to:<\/p>\n<pre><code class=\"bash\">cp -a \/source\/* \/target<\/code><\/pre>\n<p>\n\u2014 some characteristic of the node (for example, a number). The key is needed to identify the element of the tree corresponding to this key. Example of a binary search tree: <code>-a<\/code> will include copying all attributes, permissions, and will add recursion. When exact reproduction of permissions is not required, it is sufficient to use the key <code>-r<\/code>.<\/p>\n<p>After copying, we will find that not all files were copied \u2014 files starting with a dot, like:<\/p>\n<p><code>.profile<br \/>\n.local<br \/>\n.mc<\/code><br \/>\nand similar ones were ignored.<\/p>\n<p>Why did this happen?<\/p>\n<p>Because wildcards are processed by the shell (<code>bash<\/code> in the typical case). By default, bash ignores all files starting with dots, as it treats them as hidden. To avoid this behavior, we need to change the behavior <code>bash<\/code> using the command:<\/p>\n<pre><code class=\"bash\">shopt -s dotglob<\/code><\/pre>\n<p>\nTo preserve this behavior change after a reboot, you can create a file wildcard.sh with this command in the folder <code>\/etc\/profile.d<\/code> (it may be a different folder in your distribution).<\/p>\n<p>And if there are no files in the source directory, then the shell will not be able to substitute anything for the asterisk, and the copying will also terminate with an error. To handle such a situation, there are the options <code>failglob<\/code> and <code>nullglob<\/code>. We will need to set <code>failglob<\/code>, which will prevent the command from executing. <code>nullglob<\/code> won't work, as it converts the string with wildcards that found no matches into an empty string (zero length), which for <code>cp<\/code> will cause an error.<\/p>\n<p>However, if there are thousands of files or more in the folder, it's best to completely abandon the approach using wildcards. The thing is that <code>bash<\/code> expands wildcards into a very long command line like:<\/p>\n<pre><code class=\"bash\">cp -a \/souce\/a \/source\/b \/source\/c \u2026\u2026 \/target<\/code><\/pre>\n<p>\nThere is a limit to the length of the command line, which we can find out using the command:<\/p>\n<pre><code class=\"bash\">getconf ARG_MAX<\/code><\/pre>\n<p>\nWe will get the maximum command line length in bytes:<\/p>\n<pre><code class=\"bash\">2097152<\/code><\/pre>\n<p>\nOr:<\/p>\n<pre><code class=\"bash\">xargs --show-limits<\/code><\/pre>\n<p>\nWe will get something like:<\/p>\n<pre><code class=\"bash\">\u2026.\nMaximum length of command we could actually use: 2089314\n\u2026.<\/code><\/pre>\n<p>\nSo, let's manage without wildcards at all.<\/p>\n<p>Let's just write<\/p>\n<pre><code class=\"bash\">cp -a \/source \/target<\/code><\/pre>\n<p>\nHere we encounter ambiguous behavior. <code>cp<\/code>If the folder \/target does not exist, we will get what we need.<\/p>\n<p>However, if the target folder exists, the files will be copied into the \/target\/source folder.<\/p>\n<p>We can't always delete the \/target folder in advance, as it may contain necessary files, and our goal is, for example, to supplement the files in \/target with files from \/source.<\/p>\n<p>If the source and target folders were named the same, for example, if we copied from \/source to \/home\/source, we could use the command:<\/p>\n<pre><code class=\"bash\">cp -a \/source \/home<\/code><\/pre>\n<p>\nAnd after copying, the files in \/home\/source would be supplemented with files from \/source.<\/p>\n<blockquote><p>Here\u2019s a logical puzzle: we can supplement files in the target directory if the folders are named the same, but if they are different, the source folder will be placed inside the target. How do we copy files from \/source to \/target using cp without wildcards?<\/p><\/blockquote>\n<p>\nTo bypass this harmful limitation, we use a non-obvious solution:<\/p>\n<pre><code class=\"bash\">cp -a \/source\/. \/target<\/code><\/pre>\n<p>\nThose who are well acquainted with DOS and Linux already understand: within each folder, there are 2 invisible folders &#171;.&#187; and &#171;..&#187;, which are pseudo-folders linking to the current and parent directories.<\/p>\n<ul>\n<li>When copying <code>cp<\/code> it checks for existence and attempts to create \/target\/.<\/li>\n<li>Such a directory exists and that is \/target.<\/li>\n<li>Files from \/source have been correctly copied to \/target.<\/li>\n<\/ul>\n<p>\nSo, let's firmly remember this or put it on the wall:<\/p>\n<blockquote>\n<pre><code class=\"bash\">cp -a \/source\/. \/target<\/code><\/pre>\n<\/blockquote>\n<p>\nThe behavior of this command is straightforward. Everything will work without errors, regardless of whether you have a million files or none at all.<\/p>\n<h3>Conclusions<\/h3>\n<p>\nIf you need to copy <b>all<\/b> files from one folder to another, do not use wildcards; it\u2019s better to use <code>cp<\/code> with a dot at the end of the source folder. This will copy all files, including hidden ones, and won't crash with millions of files or complete absence of files.<\/p>\n<h4>Afterword<\/h4>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/users\/vmspike\/\" class=\"user_link\">vmspike<\/a><\/noindex> suggested a similar command yielding the same result:<\/p>\n<pre><code class=\"bash\">cp -a -T \/source \/target<\/code><\/pre>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/users\/oz_alex\/\" class=\"user_link\">Oz_Alex<\/a><\/noindex><\/p>\n<pre><code class=\"bash\">cp -aT \/source \/target<\/code><\/pre>\n<p>\nWARNING: case sensitivity <code>T<\/code> matters. If you mix it up, you\u2019ll end up with complete nonsense: the direction of copying will change.<br \/>\nAcknowledgments:<\/p>\n<ul>\n<li>Companies <noindex><a rel=\"nofollow\" href=\"https:\/\/ruvds.com\/\">RUVDS.COM<\/a><\/noindex> for support and the opportunity to publish on their blog on Habr.<\/li>\n<li>For the image <noindex><a rel=\"nofollow\" href=\"https:\/\/vk.com\/neoprosveschenie\">TripletConcept<\/a><\/noindex>. The image is very large and detailed, you can open it in a separate window.<\/li>\n<\/ul>\n<p>\n<b>P.S.<\/b> Report any observed errors via private message. I increase karma for this.<\/p>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/ruvds.com\/ru-rub\/#order\"><img decoding=\"async\" alt=\"cp team: correct copying of folders with files in *nix\" src=\"\/wp-content\/uploads\/2019\/10\/67131836f7fb6cf7827541e9dc607513.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\n<br \/>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/ruvds\/blog\/471092\/\">habr.com<\/a><\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0435 \u0432\u0435\u0449\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c wildcards \u043f\u0440\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b cp \u043f\u0440\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0430\u0439\u043b\u043e\u0432 \u0431\u0435\u0437 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432 \u0438 \u0432\u044b\u043b\u0435\u0442\u043e\u0432. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0451 \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \/source \u0432 \u043f\u0430\u043f\u043a\u0443 \/target. \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c \u044d\u0442\u043e: cp \/source\/* \/target \u0421\u0440\u0430\u0437\u0443 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":29205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-38928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044b.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u041a\u043e\u043c\u0430\u043d\u0434\u0430 cp: \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043f\u043e\u043a \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0432 *nix | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044b.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-10-31T19:26:48+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2019-10-31T19:26:48+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47The cp command: correctly copying folders with files in *nix | ProHoster","description":"This article will cover.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u041a\u043e\u043c\u0430\u043d\u0434\u0430 cp: \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043f\u043e\u043a \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0432 *nix | ProHoster","og:description":"\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044b.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2019-10-31T19:26:48+00:00","article:modified_time":"2019-10-31T19:26:48+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"38928","title":null,"description":null,"keywords":null,"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-01-23 23:59:19","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-03-01 01:00:26","updated":"2026-08-11 12:50:30","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/38928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=38928"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/38928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media\/29205"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=38928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=38928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=38928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}