Sunday, January 10, 2016

Some explanation on my posts at bdm or everyday one liners for hosting

My tumblr blog I mostly used for saving one liners, for when I was working in hosting, bad decision method has a bunch of one liners I would like to explain.
For mac, finding largest files:

sudo du -hcx | perl -nle 'print "$_" if(/^(\s|)(\d\.\d+G|\dG)/);' | sort -rnk1,1 

Mac's have some interesting compatibility quirks with certain things in the *nix universe. And I believe I composed this particular one liner because the -P switch on grep is not valid on a mac. So what I do was a standard, what files with du -hcx, piped that to perl and used perl in lieu of grep -P so I could seperate, then pipe to sort, what I needed to do to find the largest files and directories on my mac.

Ok so here is one on how to find bots / spiders / crawlers, in a certain time frame:

cat /var/log/httpd/access_log | perl -nle 'print "$_" if(/02:0(\d):(\d+)/);' | egrep 'bot|crawl|spider'

So it searches your log for any time between 2:00:00 and 2:09:59, with bot, crawl, or spider. This is useful if you are trying to determine if a site is down because yandex and google-bot are slamming it at the same time. 

This defeats a certain test at a certain hosting company:

perl -nle ‘print “$1” if(/(Question (\d+|\d)(.*)| (correct answer.*))/);’ quest

I've said to much already.

But for all of this shit, where I have used perl...awk is probably the better, more elegant solution.

Occasionally at my hosting job, some ubuntu boxes would just forget what happened, and where there root directory was supposed to be mounted, this is the quickest, though not the recommended way to fix that:

cat /proc/mounts > /etc/mtab

Also if you want to see the guy who beat me, in a perl(me) vs awk(him) head off, you should head to his blog, here.
He has a sed vs my perl on the apache log, finding certain times that's pretty elegant too. I mean I try to always recommend the best tool for the job, I find myself using php to often for this reason, and I don't like it, like when I just quickly need to iterate through json or something, I guess I should be using node for that. Ultimately though I use perl for text processing, because I know it well, which sometimes makes it the fastest tool for the job.

No comments: