Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First you do not need to use <code>cat</code> to count lines. This is an <a href="http://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat" rel="noreferrer">antipattern called Useless Use of Cat</a> (UUoC). To count lines in files in the current directory, use <a href="https://en.wikipedia.org/wiki/Wc_(Unix)" rel="noreferrer"><code>wc</code></a>:</p> <pre><code>wc -l * </code></pre> <p>Then the <code>find</code> command recurses the sub-directories:</p> <pre><code>find . -name "*.c" -exec wc -l {} \; </code></pre> <ul> <li><p><code>.</code> is the name of the top directory to start searching from</p></li> <li><p><code>-name "*.c"</code> is the pattern of the file you're interested in </p></li> <li><p><code>-exec</code> gives a command to be executed</p></li> <li><p><code>{}</code> is the result of the find command to be passed to the command (here <code>wc-l</code>)</p></li> <li><p><code>\;</code> indicates the end of the command </p></li> </ul> <p>This command produces a list of all files found with their line count, if you want to have the sum for <strong>all</strong> the files found, you can use find to list the files (with the <code>-print</code> option) and than use xargs to pass this list as argument to wc-l.</p> <pre><code>find . -name "*.c" -print | xargs wc -l </code></pre> <p>EDIT to address Robert Gamble comment (thanks): if you have spaces or newlines (!) in file names, then you have to use <code>-print0</code> option instead of <code>-print</code> and <code>xargs -null</code> so that the list of file names are exchanged with null-terminated strings. </p> <pre><code>find . -name "*.c" -print0 | xargs -0 wc -l </code></pre> <p>The Unix philosophy is to have tools that do one thing only, and do it well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload