I'm looking for a way to list all files in a directory, with both their size, and a line count. Right now I'm using stat -c \"%s %n\" /directory/*
to get file names and sizes, and I know I can use find /directory/ -type f -exec wc -l {} +
to get file name and line count, but is there any way I can get both at the same time?
5 Answers
You are almost there.
find directory -type f -exec wc -lc {} +
will get file name, line count, and character count.
Strictly speaking, -c
(a.k.a. --bytes
) is documented as counting bytes,
which is probably what you want.
There is also a -m
(a.k.a. --chars
) option for counting “characters”.
From the choice of the m
option letter, I guess this counts multi-byte characters;
e.g., Unicode characters.
There is also a -w
option for actually counting words.
-
Yes, this is it! I can't believe I totally missed -c, thanks!– jmatulaCommented Feb 20, 2015 at 16:56
-
wc
can provide both byte and line counts:
find /directory/ -type f -exec wc -l -c {} +
Using find
is also preferable to a wildcard argument (stat ... directory/*
), because the latter will fail when there are too many files in the directory for the names to fit in a single command.
wc -c -l file1 file2 ...
answers must be >= 30 characters long :)
-
2The approved technique for meeting the minimum character count (ironic!) requirement is to explain your answer. Commented Feb 20, 2015 at 16:56
for i in 'find \directory-name\ -type f'
do
echo "Printing line count and size of $i"
cat $i | wc -l
du -sh $i
done
I think this will help you in getting what you want.