Sunday, June 27, 2010

Finding in files

Finding a string in files is useful and not that straightforward. But don't despair, we have bash.

find . -exec grep -q 'foobar' '{}' \; -print

This searches in the current directory including its subdirectories and finds files that contain foobar.

Sunday, June 20, 2010

Smart Keyword Search with Omnibox in Chrome

Omnibox is a prominent feature of Google’s Chrome Browser. Omnibox is Chrome’s combined search and address bar feature, which allows you to use the address bar as a search bar.
Type a search term in the address box (Omnibox) and press enter to see results from your default search engine.

But it happens that you want to use Youtube search or Wikipedia search instead.

Open Chrome/Preferences/Basic
Click to the Manage button next to the Default Search label.

Here you can see what search engines Google Chrome can use (you can remove from the list or you can add a new item to the list)

Double click on an item shows up a dialog where you can set up the keyword.

If you type this keyword and your search query to the Omnibox, Chrome will search with that engine.

I changed youtube's keyword to yt so if I type yt Humcrush to the Omnibox it'll show me Humcruch youtube videos.

That's smart.

Here is my keywords.

Wednesday, June 16, 2010

What changed in your home folder today.

You can perform a simple Spotlight search from the shell

mdfind [query]

for instance

mdfind Ruby
mdfind -name stdlib.h

and then you pipe the result as you want

mdfind Ruby | grep 'pdf'

You can redefine your search by specifying any of the metadata attribute keys.
Here is a helpful list of metadata attributes that you can use.

You'd like to see what changed in your home directory today, easy.

mdfind -onlyin ~/ '(kMDItemFSContentChangeDate >= $'

Video for IPAD

Problem: You have a DIVX/DVD video on your computer and you want to watch it on your iPad.

Solution: There are a few options how to convert your video:

  • If the original file is supported by QuickTime it would be your first and simplest choice. Just load your file into QuickTime X and choose Save As from the File menu. Then choose HD 480p as a desired format and wait till the conversion is finished (this may take a while)

  • If you have some esoteric data format or you have to get more control over the encoding process or simply you want to get better quality than HD 480p, then you have to use something else.
    Basically there are plenty of choice (I assume you are coming from Unix world):

    1. Mencoder (part of Mplayer)

    2. ffmpeg

    3. x264 - this is free implementation of h.264 codec. The first two use it as a depndecny anyway

    I had a luck with tool number 2. Here is an example how I converted some video files to iPad format:

    for i in *.avi; do ffmpeg -i "$i" -acodec libfaac \
    -ab 96k -vcodec libx264 -s 1024x576 -vpre lossless_ultrafast \
    -crf 20 -threads 2 "$i".mp4; done

    Probably you'd like to set higher audio bitrate etc. But the command above will get you started. If you need explanations what these params do, please refer to the links above.

The only thing left now is to add the newly converted file to your iTunes library and sync with your iPad.


Tuesday, June 15, 2010

Monitoring file system activity on OS X

My hard disk is quite slow, so It seemed a good idea to see what's going on under the hood, why the filesystem is so busy.
I came across fslogger, which is a great user-space program that subscribes to the kernel's file system change notification service.

Sounds great and actually monitoring file system activity with fslogger is quite easy. It must be run as root but that's it.

The output is little bit verbose but you can cut it with awk.

# changed files
sudo fslogger | awk '/FSE_ARG_STRING/ { print $5 }'

# file change type
sudo fslogger | awk '/type.*=/ { print $3 }'

# process that caused the change
sudo fslogger | awk '/pid.*=.*\(.*\)/ { print $4$5 }'


Monday, June 14, 2010

Unix sort by column

Sort is a very useful unix utility. Simplest usage is here::

ls | sort

but that's not very interesting. Let's say we want to sort all the files by size:

ls -l | sort -k5 -n

What this command does exactly sort by 5th field using numeric order. Another useful option you may use is -t which allows you to select the field separator.

Sunday, June 13, 2010

&& and || bash operators

I guess you already know about && operator, which is very common. But || is not so common.

To better illustrate how it works here is an example:

>(exit 1) && echo '0' || echo '1'

>(exit 0) && echo '0' || echo '1'

also you can do tricks like that

>t=`( echo 'some output'; exit 0) && echo $t || echo 'Error!!!'
some output

>t=`( echo 'some output'; exit 1) && echo $t || echo 'Error!!!'

Very tasty usefulness! Isn't it ?

Ruby memory leaks

So we had this problem that some batch processes in our production environment were leaking memory... and the funny part is that it was working fine on our developer machines.
In order to find out what's actually happening to it I've used the tool called
bleak_house, which is very good to examine how your object space is evolving.
Istallation is fairly simple:

gem install bleak_house

And then just run you application using the ruby-bleak-house instead of a regular ruby.

Basically what bleak_house does is it dumps an object space picture to a disk when you press Ctrl-C, or send a USR2 signal to the process.
So I used the second method from crontab to get an image of the process every minute.
Then you can run bleak command to analyze the dump and see what has changed.

In our case it happened that logs were not flushed (sic!), but since we are currently running our application with debugging logs enabled - it led to very fast process growth.