Showing posts with label one liners. Show all posts
Showing posts with label one liners. Show all posts

Wednesday, January 13, 2016

More day to day one liners


List directories and sort them by largest:


du -hcx –max-depth=5 | grep [0-9]G | sort -rnk1,1

This goes to a depth of 5 directories and sorts largest, only if they are in gigs, and takes into account decimals.

If you need more drive space, you can reduce your reserve space, but you probably don't want to reduce that space to 0 their is a chance your system could grind to a halt. To reduce reserve space to 2 percent:

sudo tune2fs -m 2 /dev/sda1

Now if you need to check what your reserve space is currently set at:

sudo dumpe2fs -h /dev/sda1 2> /dev/null | awk -F ':' '{ if($1 == "Reserved block count") { rescnt=$2 } } { if($1 == "Block count") { blkcnt=$2 } } END { print "Reserved blocks: "(rescnt/blkcnt)*100"%" }'

I didn't write that one, I first encountered it at a hosting gig, but I had to look it up and found it at commandlinefu.

Parsing an html table using perl:

perl -pe "s/.*<tr><th><b>(.*): <\/b><\/th><th.*<\/th><td>(.*)<\/td><\/tr>/\"\1\":\"\2\"/gi"

I'm not going to explain this one, because I wrote it a long time ago and it's perl regex so I would essentially have to rewrite it to understand it.


I have another list of one liners, particularly related to hosting.

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.