find and awk for httpd owned files (with count and average)

These are not very efficient (two sorts in one line for example) but for my needs work fine (improvements welcomed).

Create a list of “filenames|size in bytes” of all httpd owned files sorted by reverse size:

find "/path/to/examine/" -user httpd -type f -printf '%p|%s\n' | sort -t \| +1 -2 > httpd.usage.txt

results:

/path/to/examine/some/user/files/file0.jpg|99995
/path/to/examine/some/user/files/file7.jpg|99994
/path/to/examine/some/user/files/file1.jpg|99993
/path/to/examine/some/user/files/file2.jpg|99991

Sum, average and count created file

awk -F\| '{ s += $2 } END { print "Sum: ", s,", Avg: ", s/NR, ", No.: ", NR }' httpd.usage.txt

results:

Sum:  10831638685 , Avg:  49539.2 , No.:  218648

Show top10 file counts by user:

cat httpd.usage.txt | awk -F\/ '{print$5}' | sort | uniq -c | sort -nrk 1,1 | head -n 10

results:

  61088 auser123
  35028 auser1435
  13907 auser124
   9952 auser1251
   9225 auser13516
   9181 auser1368
   8608 auser231
   7588 auser479
   6121 auser1338
   5844 auser666

Show bytes owned for a users files of specific user (from existing data):

cat httpd.usage.txt | grep USERNAME | awk -F\| '{print$2}' | awk '{  sum += $1 } END { print sum }'
2802473125

As your directory structures will probably differ from mine you must modify one or more of: +1, -2, $2 or $5

To-Do: Make size calculation automated from top10 list.

This entry was posted in code snippets, geek, servers & tech. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>