Praj's Blog

rmdir with -p

You might already know about this one, but if not, rmdir has a -p option like mkdir which allows it to recurse into any child folders and remove them as well if they are empty (it will throw an error if not empty). Handy if you have a folder structure a few levels deep that is empty and want to quickly remove all the folders in one hit. By the way if you didn’t know about mkdir -p it will let you create directories recursively as many levels deep as you need e.g. mkdir -p level1\level2\level3

Access Last Command Line Result

A nifty command to access the last result is $(!!).

For example you can do something like this:

$ find . -type f -name example.txt*
./a/folder/example.txt

$ vim $(!!)

As I only had one result from the find, I can that result and call vim on that file.

Sudo Redirect Output to File

One thing that’s annoying with sudo is that if you don’t own or have relevant permissions on the file you are trying to redirect to, you will get an error.

For example:

$ sudo cat /dev/null > filenotownedbyyou.log

This will fail, because even though you are using sudo, it doesn’t apply to the redirect portion of this command. So you will get “Permission denied”.

The solution is to call the entire command via bash and sudo that:

$sudo bash -c 'cat /dev/null > filenotownedbyyou.log'

This little trick can also be applied to number of situations like this.

MacPorts

If you need to install any range of open source software on your mac (particularly command line tools and servers), then MacPorts is a great way to go. It’s a package manager (much like yum or rpm for Linux) and there are over 15,000 ports available.

Command Line Calendar

As you may have noticed, I’m a big fan of the command line. In fact the main reason I like Mac OSX over Windows is because it is BSD (Unix) based and has a proper terminal.

One cool thing you can do is get the calendar for the current month from the command line like so:

$ cal

     June 2012
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

You can also grab the calendar for the whole year like this:

$ cal 2012