How many lines are in each file.
Use wc
, originally for word count, I believe, but it can do lines, words, characters, bytes, and the longest line length. The -l
option tells it to count lines.
wc -l <filename>
This will output the number of lines in :
$ wc -l /dir/file.txt
32724 /dir/file.txt
You can also pipe data to wc
as well:
$ cat /dir/file.txt | wc -l
32724
$ curl google.com --silent | wc -l
63
How many lines are in directory.
Try:
find . -name '*.pl' | xargs wc -l
another one-liner:
( find ./ -name '*.pl' -print0 | xargs -0 cat ) | wc -l
BTW, wc
command counts new lines codes, not lines. When last line in the file does not end with new line code, this will not counted.
You may use grep -c ^ , full example:
#this example prints line count for all found files
total=0
find /path -type f -name "*.php" | while read FILE; do
#you see use grep instead wc ! for properly counting
count=$(grep -c ^ < "$FILE")
echo "$FILE has $count lines"
let total=total+count #in bash, you can convert this for another shell
done
echo TOTAL LINES COUNTED: $total
How many lines in total
Not sure that I understood you request correctly. e.g. this will output results in the following format, showing the number of lines for each file:
# wc -l `find /path/to/directory/ -type f`
103 /dir/a.php
378 /dir/b/c.xml
132 /dir/d/e.xml
613 total
Alternatively, to output just the total number of new line characters without the file by file counts to following command can prove useful:
# find /path/to/directory/ -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'
613
Most importantly, I need this in 'human readable format' eg.
12,345,678 rather than 12345678
Bash has a printf function built in:
printf "%0.2f\n" $T
As always, there are many different methods that could be used to achieve the same results mentioned here.
change the output of 'printf' for your needs
– malyy Feb 08 '16 at 15:34printf
doesn't read its arguments fromstdin
, but rather from the command line (compare piping toecho
vs piping tocat
;cat
reads fromstdin
,echo
doesn't). Instead, useprintf "$(find ... | xargs ...)"
to supply the output as arguments toprintf
. – BallpointBen Aug 21 '18 at 22:02find . -name '*.pl' | xargs wc -l | sort -k1 -n -r | head -n 5
– Raphaël Mar 31 '23 at 08:47