{"id":3460,"date":"2017-04-02T13:47:46","date_gmt":"2017-04-02T08:17:46","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3460"},"modified":"2017-04-02T13:47:46","modified_gmt":"2017-04-02T08:17:46","slug":"solving-mv-argument-list-long","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/solving-mv-argument-list-long\/","title":{"rendered":"[ Solved -3 Answers] LINUX &#8211; Solving mv : Argument list too long"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<p>We have a folder with more than a million files that needs sorting, but we can&#8217;t really do anything because mv outputs this message all the time<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bash: \/bin\/mv: Argument list too long<\/code><\/pre> <\/div>\n<p>we using this command to move extension-less files:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">mv -- !(*.jpg|*.png|*.bmp) targetdir\/<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>xargs is the tool for the job. That, or find with -exec \u2026 {} +. These tools run a command several times, with as many arguments as can be passed in one go.<\/li>\n<li>Both methods are easier to carry out when the variable argument list is at the end, which isn&#8217;t the case here: the final argument to mv is the destination.<\/li>\n<li>With GNU utilities (i.e. on non-embedded Linux or Cygwin), the -t option to mv is useful, to pass the destination first.<\/li>\n<li>If the file names have no whitespace nor any of \\&#8221;&#8216;, then you can simply provide the file names as input to xargs (the echo command is a bash built in, so it isn&#8217;t subject to the command line length limit):<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">echo !(*.jpg|*.png|*.bmp) | xargs mv -t targetdir<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>You can use the -0 option to xargs to use null-delimited input instead of the default quoted format.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">printf &#039;%s\\0&#039; !(*.jpg|*.png|*.bmp) | xargs -0 mv -t targetdir<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>Alternatively, you can generate the list of file names with find. To avoid recursing into subdirectories, use -type d -prune. Since no action is specified for the listed image files, only the other files are moved.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">find . -name . -o -type d -prune -o \\<br\/>       -name &#039;*.jpg&#039; -o -name &#039;*.png&#039; -o -name &#039;*.bmp&#039; -o \\<br\/>       -exec mv -t targetdir\/ {} +<\/code><\/pre> <\/div>\n<ul>\n<li>If you don&#8217;t have GNU utilities, you can use an intermediate shell to get the arguments in the right order. This method works on all POSIX systems.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">find . -name . -o -type d -prune -o \\<br\/>       -name &#039;*.jpg&#039; -o -name &#039;*.png&#039; -o -name &#039;*.bmp&#039; -o \\<br\/>       -exec sh -c &#039;mv &quot;$@&quot; &quot;$0&quot;&#039; targetdir\/ {} +<\/code><\/pre> <\/div>\n<ul>\n<li>In zsh, you can load the mv builtin:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">setopt extended_glob<br\/>zmodload zsh\/files<br\/>mv -- ^*.(jpg|png|bmp) targetdir\/<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><b>or if you prefer to let <\/b><b>mv<\/b><b> and other names keep referring to the external commands:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">setopt extended_glob<br\/>zmodload -Fm zsh\/files b:zf_\\*<br\/>zf_mv -- ^*.(jpg|png|bmp) targetdir\/<\/code><\/pre> <\/div>\n<p><b>or with <\/b><b>ksh<\/b><b>-style globs:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">setopt ksh_glob<br\/>zmodload -Fm zsh\/files b:zf_\\*<br\/>zf_mv -- !(*.jpg|*.png|*.bmp) targetdir\/<\/code><\/pre> <\/div>\n<p><b>Alternatively, using <\/b><b>GNU mv <\/b><b>and <\/b><b>zargs<\/b><b>:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">autoload -U zargs<br\/>setopt extended_glob<br\/>zargs -- .\/^*.(jpg|png|bmp) -- mv -t targetdir\/<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<ul>\n<li>The operating system&#8217;s argument passing limit does not apply to expansions which happen within the shell interpreter.<\/li>\n<li>So in addition to using xargs or find, we can simply use a shell loop to break up the processing into individual mv commands:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for x in *; do case &quot;$x&quot; in *.jpg|*.png|*.bmp) ;; *) mv -- &quot;$x&quot; target ;; esac ; done<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>This uses only POSIX Shell Command Language features and utilities. This one-liner is clearer with indentation, with unnecessary semicolons removed:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for x in *; do<br\/>  case &quot;$x&quot; in<br\/>    *.jpg|*.png|*.bmp) <br\/>       ;; # nothing<br\/>    *) # catch-all case<br\/>       mv -- &quot;$x&quot; target<br\/>       ;;<br\/>  esac<br\/>done<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>A solution without a catch block using &#8220;$origin&#8221;\/!(*.jpg|*.png|*.bmp):<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for file in &quot;$origin&quot;\/!(*.jpg|*.png|*.bmp); do mv -- &quot;$file&quot; &quot;$destination&quot; ; done<\/code><\/pre> <\/div>\n<ul>\n<li>For a multi-line script you can do the following (notice the ; before the done is dropped):<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for file in &quot;$origin&quot;\/!(*.jpg|*.png|*.bmp); do        # don&#039;t copy types *.jpg|*.png|*.bmp<br\/>    mv -- &quot;$file&quot; &quot;$destination&quot; <br\/>done <\/code><\/pre> <\/div>\n<ul>\n<li>To do a more generalized solution that moves all files, you can do the one-liner:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for file in &quot;$origin&quot;\/*; do mv -- &quot;$file&quot; &quot;$destination&quot; ; done<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Which looks like this if you do indentation:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for file in &quot;$origin&quot;\/*; do<br\/>    mv -- &quot;$file&quot; &quot;$destination&quot;<br\/>done <\/code><\/pre> <\/div>\n<ul>\n<li>This takes every file in the origin and moves them one by one to the destination. The quotes around $file are necessary in case there are spaces or other special characters in the filenames.<\/li>\n<li>Here is an example of this method that worked perfectly<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">for file in &quot;\/Users\/william\/Pictures\/export_folder_111210\/&quot;*.jpg; do<br\/>    mv -- &quot;$file&quot; &quot;\/Users\/william\/Desktop\/southland\/landingphotos\/&quot;;<br\/>done<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p> Solving mv : Argument list too long &#8211; Both methods are easier to carry out when the variable argument list is at the end<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[6355,6354,6356,6362,6360,6357,6365,6359,6361,6363,6364,6358],"class_list":["post-3460","post","type-post","status-publish","format-standard","hentry","category-linux","tag-argument-list-too-long-ls","tag-argument-list-too-long-mv","tag-argument-list-too-long-python","tag-arugument-list-too-long-on-linux-system-python","tag-commonerrormessagesargumentlisttoolong","tag-cp-argument-list-too-long","tag-how-to-set-ulimit-values-red-hat-customer-portal","tag-move-a-large-number-of-files-in-linux","tag-resolved-binrm-argument-list-too-long-error","tag-ulimit-how-do-i-increase-the-open-files-limit-for-a-non-root-user","tag-ulimit-what-is-the-max-opened-files-limitation-on-linux","tag-unable-to-delete-file-argument-list-too-long"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3460"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3460\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}