Search code examples
bashexecwc

-exec wc -l {} \; prints count and path, I just need count


Lines=(find $FILEDIRECTORY -iname "*$FILEENDING" -exec wc -l {} \;)

The User can put in his path and file ending and it should count how many lines each program has... if User just wc -l it prints me out how man files I have with that file ending what I want is:

100
78
45

So from every file the lines


Solution

  • You may use it like this:

    find $FILEDIRECTORY -iname "*$FILEENDING" -exec \
    sh -c 'for f; do wc -l < "$f"; done' _ {} +
    

    Please understand that:

    • wc -l < file only prints line count without filename
    • + after exec is much more efficient than \; as find tries to pass multiple files in argument.
    • for f is shorthand for for f in "$@"

    Altenative Solution:

    find $FILEDIRECTORY -iname "*$FILEENDING" -exec grep -hc '^' {} +
    

    If + doesn't work in your find then use:

    find $FILEDIRECTORY -iname "*$FILEENDING" -exec grep -hc '^' {} \;