4

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?

6

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! – jmatula Feb 20 '15 at 16:56
  • can we print the sizes in MB ? – Hussain Akhtar Wahid 'Ghouri' Sep 5 '16 at 11:19
2

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.

1

we can simply exexute the following command in the directory

wc -lc *
0
wc -c -l file1 file2 ... 

answers must be >= 30 characters long :)

  • 2
    The approved technique for meeting the minimum character count (ironic!) requirement is to explain your answer. – Scott Feb 20 '15 at 16:56
0
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.