Google from your terminal

Foo, sometimes, when you are on terminal, its soo lazy to open google chrome from the dock, I just wrote a foolish script that does it:

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
args=("$@")
all_terms=${args[0]}
i=1
while [ $i -lt ${#args[@]} ]
do
        all_terms=$all_terms+${args[i]}
        i=$[$i+1]
done
google-chrome http://www.google.co.uk/#q=$all_terms

Well, just copy the above script in a file, chmod +x it, all it as an alias using may be “google” as alias name to your .bashrc and source .bashrc and have fun :P

Linux Tricks

ls with full absolute path names:
ls -d $PWD/*

What the Hell? I need to back up !!

Ok, I’m currently in a shiit, I’m having to backup my stuffs on my servers and this I need to do it quite frequently. So the issue is:

-Automatically backup frequently(check cron jobs if you want to know)

-Naming Convention to timestamp the folder

So, how do I recognise when a file has been copied? We timestamp the folder using a naming convention say dd.mm.yy.hh.mm.ss(day.month.year.hour.minute.second). We use scp to copy the folder to my server, ensure that your public and private keys are setup so that you can automate it. The key here is the naming convention.

scp -r /path/to/folder server@username.com:/path/to/folder/$(date +\%d.\%m.\%y.\%H.\%M.\%S)

Ok, if you really want to automate the whole stuff:

  • Set up your keys to ensure that authorization can be automated
  • Its good to create an alias of the command so that you have a shortcut whenever you want to do it manually
  • create a crontab and add the frequency

Extract Audio from webm file linux

You are on linux and there is no software to retrieve audio from a webm file, no worry, open source is here :)

if you are on ubuntu, runĀ sudo apt-get install ffmpeg libavcodec-extra-52, on Red Hat distribution, run
yum install ffmpeg

After successfully installing it, you can do a man ffmpeg to see the command and argument description.

To convert a simple file, just do

ffmpeg -i videoFile.webm -ab 320k -ar 44100 audioFile.mp3

Reload bash in Linux

I was a fool, ya I was. When I newly started with Linux, each and every time I was changing my bashrc, I was restarting my PC just to reload the new bash setting, crazy naa. In fact, you can do this in a very easy way:

just run this command:

source .bashrc

and it will do the job

Openshift Console in terminal

Needs an openshift console to control your openshift application, just install rubygem-rhc, i’m using fedora 17, I just installed it with yum. First, do a yum provides to check the version available from repository and then you install it

1. yum provides rubygem-rhc

2. yum install rubygem-rhc-0.91.12-3.fc17.noarch

The openshift console is simply your terminal and some commands to control the application from it.

Now that you have installed, you can take control of your openshift account from your terminal:

Openshift console command

rhc-app ( | cartridge cartridge-action | --help) [args]

in your terminal, just run the command rhc-app, you’ll get the syntax of the command including all the command, cartridge-action and arguments,

Now suppose you want to restart your application,

its as simple as

rhc-app restart -a applicationName -l usernameOpenShift

if everything goes fine, it should output a success
RESULT:
Success

There are some links where you can find more information:

1. https://openshift.redhat.com/community/get-started
2. https://openshift.redhat.com/community/kb/kb-e1067-running-rhc-commands-without-re-entering-password

Broadcom on fedora 17 due to missing firmware

Getting Firmware missing on broadcom fedora 17

broadcom fedora 17.So, I’ve just installed Fedora 17, no great changes in UI, but it’s nice. Now, as always, the problem I get is to set up wireless on laptop(I love my old Dell Inspiron 1545) which uses a broadcom 4312. In case, you are having the same issue, you can follow the procedures below or follow the links found at the end of this post.

First, ensure you have b43-fwcutter,

yum install b43-fwcutter

Now, perform the following:

1 export FIRMWARE_INSTALL_DIR="/lib/firmware"
2 wget http://www.lwfinger.com/b43-firmware/broadcom-wl-5.100.138.tar.bz2
3 tar xjf broadcom-wl-5.100.138.tar.bz2
4 sudo b43-fwcutter -w "$FIRMWARE_INSTALL_DIR" broadcom-wl-5.100.138/linux/wl_apsta.o

The above information has been found in the links below:

1.http://linuxwireless.org/en/users/Drivers/b43#If_you_are_using_the_b43_driver_from_3.2_kernel_or_newer
2.http://ask.fedoraproject.org/question/2053/wireless-broadcom-4312

Git

History

So, it’s always nice to know a bit of history. What happened was that the Linux Kernel was being maintained using bitkeeper free of charge. However, later Bitkeeper free of charge license terminated. So, Linus Torvalds decided to create Git to maintain the Linux Kernel Development.

Differences with other VCSs

Git is a Version Control System. There are many other VCSs but what’s good with it is that it was initially designed to maintain the Linux Source code which is huge. By that, you can understand that it’s going to consider lots of aspects such as size, bandwidth optimization and many others into consideration.

There are several differences between Git and other VCSs is that Git stores whole snapshots of the project rather than only deltas. Also, files are checksumed using SHA-1 Hash before they are stored and referenced using the checksum.

Now, a file in Git can be three states:

  1. Modified
  2. Staged
  3. Committed

It’s very simple. Whenever, we create/modify a file, we need to explicitly tell Git (Staging Process) to include the file in the next version. Now, to create this “next version”, we simply commit.

Configuration Files

Git has 3 level of configuration:

  1. System Wide Configuration
  2. User Specific Configuration
  3. Project Specific Configurations

What’s more interesting is that the configuration are overridden. So, User Specific configuration overrides System Wide Configurations and Project Specific configurations overrides User Specific configurations.

To view configurations, you can simply use this command:
git config --[system|global|local] -l

1. System Wide Configurations
Global git configurations are stored in the /etc/gitconfig file. To add/modify to this file, simply pass —system as an argument
git config --system user.name "Noorani Bakerally"

2. User Specific Configuration
All user specific configurations are stored in a .gitconfig file. This file is found in the home folder of users. So, to display it, simply run cat ~/.gitconfig.To add/modify to this file, simply pass – -global as an argument
git config --global user.name "Noorani Bakerally"

3. Project Specific Configuration
So, project specific configurations overrides all configurations. So, add/modify these project specific configurations, simply ensure that in the git repository of the project and do git config user.name "noorani Bakerally"

Git Objects

There are 4 types of object:

  1. blog: Consider it as a file in a git repository
  2. tree: A directory which can contain other trees or blobs
  3. commit: A specific point in time containing meta information and a pointer to the previous commit
  4. tag:A milestone in the life of the project. e.g. A new version

Also, all object have 3 attributes, a type(1 of the above 4), a size and its content.

Git basics

To create a git repository
mkdir new_git_repository
cd new_git_repository
git init new_git_repository

To clone an existing git repository
git clone <URL_Existing_git_repository>

To view status of files from the last commit
git status

File tracking
To manage your files, git track your file so that it can identify and changes to it. This is not automatic as you can avoid tracking some files but not adding adding or simple creating pattern in a gitignore file(we’ll see that later)

To explicitly tell git to track a file, do

git add <filename>

Creating/managing commit
git commit -a -m "the commit message", -a:all, -m:message
A commit is like a milestone in the life of a project. It’s like saving the project state in the project life time. A commit is represented in git as a 40 character SHA-1. Anything that is committed in git can be obtained again. At any point in time, you can return back to a specific commit using the reset.

git reset --hard <commit SHA1 Code>

The above command is dangerous. Suppose currently you have some modifications pending (not yet committed), if you run it, you lose all your modifications. Remember, anything committed can be regained. The negation is also true.

Suppose that you have created a commit and have forgotten to include some files. Therefore, just after the commit, perform the necessary modifications and add the files. Then do a

git commit --amend

To undo a commit,

git reset --[soft|hard] HEAD^
soft leaves all unchanges while hard removes all changes and returns back to the previous commit(HEAD^ refers to the previous commit).

Revert and Undo is different. Revert returns to a previous commit and commit this return. To undo a commit using revert,

git revert HEAD

To revert to a previous commit if you know the commit code:

git revert <SHA-1 Code of commit>

You can also revert by going back in history without entering the hashcode:
# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

Just by changing the number in the {}, you can revert back

Sometimes you need to know where you are:
To view the location of the current head,
cat .git/HEAD

this outputs the reference of the current head, ref: refs/heads/master

now to view the current head, simply perform a cat on the file:

cat .git/refs/heads/master

To get the difference between two branches:

git diff –name-status –color branch1..branch2

e.g. git diff –name-status –color master..development

To change the name of the branch you are currently are:

git branch -m newname

File Operations within a git repository
Like in any other folder, in a git repository, you can perform file operations like creating,removing,editing or moving file. For all file operations, you need to explicitly tell git about it. For the “create” operation, consider the file tracking above.

References:

  • http://www.codinginahurry.com/2011/02/05/three-levels-of-git-config/
  • http://linuxmanpages.net/manpages/fedora13/man1/git-config.1.html
  • http://git-scm.com/book
  • http://www.gitguys.com/topics/head-where-are-we-where-were-we/

Formatting HTML options

One day I had a boring thing to do which in the end came an interesting one. So, I had a list of items and I had to format all these items in proper HTML option tag like

1
Text

Formatting HTML options is easy but when there are too many, it’s damn boring.
Consider a similar problem , I had a file like this CarBrands.txt. For each and every item, I had to format it so that I can input it in an HTML select.

Of course, one way was to do it manually. Again, I hate manual tasks. So I though a little and again I found a solution that worked. Again, the solution works for Linux and only Linux.

So try to get a copy of the file above and try it.

Formatting HTML Options using AWK

First, display the whole file

1
cat carBrands.txt

Now, let’s start adding a bit of HTML in it. So lets only add the option tag with the attribute value and no text

1
2
3
4
cat carBrands.txt | awk 'BEGIN{}{ print "
" $n "

";}'

This outputs something like:

1
Audi AWZ Barkas Bitter BMW Borgward

Ok, now I’m having to add the value in ascending order from value 1 to the number of items the file contains. So its simple, I just need to start with a counter of 1 and increment it each time.

1
2
3
4
cat carBrands.txt | awk 'BEGIN {val=1}{ print "
" $n "

";val++}'

So, as you may already know, the first {} of awk is run at start, the middle one is run for each line and the last is run at end. So, I here, I need only the first one for initialising the counter and middle one for each like. Finally, thats how many options looks like :)

1
Audi AWZ Barkas Bitter BMW Borgward

Formatting HTML options using AWK is useful only if you have lots of options. Lol, Don’t use this if you have only 2 options. AWK is interesting, isn’t it? I’m also thinking of learning a little bit more of it. Here are two links I’ve considered:

Terminator, several tabs, horizontal and vertical splitting solved my issue

In the past, I was not command-line friendly. I would use windows, Linux and Mac OSx. But in all the three, there were some problems due to following reasons:

Windows

  • Foooo, viruses, Windows first devil
  • I try to do several tasks at the same time, but there no workspaces

Mac OSx

  • Mac is great, beautiful user interfaces,compatibility for professional programs but again no workspaces

and finally………….. Linux

  • I knew only how to use GUI
  • not all programs are compatible
  • but it had workspaces :)

So, finally, I chose Linux and believe me, it was the best choice. But, I learnt a little bit of command line and I started to enjoy and finally that’s how I was enjoying

I was no longer using GUI, so Terminal came and I had to use several workspaces simulteneously, so Terminator came :)