Thursday, February 24, 2011

5 useful Grep options that you must know

Grep is a very handy utility for searching for a pattern in files. While it has many options, every grep user should at-least know the following five options


-R, -r, --recursive

This option makes grep search all the files in the specified directory and its sub-directories, recursively. This specifically comes in handy for programmers when they are looking for the declaration or all uses of some variable/function/anything in the source tree.
So, for example if you want to search the current directory for all files containing the word "foo" then you can use:
grep -r foo .


-n, --line-number

This option makes grep to prefix the ouput with the line number of each matched line withing its input file. This makes it easier to make changes as you know the line number, so just open the file, go to the specific line and make the required changes.



-I

This option makes grep ignore binary files i.e binary files are not searched. This option is again very helpful for programmers who only want to search in the source files.



-A NUM, --after-context=NUM

This option prints NUM lines following the matched line from the input file. For example if you want the output to contain the matched line and 2 lines after it then you can use:
grep -A2 <PATTERN> <FILES>


-B NUM, --before-context=NUM

This option prints NUM lines before the matched line from the input file. So if you want the output to contain the matched line and 2 lines before it then you use
grep -B2 <PATTERN> <FILES>


Thursday, February 10, 2011

OpenSUSE: turn firewall off

To disable the firewall on OpenSUSE use the following command as root user:
/sbin/SuSEfirewall2 off
To turn it back on use:
/sbin/SuSEfirewall2 on

Sunday, January 30, 2011

Remove envelope icon from Indicator Applet

The envelope icon in Indicator Applet really annoys me. It unnecessarily increases the number of clicks needed to open an application like Empathy or Pidgin. To remove that icon uninstall the indicator-messages package using following command:
sudo apt-get remove indicator-messages
This makes that envelope icon disappear and applications start appearing in Notification Area.

Wednesday, January 19, 2011

Limit Internet Bandwidth Usage (internet speed) in Ubuntu

There are many reasons one would want to limit internet bandwidth/speed on a system e.g you dont want your siblings to choke your internet or, if you are a network admin, you dont want one user, downloading a big movie over torrent, to hurt every one else's surfing.

To limit internet bandwidth on a Ubuntu machine install wondershaper. For this open a terminal and type following command:
sudo apt-get install wondershaper
Now use ifconfig command to find out the name of interface connected to internet. The name will be something like eth0, eth1, wlan0 or something similar. In my case it is eth0


Now issue a command similar to follwoing (relpacing eth0 with your interface name):
sudo wondershaper eth0 1024 256
this will limit the download speed to 1024 Kbps and upload to 256 Kbps. Remember that this limit is for all internet traffic i.e torrent, ftp, browsing and any thing else.

If you want to remove these limits use
sudo wondershaper clear eth0
again replacing eth0 with your interface name

Using LD, the GNU Linker, to create a relocatable section of code or data

Relocatable sections are used when you cant load a section of code/data directly where you want. This may be for many reasons for example the memory where the code is supposed to go needs to be initialized first as is the case in ROM images.

For a relocatable section you need two addresses:
1. Relocation Address
2. Load Address

Relocation Address is the address for which the section is linked. Whereas Load Address is the address where the section will get loaded. Confusing? Lets try an example:

Consider an embedded system with 64MB ROM memory mapped at address 0x1E000000 and 256 MB DRAM mapped at 0x00000000. Your code will reside in ROM but when the system starts, it will copy it self from ROM into DRAM and continue execution from there.

To create a relocatable section we use AT keyword in section definition as follows
section_name startaddr : AT( loadaddr)
{
    /*section contents*/
    ...
}
 This will create a section which is linked for address startaddr but which is loaded in memory at loadaddr. Going back to above example embedded system, you will need at least two sections, one will contain the actual code and the other will contain the relocation/copying logic. This is required because the code for copying will get executed from ROM, so it needs to be linked for that address. So the GNU ld script will look something like following:

.init 0x1E000000 :
{
    *(.init)
}
.text 0x00000000 : AT ( ADDR(.init) + SIZEOF(.init) )
{
    *(.text)
}
the expression ADDR(.init) + SIZEOF(.init) will give us the address of first byte just after .init section. So .text section will get loaded just after the .init section. Upon reset, the code in .init section will copy the text section to DRAM i.e address 0x00000000 and pass control to it.

Tuesday, December 21, 2010

Install Firefox 4 beta (Minefield) on Ubuntu

UPDATE: Firefox 4 (stable version) is now available for Ubuntu. So you can install it instead. Instructions on installing Firefox 4 can be found here

To install Firefox 4 beta on Ubuntu execute following commands
sudo add-apt-repository ppa:ubuntu-mozilla-daily/ppa
sudo apt-get update
sudo apt-get -y install firefox-4.0
The new browser will appear as Minefield 4.0 Web Browser in Applications Menu. It will be installed alongside your existing Firefox install (if any).

Sunday, December 19, 2010

Track Your Internet Bandwidth Usage with NTM (Network Traffic Monitor)

For those of us with limited network connection Network Traffic Monitor (NTM) is a very helpful tool. It can monitor upload, download or total network traffic. It can also monitor time spent online and lot of other things. NTM is not available in Ubuntu repositories so you will have to download the .deb from here and install it. After installing start NTM from Applications->internet->NTM. Now right click on the NTM icon  in the taskbar and select preferences from the menu that appears. This will open the NTM preferences window. In the General tab specify the interface that you use to connect to internet. In my case it is wlan0


Now select Traffic tab and enter the traffic limit of your internet plan. I have entered 2G i.e 2048MB as an example value.


You can select Auto Disconnect if you want the connection to disconnect as soon as you reach your limit. This can be very helpful if extra MBs are charged at a high rate. Click Apply and you are all done. NTM will monitor your internet use and you can left click on the NTM icon in Taskbar to see your current speed, bandwidth you have consumed and lot of other information.


If you have time limited connection and not bandwidth limited i.e you buy internet hours and not GBs then you can also use NTM to keep track of time you spend on internet. Open the time tab to configure time related options.