A reverse cat!
# cat file
1st line
2nd line
3rd line
# tac file
3rd line
2nd line
1st line
Sometimes you need to process a file starting at the end, case in point a log file and you want to go from the last lines up. I remember programming something like this in C some years ago because I didn’t knew this command existed…
Everybody knows the rsync command. It’s very useful when you need to keep two machines in sync over the network, minimizing the bandwidth used.
The only problem lies when running it for the first time, or when a new file appears and it’s newly uploaded to the other end: if the network connection is unstable and goes down, rsync deletes the partially uploaded file and it needs to be uploaded again. In other words, when copying new files instead of syncing existing ones, rsync only copies full files.
Luckily there’s a command line parameter to avoid this behaviour and, in the event of a network failure, force rsync to leave the partially-uploaded file as-is and continue syncing it from there on the next run:
# rsync --help
...
--partial keep partially transferred files
Many times the reason because a web server is slow and unresponsive is that it’s under “attack”, on purpose or not, by a bot. I’ve seen cases where Google Bot, bots from research engines from universities or some other kind of indexer were responsible for more than half the traffic of a site. These cases are not real DoS attacks, this traffic can be considered legitimate, but the result is that it brings the service down. You can instruct some of these bots not to visit your site so often, like Google Bot using the Google Webmaster Tools and the sitemaps and/or robots.txt files, but usually you can’t and have to consider filtering all this traffic at the firewall. But in any case, the first step is realizing that a single IP (or a couple of them) is responsible for most of your traffic, identifying this IP and using whois learn who it belongs to.
You can run something like this to list the top five IP addresses on your Apache’s access.log:
cut -d" " -f 1 access.log | sort | uniq -c | sort -nr | head -n 5
The tar command, like many other UNIX commands, can use stdin/stdout as its input/output target by using “-” instead of a real file.
What’s this useful for? For example, copying a full directory tree while keeping dates, ownerships and permissions, locally or remotely with ssh.
$ tar cf - . | ( cd /some/other/dir ; tar xfv - )
$ tar cf - dir | ssh usr@srv "tar xfv -"
It’s possible to assign a default value to a variable in case it isn’t already defined. The syntax is:
FOO=${FOO:=bar}
Example:
$ FOO=${FOO:=bar}
$ echo $FOO
bar
$ BAR=test
$ BAR=${BAR:=foo}
$ echo $BAR
test
watch runs a command and keeps its output on screen updating it ever X seconds and, optionally, highlighting the differences on the output between executions. It’s an easy way to monitor the output of some command without having to spend several minutes pressing “cursor up - enter”.
watch -n 1 "ps aux | grep apache"
Si te ha interesado, ¿me invitas a una cerveza? / If you liked the post, would you buy me a beer?
One thing I always missed in WordPress is the ability to select the size of the thumbnail that’s generated every time you upload a picture to your blog. The default size is quite small, sometimes you may want it, for example, to be as wide as the page, without needing to scale the original pic down in HTML with width=”xx”.
I’ve found today the imagesControlSize plugin by aNieto2k, that allows you to select the thumbnail’s width when uploading an image (the height is calculated automatically, maintaining the aspect ratio). Works great.
A small detail about Time Machine I hadn’t thought about before: this article recommends removing all the virtual disks from virtual machines like VMWare, Virtual Box, et al. from the backup.
Makes sense: Time Machine works at a file level, in the end it’s just something like this but with an über-posh interface. So every time you boot one of your VMs, some small change will inevitably be made on some file inside it, causing Time Machine to store a new copy of the full virtual disk.
Some days ago, a representative of Redfone Communications got in touch with me because of a HOWTO I wrote some months ago about building Asteirsk clusters with the fonebridge2: they liked it and wanted me to write a Case Study about the cluster I built at my previous job.
The Case Study has just been published and is available here:
At work I have a Dell laptop with a Broadcom BCM94311MCG wireless card:
# lspci
...
0b:00.0 Network controller: Broadcom Corporation BCM94311MCG wlan mini-PCI (rev 01)
...
# lspci -n
...
0b:00.0 0280: 14e4:4311 (rev 01)
...
The drivers that Ubuntu installs by default were giving me lots of headaches depending on the network’s access point: on some of them the card worked OK; on some others I kept loosing the connection every few minutes, or I couldn’t connect at all. I never found out if the problem was the encryption algorithm in use, or the wifi a/b/g/whatever protocol. Bottom line is the driver worked on some networks but didn’t on some others.
A co-worker told me he had the same problem until he switched to the ndiswrapper driver, so reluctantly I tried it. It work great. 
More info here: