Monthly Archives: March 2012

Good bash tricks are hard to find – command within command

Every once in a while I accidentally discover a useful and clever trick and no matter how hard I try, I cannot find it in any sort of documentation. This bash trick is one of my favorites.

Sometimes you want to use a bash command that takes a file as an argument but you don’t actually have a file but another command that creates it or something like that. So what you would normally do is to create a temporary file, use it and delete it afterwards. That’s not so complicated but the extra steps might be annoying and interrupt your flow. What if the command you want to use is part of a one-liner and it would really be a shame to “ctrl-c” the whole thing to create the temporary files, wouldn’t it?

The trick is simple then, all you have to do is put the command that creates the temporary file you want to use inside “<( )” and you’re done. For example, let’s say you have two files, one has 1000 lines and the other has 100 and you want to paste them. Let’s say you don’t want empty spaces on the 900 uncommon lines, so you want the head of the 1000 line file. Now instead of doing:

head -100 1000_line_file.txt > tmpfile
paste tmpfile 100_line_file.txt > pasted.txt
rm tmpfile

You can do it in one line:

paste 100_line_file.txt <(head -100 1000_line_file.txt)

What’s going on being the scenes there is bash actually creates a file descriptor out of the command you put in the parentheses and gives it to the main command (paste, in this example), you can see this happen if you write something like:

ls <(head -100 1000_line_file.txt)

Such a simple yet hardly documented feature, I’m sure remembering this little fellow exists will make your life easier some day.
And you’ll get a result similar to:

/dev/fd/63

Such a lovely trick, yet hardly documented. I’m sure it will make your life easier some day, just try to remember it exists.

Log Rotation for a Good Night’s Sleep

It’s one of the dumbest things you can ever get called out in the middle of the night for – because a filesystem has filled up. Dumb, because it’s preventable and because you shouldn’t be the one doing housework. It’s a computer – its whole purpose is to do the work for you.

The logrotate script was created to monitor, archive and delete log files so you don’t have to. It is an absolutely vital utility with which, in theory, a Linux host could run literally forever without maintenance. It’s installed in the base bundle on all major versions of Linux.

The key things you need to know is that the logrotate process is called by the cron daemon, with the wrapper script located at:

   /etc/cron.daily/logrotate

And each logfile (or set of logfiles) which is to be monitored and archived has its own logrotate configuration file under:

   /etc/logrotate.d/

Every time the logrotate program is executed by cron, it checks every monitored logfile against the conditions in the logrotate.d configuration, and then copies it aside, with a number added as an extension, while resetting the original logfile size to zero.
Continue reading

Improving on Perfection: Tweaking the Linux Mint Desktop

The great selling point of Linux Mint is that it comes pre-installed with everything you need, and very little subsequent configuration is required. The desktop is supposedly a consummation of the most ideal preferences for general users and is designed to provide the greatest benefit to the greatest number of users. This approach has proved very successful for Mint, and the pop charts don’t lie: http://distrowatch.com/dwres.php?resource=popularity

For the rest of us though – particularly those of us who have become habituated over time to our own little peccadilloes – a few tweaks of the desktop are required before we feel at home. Fortunately, the Linux Mint window manager is ripe for customisation.

This post will be relevant to Linux Mint 12, running the Gnome 3 Desktop . Here is the virgin desktop you get fresh after installing from the Live DVD.

The virgin Linux Mint desktop

Mint condition (excuse the pun) is alright for some, but let’s explore a few initial options for pimping things out.
Continue reading