Quick command line tricks ....

Couple of useful shell combinations for anyone getting started with the command shell (bash);

find . -exec grep -Hin <string> {} \;

Search all files from the current folder downwards (including sub-folders) looking for the text and printing the files that match. Search is case insensitive and a line is printed (with a line number) for each line the string appears in.

cat /var/log/apache2/access.log|cut -d" " -f1|sort|uniq -c|sort -rn|head -10

Print the top 10 clients accessing your web server in terms of number of hits on the site, sample output;

407 212 130 108 93 92 90 81 80 78
cat /var/log/apache2/access.log|cut -d'"' -f2,3|cut -d" " -f2,5|sort|uniq -c|sort -rn|head -10

Print the top 10 artifacts on your web server in terms of number of the number of times they’ve been downloaded and their size, sample output;

213 /logo.jpg 12222 208 /spinner.gif 2037 205 /portal_css/Plone%20Default/images/top.png 2045 205 /portal_css/Plone%20Default/images/middle.png 538 205 /portal_css/Plone%20Default/images/bottom.png 2102 204 /input_background.gif 460 203 /search_icon.gif 433 201 /portal_css/Plone%20Default/images/background.jpg 30730 188 /favicon.ico 778 180 /portal_css/Plone%20Default/popoll-cachekey9302.css 202

This can be also done with grep:

grep -r '<string>' DirName

will show all files in the directory DirName containing

Useful, will the grep variant list line numbers?

Mmm, answers own question:

Yes, equivalent command would be;

grep -Hirn <string> Dirname


Here’s one. This is a set of commands that will tell you the top ten IP addresses connected to your computer or server, sorted by number of connections.

netstat -nt | awk '{print $5}' | cut -d: -f1 | sort -n | uniq -c | sort -nr | head

You have to use sort twice because uniq is dumb.