{"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\/nl\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","title":{"rendered":"cp-commando: correct mappen met bestanden kopi\u00ebren in *nix","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p><img decoding=\"async\" alt=\"cp-commando: correct mappen met bestanden kopi\u00ebren in *nix\" src=\"\/wp-content\/uploads\/2019\/10\/44fed4a64417950ae1f2307d2c9c92ca.jpg\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nIn dit artikel worden enkele minder voor de hand liggende zaken met betrekking tot het gebruik van <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> bij het kopi\u00ebren, het onduidelijke gedrag van de <code>cp<\/code> kopieeropdracht en methoden om een groot aantal bestanden correct te kopi\u00ebren zonder over te slaan of fouten.<\/p>\n<p>Stel dat we alles willen kopi\u00ebren van de map \/source naar de map \/target.<br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><br \/>\nHet eerste wat in je opkomt is:<\/p>\n<pre><code class=\"bash\">cp \/source\/* \/target<\/code><\/pre>\n<p>\nLaten we deze opdracht meteen verbeteren naar:<\/p>\n<pre><code class=\"bash\">cp -a \/source\/* \/target<\/code><\/pre>\n<p>\nSleutel <code>-a<\/code> voegt het kopi\u00ebren van alle eigenschappen toe, behoudt permissies en voegt recursie toe. Wanneer exacte weergave van rechten niet nodig is, is de sleutel <code>-r<\/code>.<\/p>\n<p>Na het kopi\u00ebren zullen we merken dat niet alle bestanden zijn gekopieerd; bestanden die beginnen met een punt, zoals:<\/p>\n<p><code>.profiel<br \/>\n.lokaal<br \/>\n.mc<\/code><br \/>\nen dergelijke, zijn genegeerd.<\/p>\n<p>Waarom is dit gebeurd?<\/p>\n<p>Omdat wildcards door de shell wordt verwerkt (<code>bash<\/code> in de typische situatie). Standaard negeert bash alle bestanden die beginnen met punten, aangezien deze als verborgen worden beschouwd. Om dit gedrag te voorkomen, moeten we de configuratie wijzigen <code>bash<\/code> met de opdracht:<\/p>\n<pre><code class=\"bash\">shopt -s dotglob<\/code><\/pre>\n<p>\nOm deze wijziging permanent te maken na een herstart, kun je een bestand wildcard.sh maken met deze opdracht in de map <code>\/etc\/profile.d<\/code> (mogelijk is de map anders in jouw distributie).<\/p>\n<p>Als er geen bestanden in de bronmap zijn, kan de shell niets vervangen voor de wildcard en zal het kopi\u00ebren ook met een fout eindigen. Voor een dergelijke situatie zijn er opties <code>failglob<\/code> en <code>nullglob<\/code>. We moeten <code>failglob<\/code>, instellen, zodat de opdracht niet kan worden uitgevoerd. <code>nullglob<\/code> is niet geschikt, omdat deze de string met wildcards zonder overeenkomsten omzet in een lege string (nul-lengte), wat voor <code>cp<\/code> een fout zal veroorzaken.<\/p>\n<p>Echter, als er duizenden bestanden in de map staan, is het beter om helemaal af te zien van het gebruik van wildcards. Dit komt omdat <code>bash<\/code> de wildcards omzet in een zeer lange opdrachtregel die eruitziet als:<\/p>\n<pre><code class=\"bash\">cp -a \/source\/a \/source\/b \/source\/c \u2026\u2026 \/target<\/code><\/pre>\n<p>\nEr is een limiet aan de lengte van de opdrachtregel die we kunnen achterhalen met de opdracht:<\/p>\n<pre><code class=\"bash\">getconf ARG_MAX<\/code><\/pre>\n<p>\nWe krijgen de maximale lengte van de opdrachtregel in bytes:<\/p>\n<pre><code class=\"bash\">2097152<\/code><\/pre>\n<p>\nOf:<\/p>\n<pre><code class=\"bash\">xargs --show-limits<\/code><\/pre>\n<p>\nWe krijgen iets als:<\/p>\n<pre><code class=\"bash\">\u2026.\nMaximale lengte van de opdracht die we eigenlijk konden gebruiken: 2089314\n\u2026.<\/code><\/pre>\n<p>\nDus laten we helemaal zonder wildcards werken.<\/p>\n<p>Laten we gewoon schrijven<\/p>\n<pre><code class=\"bash\">cp -a \/source \/target<\/code><\/pre>\n<p>\nEn hier stuiten we op onduidelijk gedrag. <code>cp<\/code>. Als de map \/target niet bestaat, krijgen we precies wat we nodig hebben.<\/p>\n<p>Echter, als de map target bestaat, worden de bestanden gekopieerd naar de map \/target\/source.<\/p>\n<p>We cannot always delete the folder \/target in advance, as it may contain files we need and our goal, for instance, is to add files from \/source to \/target.<\/p>\n<p>If the source and destination folders had the same names, for example, if we were copying 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 have been supplemented with files from \/source.<\/p>\n<blockquote><p>Here\u2019s a logical puzzle: we can supplement files in the destination directory if the folders are named the same, but if they differ, the source folder will be placed inside the destination. How to 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>\nWie degenen die goed bekend zijn met DOS en Linux al hebben begrepen: binnen elke map zijn er 2 onzichtbare mappen &#171;.&#187; en &#171;..&#187;, die fungeren als pseudo-mappen-koppelingen naar de huidige en bovenliggende directories.<\/p>\n<ul>\n<li>When copying <code>cp<\/code> it checks for existence and tries to create \/target.<\/li>\n<li>That directory exists and it is \/target.<\/li>\n<li>Files from \/source have been correctly copied to \/target.<\/li>\n<\/ul>\n<p>\nSo, let\u2019s put it in a bold frame in our memory or 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 unequivocal. Everything will work without errors, regardless of whether you have a million files or none at all.<\/p>\n<h3>Conclusies<\/h3>\n<p>\nIf you need to copy <b>all<\/b> files from one folder to another, do not use wildcards; instead, it is better to use <code>cp<\/code> in combination with a dot at the end of the source folder. This will copy all files, including hidden ones, and will not crash with millions of files or the complete absence of files.<\/p>\n<h4>Naschrift<\/h4>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/users\/vmspike\/\" class=\"user_link\">vmspike<\/a><\/noindex> suggested a similar result with the command:<\/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 them up, you will get complete nonsense: the direction of copying will change.<br \/>\nThanks to:<\/p>\n<ul>\n<li>The company <noindex><a rel=\"nofollow\" href=\"https:\/\/ruvds.com\/\">RUVDS.COM<\/a><\/noindex> for the support and the opportunity to publish in their blog on Habra.<\/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 noticed errors privately. I will raise your karma for this.<\/p>\n<p>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/ruvds.com\/ru-rub\/#order\"><img decoding=\"async\" alt=\"cp-commando: correct mappen met bestanden kopi\u00ebren in *nix\" src=\"\/wp-content\/uploads\/2019\/10\/67131836f7fb6cf7827541e9dc607513.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\n<br \/>Bron: <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.3 - 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\/nl\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"nl_NL\" \/>\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\/nl\/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: correct copying of folders with files in *nix | ProHoster","description":"This article will cover.","canonical_url":"https:\/\/prohoster.info\/nl\/blog\/administrirovanie\/komanda-cp-pravilnoe-kopirovanie-papok-s-fajlami-v-nix","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"nl_NL","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\/nl\/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\/nl\/wp-json\/wp\/v2\/posts\/38928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/comments?post=38928"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/posts\/38928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/media\/29205"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/media?parent=38928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/categories?post=38928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/nl\/wp-json\/wp\/v2\/tags?post=38928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}