[SOLVED] ‘Unknown’ filesystem in gparted

I had a primary NTFS partition with windows 7 installed on it. Somehow, its bootloader went corrupt so I used install-mbr in ubuntu to fix that but it only made the problem worse. Now my system didn’t recognise the partition as NTFS partition and I couldn’t access the partition.

I modified the partition ID using fdisk but that didn’t work. Fdisk recognised it as NTFS but gparted didn’t, nor did Ubuntu. To fix it, I followed the following steps:

 

      • Download

    testdisk

      •  and extract the files to a folder. The files are compiled executable files so there is no need for any installation. I extracted it in a folder named testdisk on my home directory. Ubuntu users can install it using

    sudo apt-get install testdisk

    cd ~/testdisk/linux
      • Execute the testdisk:
    sudo ./testdisk_static
      Select [Create] and press enter. On the resulting screen, select your harddrive using cursor keys and press enter. On the next screen select the partition table type. It’d generally be Intel if you are using a PC and Mac if you are on Mac. Select the correct one and press enter.
      Now you’ll be presented with lots of options. Use cursor keys to select Advanced and press enter. Select the partition which shows unknown type. Use left and right arrow keys to select [Type] and press enter. You’ll see the list of partition types. In the list the HPFS/NTFS has partition id 07. Press enter, type 7 for NTFS or another partition ID corresponding to your partition type. Press enter.
      Now use the right arrow key to select the [Boot] and press enter. Use right arrow key to select [Rebuild BS] and press enter. Select [Write] and press enter. Now type y. The partition should be fixed by now. You can now quit the testdisk
      • Refresh partition list
    sudo partprobe

 

The disk should be accessible again now

Cheers!

[HOWTO] Download youtube video from command-line

Linux is awesome and so is the terminal. Things become a lot more easy if you can just type in few commands and get your work done. Downloading youtube video has always been messy with GUI downloaders, browser extensions and web services which claim to give you the download link to that video. Now, you can download it from the terminal using youtube-dl in ubuntu. You can also download and install it in other flavors of linux. The script is written in python.

Install youtube-dl from the official repository in Ubuntu:

sudo apt-get install youtube-dl

To download a video execute the following:

youtube-dl <url>

Example screenshot:


More options and switches:

-b            Best quality
-m            Mobile version
-d            High Definition
-g            Don’t download, just show the url
-c            Resume download of a video which was interrupted before
-w            Don’t overwrite existing file

For complete list of switches execute man youtube-dl in terminal.

Cheers!

Passing variable number of arguments to a function in PHP

We all know about functions and how we can pass arguments to it. But did you know that we can pass variable number of arguments in a function?

Consider a function sum() which adds numbers passed into its arguments. Now we want it to add all the numbers passed onto the arguments. Eg; if we call sum (21, 45) or sum (45, 23, 78, 56, 90) it should add all of them. Even if we pass a 100 arguments, it should execute successfully.

Below is the code for making the function sum() :

function sum() {
	$count = func_num_args();
	 $sum = 0;
	 for($i=0; $i<$count; $i++) {
		 $num = func_get_arg($i);
		 $sum += $num;
	 }
	 return $sum;
}

In above function func_num_args() give the total number of arguments that have been passed and func_get_arg() takes argument number as argument and returns the argument value.
We also have another function – func_get_args() which returns all the arguments passed to the function as an array.

For more information and examples on these functions:

func_num_args()
func_get_arg()
func_get_args()

Cheers!

[HowTo] Optimize Mozilla Firefox for better performance

Firefox is getting very popular lately, mainly due to its customizability. I use firefox as my main browser and use all sorts of addons which help me in web-development. These addons make my firefox hungry but I can’t do without them. So, instead I tweaked around some settings in firefox which made it much responsive and faster than before.

To tweak those settings, follow the following steps:

1. Type about:config in your address bar and hit enter.

2. In the filter type browser.session
3. Modify browser.sessionhistory.max_entries by double clicking it. This setting sets the number of steps you can go forward or backward on a page. Default is 50 which is just too much. 20 will be more than sufficient for most people.

4. Modify browser.sessionstore.interval by double clicking it. This setting specifies the rate of saving current session. The default is 10000 milliseconds which means that every ten seconds your sessions are stored. So, when your browser crashes you will, at most, lose only tabs since last nine seconds prior to crash. Change it to 30000 or 20000, whatever suits you. 10000 is definitely not required by most of the people.

5. Modify browser.sessionstore.max_tabs_undo. This setting stores the number of tabs you can undo-close by pressing ctrl-shift-t. Default has been set to 10. I set it to 5 which is more than sufficient for me.

6. Modify browser.sessionhistory.max_total_viewers. This setting can vastly improve the performance. It specifies the number of pages to store in memory. When you press back/forward in a browser, pages are instantly displayed. This is because those pages have been stored in the memory and this setting specifies how many pages should be stored. By default, its -1 which means it depends upon the amount of ram. I have 2 GB and I set this setting to 4.

7. Modify network.prefetch-next. This setting specifies if the prefetching should be enabled or not. Prefetching is when a site tells the browser to download a link (which is most likely to be visited) beforehand only. It is useless and only takes up extra bandwidth and memory. Set the value to false.

Lastly, you can try to change some settings yourself and see the results. You can check the mozillazine knowledgebase to know what a particular setting does. For example, for network.prefetch-next you can check out the setting details at http://kb.mozillazine.org/Network.prefetch-next. Similarly, for other settings.

Some more tricks to free up memory usage of firefox:

1. The awesome bar (address/location bar), as you must be knowing is really amazing feature but it may lead to slowdowns if history is too large. Go to Preferences->Privacy, and in ‘Remember my browsing history for atleast’, put minimum history that you ever require. Mine is 30, since I don’t need more than that and putting a less value always speeds up things. In the same dialog box, In ‘When using the location bar, suggest:’ , try an use only one option if you don’t require both. This will further reduce the address bar lag.

2. Remove useless plugins/extensions: Periodically check the list of your extensions and disable those which you haven’t used since a long time. Extensions are the main cause of Firefox’s slowdown.

3. Use a blank pages as your home page. This makes firefox startup a bit faster.

4. Try and use default theme as themes eat up way too much memory. It delays the startup very significantly.

5. Flash is one of the reasons why Firefox eats so much amount of memory. It has been known that memory taken by a flash animation isn’t freed even after closing that tab. They said they improved this memory leak from Firefox v3 onwards but I don’t see much of a difference. The solution is you can replace all your flash animations with a play button using flashblock extensionsand just click the play button to play the required flash animations.
This way you control which flash files are being played, thereby reducing the amount of memory taken.

These settings will definitely make your Firefox blazing fast. Try and implement all of them and see the difference.

Cheers!

[HowTo] Suspend Ubuntu from terminal or keyboard shortcuts

When learning to make bash scripts, at one point or other you’d like to know how to suspend your computer from terminal. Well, there are quite some methods available for Ubuntu. You can choose whatever suits your needs. I prefer method 3 (look below).

Method 1:

sudo sh /etc/acpi/sleep.sh force

Method 2:

sudo pm-suspend

This command might not be available in older versions of Ubuntu. It works perfectly fine on Lucid and Maverick.

Method 3:

This command does not require sudo so it might come in handy. For example, you can map it to a keyboard shortcut for easy access.

dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

One thing to notice here is none of the commands I mentioned here will ask your password on resuming. For that you’ll have to lock your computer before suspending. You’ll have to use gnome-screensaver-command –lock before suspending to lock the computer

Create a suspend script:

Open a new file in gedit:

sudo gedit /usr/bin/suspend-comp

Copy and paste the following code and save it:

#!/bin/sh 
sleep $1;
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

If you want the script to ask for a password on resuming, copy-paste this code instead:

#!/bin/sh 
sleep $1;
gnome-screensaver-command --lock
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

Add the executable bit:

sudo chmod +x /usr/bin/suspend-comp

Now suspend-comp time-in-seconds can be used in terminal. For example, if I want to have my computer suspended after 20 seconds I can simply type:

suspend-comp 20

Mapping it to keyboard shortcut for easy access:

Go to System->Preferences->Keyboard Shortcuts. Click Add. Type Suspend for Name and suspend-comp 0 in Command field. Click Apply.

Now scroll to the bottom of the list to find Suspend. Click on ‘Disabled’ on the right to ‘Suspend’. Now press any key combination to map the command to it. I used Ctrl+Alt+Shift+s.

Now simply pressing Ctrl+Alt+Shift+s will suspend your computer.

Cheers!

[HowTo] Find number of files inside a folder in Linux

I wanted to find out the number of files inside a folder. I didn’t want the folders to be included in the result.
A blog post on zimbio.com shows us how to do it with ls command which is not only downright inaccurate, it is also slower than what I am going to show you.
I am going to show you how to do it using find command which is extremely powerful once you know how to use it.
Executing the command below will print the number of files (excluding folders) in current directory (including all subdirectories):

find ./ -type f | wc -l

This can be used to find number of directories too:

find ./ -type d | wc -l

You can do more complex counting for example finding out the number of files which are of filesize 1Mb and more.

find ./ -type f -size +1M | wc -l

Finding number of symlinks:

find ./ -type l | wc -l

You can also see number of files accessed within last 1 hour from current directory:

find ./ -type f -amin -60 | wc -l

Here -60 means less than 60 minutes ago.

Only imagination is the limit with what you can achieve using find command.

[HOWTO] Delete all unstarred emails from Inbox or Label in GMail

Its easy to delete all mails from a label/folder or inbox, but sometimes we’d like to save our starred mails and delete all the rest. The search box in gmail comes in handy in such situations. Its really a very powerful tool with various operators to make the search more efficient. The gmail search really gives it an edge over other free email applications.

I have subscribed to one of the yahoo groups and over time it has accumulated over 9000 mails. I quite frequently star the mails which I like. I wanted to delete the rest. To do that I just typed “label:techgroup -is:starred” without quotes in the search bar and clicked on Search. It displayed all the emails from that label which isn’t starred. Notice the hyphen, it means exclude.

Explanation

Its really simple and I doubt anyone would be needing this section.
label:techgroup means we want to display all the mails from label techgroup.
is:starred means we want to display all starred mails form the label.
But the – infront of is:starred negates what it does. It means we want to exclude starred items from the search results. So we get unstarred mails from label techgroup.

After making the search, click on the checkbox beside the ‘Archive’ button to select all the emails on that page. You’ll get another option just above the search result which would say:

All 20 conversations on this page are selected. Select all conversations that match this search

Click on ‘Select all conversations that match this search’. This will select all the matched results. Click on ‘Delete’ and empty the trash if you want.

Read more about the advanced search in gmail.

Cheers!

Extract almost any archive through terminal using a single command in linux

I came across this simple script on ubuntuforums which I thought was really very useful and worth sharing it on my blog. You can either make a function out of it and put it in .bashrc file or make an executable script and put it in /usr/bin/.

Method 1:

Open your ~/.bashrc file using any editor.

gedit ~/.bashrc

Copy and paste the following code at the end of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extract-file () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjvf $1 ;;
*.tgz) tar xzvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract-file" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

Now you can use the following command to extract any archive:

extract-file

The command extract-file would be available only to terminals which have been opened after saving the .bashrc file with the above code. Also this code is user-specific, so if another user logs in he cannot use this command.

Method 2:

Use the following command to create a new file in /usr/bin directory and launch the gedit.

sudo gedit /usr/bin/extract-file


Copy and paste the following code in gedit and save the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjvf $1 ;;
*.tgz) tar xzvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract-file" ;;
esac
else
echo "'$1' is not a valid file"
fi

This is essentially the same code but instead of using it as a function, we’ll use it as a script.

Now we’ll add the executable bit to the file:

sudo chmod +x /usr/bin/extract-file

You can now use the following command in terminal to extract (almost) any kind of archive.

extract-file <filename>

Note that we have used /usr/bin directory here. You can use any other directory included in the PATH environment variable directory or add a new directory in PATH environment variable and use it here.

[How-To] Find windows live (hotmail) contacts on twitter

Twitter removed its hotmail/msn contact importer tool last year which was a very useful tool. However, there are few tricks which enables you to do just that. The easiest one is:

Twitter allows you to import gmail contacts.
Gmail allows you to import MSN contacts.

Hence, you can indirectly import MSN contacts in twitter by importing them first on your gmail account.

Step by step instructions:

  • Create a new gmail account or use your existing one if you don’t mind having your hotmail/msn/windows live  and gmail contacts all at the same place. I used my existing one.
  • Click on settings on the top right corner once you are logged in to your gmail account.
  • Click on ‘Accounts and Import’ and click on ‘Import Mail and Contacts’.
  • A pop-up window will appear asking your email address. Enter your complete hotmail email address and click on Continue.
  • Enter your password and click on Continue.
  • Select Import Contacts and unselect all others (You can select others too if you want to import mail) and click on Start Import.
  • You’ll be presented with a message saying it may take up to 2 days. It doesn’t, it takes only few minutes for contacts to import. The message is only valid for importing mails. Click on ‘OK’.
  • Log in after some time and you’ll see a message on the top of the screen saying that import has been completed.
  • You can now log in into twitter and use the gmail import tool to find your hotmail friends.

I hope that helped.

Cheers!