Anything can go here, in any language... except my native language Sinhala. Be cool... anybody is warmly welcomed! :)

Invoke Google's Web Search from Bash

Nice ideas come in handy way :) ; invoke Google's web search from your Linux desktop shell! :-O

Here's a small shell script that I wrote couple of minutes ago. Copy-paste it into a new file, save it inside /usr/bin as google. This works in GUI mode with firefox very well, but it doesn't work with lynx. I still could not find any reason,.. may be somebody can come up with an idea.

if [ $# -ne 0 ]
  for i in $@
  firefox $query &
  echo "Usage: google <your search query>."

Then chmod +x /usr/bin/google as root to make it executable.

Now, enter the following as a command and see: B-)

$ google shaakunthala


Just press Alt+F2, type google shaakunthala and press Enter.

You can use whatever as your keyword. But, have to follow usual bash (shell) syntax.

For example,
If your query is sameera shaakunthala, your command should be,
$ google sameera shaakunthala

If your query is shaakunthala's portal, your command should be,
$ google shaakunthala\'s portal

If your query is "shaakunthala's portal", your command should be,
$ google "\"shaakunthala\'s portal\""

... and so on.

And use your creativity and combine this with some google search hacks that you know. It can be a powerful web search command from your desktop! :-O

Bye! Ciao! Sweet dreams! :P

*** Update ***

I re-wrote the script for lynx. The problem arises with the hash (#)sign in the URL. So I removed it. The whole thing is more convenient in the command line, than in the GUI mode.

if [ $# -ne 0 ]
  for i in $@
  lynx -accept_all_cookies $query
  echo "Usage: google <your search query>."

Here's the code explanation (how it works):
When you execute the script with some arguments, it will concatenate all arguments with plus sign (+) in between. This is necessary to parse the input as web browser URL format. Then it will concatenate it to the Google search URL string to pass it to the server script at Google.

And finally pass the whole thing as an argument to your web browser.

if - then - else - fi : Decision making
$# : Total number of arguements passed to the shell
-ne : not equals
for - do - done : Looping
$@ : all the arguments (except the command itself) passed to the shell

Ciao! :)

MP3 Batch Transcoding with Linux

Well, hello there; after some time... this one is about some small work I've done, that I think usful for other Linux users.

 I'm huge fan of music -- especially hard rock and thrash metal. I feel almost dead if I don't get a chance at least once a day, to listen to them. So I wanted to put my huge collection into my mobile phone's memory chip so I can carry them anywhere I go. But, my poor memory chip is just 1 GB. It's not enough at all! So, the only way to pack as much as I can is, reduce the bitrate of the MP3 files.

The next problem... I don't use Windows, nor Windows based software. But, if occurs a situation where I don't have any chance with Linux, I have installed Windows inside a virtual machine for use. But I rarely use that. Of course I can use JetAudio or any other audio conver to do this easily, but, as a professional and permanent Linux user, that's not my style.

So I asked our smart genie... Google to find out a solution. Here's what I've got:
for file in `ls`
file=`echo $file | sed s/\ /_/g`
echo $file
lame --decode $file
lame -b 128 $file.wav
rm $file
rm $file.wav
mv $file.wav.mp3 $file

It's a shell script for batch processing. It will process all the MP3 files inside a given directory. Yes, that's more than what I wanted, but, what would happen if filenames contain whitespaces? The solution is not much practical, because most MP3s we get contains at least one whitespace in filename. I don't have time to rename each and every file. So I wsa thinking about a suitable solution.

It is based on lame encoder, if you don't have it, please install it first.
# apt-get install lame

Yes, the find command! Got that!!! Here's the code I've wrote:
Copy this into a text file (

if [ $# -eq 2 ]
mkdir $1/output
cd $1
find . -maxdepth 1 -exec lame -h -b $2 {} ./output/{}  \;
echo "Invalid arguements, please refer"

Then execute,
$ chmod +x

Now run it,
$ ./ <mp3_files_directory> <target_bitrate_kbps>

$ ./ /home/ubuntu/Music/MyMusic 128

It will make a directory called output inside your music directory, and put the encoded files in.

It's simple as that! I think it's easier than JetAudio, isn't it? ;)

How I Prepared My GRUB-Bootable USB Flash Drive

Again... the GNU GRUB. I really can't forget such an interesting software that I can study. Today, I'm going to put the English version of my another Sinhala blog post. It's about how I made my flash drive a bootable one.

Now you might say,.. "That's pretty easy stuff with Windows... Just right click and Format...."; wait.........! I'm not going to talk about the DOS/ Windows bootloader. DOS/ Windows bootloader is nothing compared to the GNU GRUB. What I'm going to put here is how to put the GRUB + kernel into your flash drive.

First, I would like to give a small introduction on the GNU GRUB. Wikipedia got a whole lot of information,.. but I'll also explain. Simply, GRUB is a bootloader. A bootloader is the program loads the operating system when your computer boots. Have you ever seen the "NTLDR is missing" error on a Windows XP installed system? Yes, that NTLDR is the bootloader of Windows NT based operating systems. There are two most used types of bootloaders with Linux. One is LILO (LInux LOader) and the other is GRUB (GRand Unified Bootloader). Due to numerous reasons the most popular bootloader among the two is the GRUB. I'm not going to list them here,.. but believe me.. the GRUB is really a 'thing' to study!

So,.. we are bit off the topic.Let's get back on it. How would it be if I install the GRUB on a USB flash drive? That's what came into my mind when I was reading the posts on UCSC LMS (I'm a student). Yes, it would be great! There are several advantages I can think of.
  • If I boot a Live CD, it takes some time. But, here as it is a bootloader only system, it takes a less time to boot up.
  • I can study further,.. (I'm not a Linux expert)
  • I can hijack Linux systems,... :D
  • I can check the RAM using memtest+86.bin kernel

I have a 4 GB flash drive. So here's how I did with it:

1. Backed up all the data on the flash drive as I'm gonna partition it.

2. Divided the drive into two primary partitions. It doesn't matter whether it is primary or logical, but as I didn't need any more partitions, I set it as this.

# fdisk /dev/sdb

One partition is a FAT-32 one to keep my personal files,which I might need to use with both Windows and Linux. It should be the first partition on the flash drive. Unless, Windows will spoil up everything. I left 50 MB at the end of the partition table as unpartitioned/ free space which I'm going to use for the boot partition.

As I've mentioned earlier, Windows can not identify the partitions other than the physically first partition on the flash drive partition table. That means, even if the partition number is not 1, it should exist first to be identified by Windows.

The second one, is nearly 50 MB, and is ext-2 type. It holds the GRUB's files and the kernel.

Formatted the partitions using the following commands:
# mkdosfs /dev/sdb1 -v -F 32 -n LEONIDAS_4G
# mke2fs /dev/sdb2 -L boot

Now mounted them,
# mkdir /media/data; mount -t vfat /dev/sdb1 /media/data

# mkdir /media/boot; mount /dev/sdb2 /media/boot

3. Well, there could be a systematic way to do this. But, I'm not a Linux expert. So, please don't laugh at me. This is how I did it:

# cd /
# cp -rfv boot /media/boot

Now, the GRUB installation, the thing I recently got to know from our LMS forum.

# grub
grub> find /media/boot/boot/grub/stage1

Now it gives the following output:

According to the notation, the second partition on the flash drive is (hd1,1). The next step is;

grub> root (hd1,1)
grub> setup (hd1)
grub> quit

Now, the GRUB has been installed on the flash drive.

4. Now I carefully examined the boot partition on the flash drive. There were two versions of the kernal and related files. This might be probably due to a kernal update on my system. It doesn't matter. I kept the most recent version and deleted the other.

5. Finally I edited the menu.lst, which caontains the initial configuration of the GRUB, when it boots. Here's the entire menu.lst file:

title    Linux kernel 2.6.27-14-generic
root     (hd0,1)
kernel   /boot/vmlinuz-2.6.27-14-generic root=UUID=d1670f8e-eb3f-4dba-bba5-e00f0437e2a2 ro single
initrd   /boot/initrd.img-2.6.27-14-generic

title    memtest86+
root     (hd0,1)
kernel   /boot/memtest86+.bin

There's another few important things to state here. Althoug the flash drive is (hd1) here, it becomes (hd0) when booting. The reason is almost obvious, when you set it as the first boot device on the BIOS, it becomes the first device.

The next thing is the UUID. UUID is sent to the kernel as a parameter. You can find the UUID using this:

# vol_id --uuid /dev/sdb2

6. Okey,.. I'm almost done. The final step is to boot. The GRUB loading can bee seen and then the boot menu. You can either edit the menu and boot the kernel on the hard disk or boot the kernel on the flash drive.

There is no init on the flash drive because I didn't put it on the drive. So when booting, it show an error message, and drops into a BusyBox shell. It works on the RAM (initramfs). In simple terms, there is a shell = we can make use of the kernel. :)

7. Restore the backup (personal data/ files) onto the FAT 32 partition on the flash drive. If you plug the flash drive inta a Windows machine, the whole thing may look like this:

Do not delete any of the partitions using the Windows Disk Management Console. If you want to format your data drive, you can go through My Computer and format. Do not touch the other one as it could ruin the entire filesystem on the flash drive which we have built so far. Also if you want to adjust the partition table, the safe option is to do it with Linux.

Finally, I have to say is I've experimented and learnt something new. And I wanted to share my experience here!

Thanks for reading.

Solution for Some USB Flash Drive Problems

Today I'm gonna put the English translation of one of my Sinhala blog posts, which talks about a solution some possible problems that might occur with USB flash drives. Roughly I would say the solution is to destroy and rebuild the filesystem on the device. But this is not actually the same content from original blog post.

Well, most of us are carrying those portable memory devices... some call it pen drive... and some call it flash drive... and some other may call thumb drive. Whatever it is, it has become an essential thing with our day to day life.

First, I must tell an important thing,.. pay extreme attention when following these instructions. Unless, a single mistake could ruin your entire hard drive!

OK, the first thing that I'm going to point out is, you may have seen that capacity of a flash drive is indicated less than the value printed on it. I'm not talking about indicating a 4 GB drive as 3.77 GB. Sometimes you might have seen a scenario where the drive has been indicating 3.77 GB, but later it has become a lower value like 1.2 GB. Yes, I'm talking about that problem.

In certain other cases, Windows would not detect the device. It might show a message like 'Insert disk...' also.

As far as I think, the reason behind this might be physical, electrical or electronically damage to the device. Also, viruses are also a suspect. However, if the damage is too severe, then you might not be lucky enough to succeed with these instructions. I have tested this several times successfully, but unsuccessful attempts are there as well.

So, my instructions are based on Linux, to be executed on a command line shell, free, open and adventurous! :)

First, you need to backup existing data on the device. Use any backup tool or you can use the following command. (Assumed that the flash drive is /dev/sdb and it's filesystem is FAT32)
# mount -t vfat /dev/sdb1 /mnt
# tar cvpzf backup.sdb.tgz /mnt

If you are not sure about the device file (/dev/?d?), just enter the following command and identify your flash drive.
# fdisk -l

Now unmout the device if mounted.
# umount /dev/sdb

Then we will clear everything on the device, including it's partition table (overwriting MBR). We do not have this option with Windows.
# fdisk /dev/sdb
Enter o to create a new disklabel (this is not the drive label which we can see on a windows parition)
Enter w to write the MBR and exit fdisk.

Now, create the only partition on the flash drive. Of course you can have multiple partitions on the flash drive. But I'm not sure about whether Windows would detect all the partitions.
# fdisk /dev/sdb
Enter n to create a new partition.
Enter p to make it primary.
Enter 1 as the partition number.
Press Enter twice to use default starting and ending points.
Enter w to write changes and exit.

We have finished setting up the partition, now the next thing is to format. It's a good choice to format as FAT32 because when you do so, the drive can be used with both Windows and Linux. If the device capacity is more than 4 GB, I would recommend NTFS. Well, that would be another story. I'm gonna write about that some day as well. If it is the memory stick on your mobile phone, your choice should be FAT16 -- this depend on your phone's type also.

To format as FAT32,
# mkdosfs -F 32 -cv -n DRIVELABEL
I use verbose (-v) option so I can see the progress, -c is to probe for bad clusters (this can take some time), and -n for Windows disk volume label (DRIVELABEL in this example) which can be or less than 11 ASCII characters.

To format as NTFS,
# mkntfs -C -I -v -L DRIVELABEL
-C to enable compression, and I to disable indexing (Windows XP), -v for verbose output and -L for volume label which cab be or less than 32 ASCII or unicode characters.

So, this is the end of my post. This method is not alwsys successful, but I was lucky in two of three situations.

Thanks for reading!


Subscription Options

 Subscribe in a reader


Enter your email address:

to inscrease

and make me happy. :)

Other Blogs by the Author

The Author

My photo
Ambalangoda, Southern Province, Sri Lanka
Shaakunthala works as a Systems Support Engineer at a World's leading IT solutions company. A wannabe hacker, FOSS enthusiast, cat lover and an insane motorcyclist. And he comes from Sri Lanka!! :)
ශාකුන්තල | Shaakunthala's items Go to ශාකුන්තල | Shaakunthala's photostream
Related Posts with Thumbnails