Author Topic: Quick command line tricks ....  (Read 2327 times)

0 Members and 1 Guest are viewing this topic.

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1420
  • Karma: 10018
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Quick command line tricks ....
« on: May 06, 2010, 12:54:23 pm »
Couple of useful shell combinations for anyone getting started with the command shell (bash);

Code: [Select]
find . -exec grep -Hin <string> {} \;

Search all files from the current folder downwards (including sub-folders) looking for the text <string> 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.

Code: [Select]
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;

Quote
    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

Code: [Select]
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;

Quote
    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

Offline shtromm

  • Jr. Member
  • **
  • Posts: 55
  • Karma: 2
  • I've just joined!
    • View Profile
    • IT Consultants - IT consultant specialised in Free Software
    • Awards
Re: Quick command line tricks ....
« Reply #1 on: June 18, 2010, 02:48:12 pm »
Couple of useful shell combinations for anyone getting started with the command shell (bash);

Code: [Select]
find . -exec grep -Hin <string> {} \;

Search all files from the current folder downwards (including sub-folders) looking for the text <string> 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.

This can be also done with grep:

Code: [Select]
grep -r '<string>' DirName

will show all files in the directory DirName containing <string>

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1420
  • Karma: 10018
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Quick command line tricks ....
« Reply #2 on: June 18, 2010, 03:22:37 pm »
Useful, will the grep variant list line numbers?

Offline Mad Penguin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1420
  • Karma: 10018
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Quick command line tricks ....
« Reply #3 on: June 18, 2010, 03:34:34 pm »
Mmm, answers own question:

Yes, equivalent command would be;

Code: [Select]
grep -Hirn <string> Dirname

 :D

Offline kirrus

  • Full Member
  • ***
  • Posts: 133
  • Karma: 2
  • Sysadmin / Linux Tech Support
    • View Profile
    • Kirrus' Blog
    • Awards
Re: Quick command line tricks ....
« Reply #4 on: June 23, 2010, 05:24:29 pm »
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.

Code: [Select]
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.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal