title: Why do I get "usr/bin/cp(&find): Argument list too long"?
body: <h1>Error: Argument list too long</h1>
  <p><code>sudo cp</code> and <code>find... -exec sudo cp</code> yield</p>
  <pre class="lang-none prettyprint-override"><code>/usr/bin/sudo(or find): Argument list too long
  </code></pre>
  <p>I get this error in my Travis CI build output. My .travis.yml file uses a shell script (deploy.sh) to run a <code>git diff</code> against branch2's force-app folder, and the output (several thousand files) is <code>cp</code> into a new directory:</p>
  <pre><code>sudo cp --parents $(git diff --name-only branch2 force-app/) $DEPLOYDIRECTORY;
  </code></pre>
  <p>The error is two lines:</p>
  <pre class="lang-none prettyprint-override"><code>./deploy.sh: line 122: /usr/bin/git: Argument list too long
  ./deploy.sh: line 122: /usr/bin/sudo: Argument list too long
  </code></pre>
  <p>I have the same issue with the subsequent <strong>find</strong> command. I use a naming convention to find and copy a file that corresponds with each file from the git diff:</p>
  <pre><code>for FILE in $GIT_DIFF_FILES; do
      if [[ $FILE == *Test.cls ]]; then
          find $classPath -maxdepth1 -samefile &quot;$FILE-meta.xml&quot; -exec sudo cp --parents {} $DEPLOY_DIRECTORY +
      fi;
  done;
  </code></pre>
  <p>Again, I get the error: <code>./deploy.sh: line 142: /usr/bin/find: Argument list too long</code>.</p>
  <p><strong>Note:</strong> Travis CI is running an Ubuntu Linux (Xenial) virtual environment for this build.</p>
  <hr />
  <h1>What I've Tried</h1>
  <h3>1. Resetting the stack size</h3>
  <p>Travis CI automatically sets the stack size to <strong>8192</strong> and the arg max to <strong>8388608</strong> for each new build. In my deploy.sh file, I <code>ulimit -s 9999999</code> to change the stack size to <strong>9999999</strong> and the arg max to <strong>2559999744</strong> for each new build.</p>
  <h3>2. Command Variations</h3>
  <p>For the first <code>sudo cp</code> command, I've tried:
  <br /></p>
  <ol>
  <li>formatting command as a <code>for loop</code></li>
  <li><code>tar -cf - -C files... | tar xpf - -C target directory...</code></li>
  <li>formatting command as <code>git diff ... | xargs cp ...</code>
  <br /></li>
  </ol>
  <p>For the <code>find</code> command, I've tried:</p>
  <ol>
  <li><code>find ... | xargs -n 1000 sudo cp ....</code>
  <ul>
  <li>Any different flags added to this command don't work either.</li>
  </ul>
  </li>
  <li><code>find ... -exec cp ...</code>
  <ul>
  <li>Any flags or syntax changes return the same error</li>
  </ul>
  </li>
  </ol>
  <h2>Changing the command doesn't appear to be the solution</h2>
  <p>After reproducing the error locally, I found that my commands already work fine when interacting with a smaller number of files (~1000 or less).
  <br />
  <br />
  However, running the <code>cp</code> and <code>find</code> commands when the output of <code>git diff</code> is approximately <strong>1200-1500 files or more</strong> then returns:</p>
  <blockquote>
  <p>Argument list too long</p>
  </blockquote>
  <p>In addition, simply running <code>find</code> or <code>cp</code> from my shell script also returns:</p>
  <blockquote>
  <p>Argument list too long</p>
  </blockquote>
  <p>It does not seem to matter what I do to these commands, therefore. Something more fundamental is causing the problem, but only when the file count of the <code>git diff</code> output exceeds approximately 1200 files.
  <br /></p>
  <p><strong>How do I fix my <code>cp</code> and <code>find</code> commands?</strong></p>
  <hr />
  <h2>How to Reproduce the Error</h2>
  <p>Here are the steps to take to reproduce this error according to my use case. I used VS Code, so I recommend you do as well to replicate this as similarly to me as possible. Note that you will need to replace <code>USERNAME</code> in the below code with your username.
  <br /><br />
  <strong>Step 1: Fork <a href="https://github.com/forcedotcom/sfdx-travisci.git" rel="nofollow noreferrer">this repo</a>.</strong><br />
  <br />
  <strong>Step 2: Follow the steps below in your terminal</strong></p>
  <pre><code>cd force-app/main/default
  mkdir diff
  git checkout -b branch2
  cd force-app/main/default/classes
  </code></pre>
  <p>Add <code>//comment</code> to the bottom of <strong>myclass.cls</strong> and <strong>myclass.cls-meta.xml</strong> files. Save changes.</p>
  <pre><code>for n in {001..1500}; do cp myclass.cls myclass$n.cls; done
  for n in {001..1500}; do cp myclass.cls-meta.xml myclass$n.cls-meta.xml; done
  git add .
  git commit -m “first commit”
  git checkout master
  for n in {001..1500}; do cp myclass.cls myclass$n.cls; done
  for n in {001..1500}; do cp myclass.cls-meta.xml myclass$n.cls-meta.xml; done
  git add .
  git commit -m “second commit”
  cd .. #back to the sfdx-travisci folder
  sudo cp -p $(git diff --name-only branch2 /Users/USERNAME/sfdx-travisci/force-app/main/default/classes) /Users/USERNAME/sfdx-travisci/force-app/main/default/diff
  for file in $(sudo cp -p $(git diff --name-only branch2 /Users/USERNAME/sfdx-travisci/force-app/main/default/classes) /Users/USERNAME/sfdx-travisci/force-app/main/default/diff); do if [[ $file == *.cls ]]; then find /Users/USERNAME/sfdx-travisci/force-app/main/default/classes -samefile “$file-meta.xml” -exec sudo cp -p {} /Users/USERNAME/sfdx-travisci/force-app/main/default/diff +; fi; done;
  </code></pre>