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 93.91.192.135 212 66.249.71.12 130 93.186.28.209 108 77.98.166.130 93 161.76.196.229 92 77.101.23.80 90 194.80.21.10 81 195.195.43.18 80 86.25.150.174 78 222.90.192.130
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

:smiley:

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.