title: Using find in a directory in node.js
body: <p>I have a function which generate a shell command which use find to delete all files that are not useful anymore:</p>
  
  <pre><code>DOWNLOAD_DIR = '/home/user/directory';
  function purge(psmil, callback) {
      var arg = [DOWNLOAD_DIR, '\\(', '-name', '"*.mp4"', '-o', '-name', '"*.zip"', '\\)', '!', '\\('],
          file = [],
          i = 0;
          cpurge;
  
      //Fill file with names of the files to keep
  
      arg.push('-name');
      arg.push('"' + file[i] + '"');
      i = i + 1;
      while( i &lt; file.length) {
          arg.push('-o');
          arg.push('-name');
          arg.push('"' + file[i] + '"');
          i = i + 1;
      }
      arg.push('\\)');
      arg.push('-ls');
      arg.push('-delete');
  
      cpurge = spawn('find', arg);
  
      cpurge.stdout.on('data', function(data) {
          console.log('data');
      }
      cpurge.stderr.on('data', function(data) {
          console.log('err: ' + data);
      }
      cpurge.stdout.on('data', function(data) {
          callback();
      }
  }
  </code></pre>
  
  <p>Example, it will generate the command:</p>
  
  <pre><code>find /home/user/directory \( -name "*.mp4" -o -name "*.zip" \) ! \( -name "tokeep.mp4" -o -name "tokeep2.mp4" \) -ls -delete
  </code></pre>
  
  <p>Which, put in a .sh file and started, work file, it list all .mp4 and .zip in /home/user/directory, print them and delete them</p>
  
  <p>But when I look at the log of my app, it list everything on the disk, and delete all .mp4 and .zip in the directory</p>
  
  <p>Why?</p>
  
  <p>EDIT: Use find directly</p>
  
  <p>I ve tried to use strace, I ve got this line:</p>
  
  <pre><code>2652 execve("/usr/bin/find", ["find", "/home/user/directory/", "\\(", "-name", "\"*.mp4\"", "-o", "-name", "\"*.zip\"", "\\)", "!", "\\(", "-name", "\"filetokeep.mp4", "-o", "-name", "\"filetokeep2.mp4\"", ...], [/* 17 vars */]) = 0
  </code></pre>