title: remove the last n lines from all files of a certain type in the current directory, recursive
body: <p>I have the following bash script.</p>
  
  <pre><code>for i in **/*.h; do head $i -n -2 &gt; $i.tmp &amp;&amp; mv $i.tmp $i -f; done
  </code></pre>
  
  <p>The purpose is to run over every .h file inside the current directory and remove the last two lines from that file.</p>
  
  <p>However this does only work as intended on files that are located directly in subfolders of the current directory, but it doesn't process files inside subfolder of subfolders and so on, so ./foo/foobar.h gets processed, but ./foo/bar/foobar.h or for/bar/foobar/.foobar.h don't.</p>
  
  <p>Therefor I have tried it this way:</p>
  
  <pre><code>for i in 'find -name *.h'; do head $i -n -2 &gt; $i.tmp &amp;&amp; mv $i.tmp $i -f; done
  </code></pre>
  
  <p>This fails, because > $i.tmp is ambiguous</p>
  
  <p>How should my script look so that it processes all the header files in a directory, no matter, how deeply they are nested in subfolders?</p>
  
  <p>EDIT:
  If the original file hasn't had a newline directly before eof, the working first script from above (and also the sed alternative from the answers) will add one, so if a newline before eof isn't desired, it has to be removed afterwards:</p>
  
  <pre><code>for i in **/*.h; do head $i -n -2 &gt; $i.tmp &amp;&amp; mv $i.tmp $i -f &amp;&amp; truncate --size=-2 $i; done
  </code></pre>
  
  <p>size should be -2 for CRLF (Windows style) newline and -1 for both, CR (old MacOS style) and LF (unix style), line endings</p>
  
  <p>Of course this approach will also remove a newline before eof, that has already been there before</p>