Thursday, 1 August 2019

Linux - Sum using awk

Sum a column of file sizes output from an list (ls) command using awk.

ls -lh php*; ls -l php* | awk '{ SUM += $5} END { print SUM/1024/1024 }'
The line above prints the list with 'human' numbers ls -lh
for all files that start with php.
The AWK command sums the fifth column (filesize) of the ls -l (bytes)
and print the result divided by 1024 to give the megabytes total.

-rw-rw-r--    1 admin    site13       1013 Aug 12 11:01 php_checkbox_check.php
-rw-rw-r--    1 admin    site13       8.9k Nov 11 23:15 php_contact_form.php
-rw-rw-r--    1 liamdela site13        560 Oct  1  2002 php_convert_url_to_link.php
-rw-r--r--    1 root     site13        948 Dec  7 21:56 php_gethostbyaddr.php
-rw-rw-r--    1 liamdela site13       2.1k Feb 25  2002 php_list_a_directory.php
-rw-rw-r--    1 liamdela site13       1.7k May 28  2003 php_multi_dimensional_arrays.php
-rw-rw-r--    1 admin    site13       2.9k Nov 14 13:15 php_prev_next_array.php
-rw-r--r--    1 admin    site13       1.9k Feb  3 00:55 php_uk_mailing_list.php
0.0194302

0 comments:

Post a Comment