Linux commands : Part 3


Let’s have a look at some of advance commands that we won’t be using frequently but are handy if known.


1) Console output to file
To write output of terminal commands to text file, we can use ‘>’ or ‘tee’.
The ‘>’ followed by file name will create a new or append to existing (with flag) file, where all output will be witten.

mount > mount_info.log

It’ll redirect and write all output to ‘mount_info.log’ file.

mount | tee mount_info.log

It’ll will display command output in terminal and write it to ‘mount_info.log’ file also.
tee : read from standard input and write to standard output and files.

2) System hardware info
To get system hardware information, we can use ‘lshw’ : list hardware command. It’ll display detail informations related to cpu, memory, disk, partition, firmware etc.

sudo lshw

The output will look like this :

ark
description: Portable Computer
product: Inspiron N5110 (To be filled by O.E.M.)
vendor: Dell Inc.
version: Not Specified
serial: 6DR0HP1
width: 64 bits
capabilities: smbios-2.6 dmi-2.6 vsyscall32
configuration: boot=normal chassis=portable sku=To be filled by O.E.M. uuid=44454C4C-4400-1052-8030-B6C04F485031
*-core
description: Motherboard
product: 0FXK2Y
vendor: Dell Inc.
physical id: 0
version: A06
*-cpu
description: CPU
product: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz
vendor: Intel Corp.
physical id: 4
bus info: cpu@0
version: Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz
serial: To Be Filled By O.E.M.
slot: CPU 1
size: 800MHz
capacity: 800MHz
width: 64 bits
clock: 100MHz

3) Execute multiple commands
To execute multiple commands in single go, we can use a ‘;’ separator. The commands will be executed from left to right in order.

sudo mkdir hello; sudo mv hello ~/Desktop

Create a directory named ‘hello’, then move it to desktop.

4) Unlock locked resource
When we run a package manager and install/update packages then it perform exclusive lock. If we try to run second package manager then we get error message stating :

E: Could not get lock /var/lib/dpkg/lock – open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

To unlock the resouces, we can use ‘fuser’ to find out the runaway process and kill the process that is hogging the resource.

$ sudo fuser -vki /var/lib/dpkg/lock; sudo dpkg --configure -a

fuser : identify processes using files or sockets.
k : kill the process that is still using /var/lib/dpkg/lock.
i : Ask the user for confirmation before killing a process.
Then configure all packages that are yet unpacked and unconfigured.

Format USB Drive Using Command Line


Formatting USB drives via command line takes only few steps and its easy.

Plug in your USB drive to the computer and run these following steps to format them.


1. View the mounted drives on system

mount

You should get output something like this:

/dev/sda6 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
/dev/sdb1 on /media/pj/Cruzer type vfat
(rw,nosuid,nodev,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,showexec,
flush,uhelper=udisks2)

The command will list all currently mounted drives on system.


2. Unmount the USB drive that needs to be formatted

sudo umount /dev/sdb1

The ‘/dev/sdb1’ is the media drive where USB drive was mounted, see step 1. It can be different from you.


3. Format the drive
To FAT filesystem.

sudo mkdosfs -n 'MyDrive' -I /dev/sdb1

To FAT32 filesystem

sudo mkdosfs -n 'MyDrive' -I -F 32 /dev/sdb1

To Ext4 filesystem

sudo mkfs.ext4 -n 'Label' -I /dev/sdb1

mkdosfs : creates an MS-DOS file system under Linux
mkfs : build a Linux filesystem
n : Volume name
I : This switch will force mkdosfs to work properly
For more information on mkdosfs, use man mkdosfs.
Similarly, for more information on mkfs, use man mkfs.

Eclipse selection background color fix in ubuntu

This is a article about how to change the highlight color in autocomplete box in Eclipse

Please check out this stackoverflow problem. There is really cool solution given for this annoying problem.

http://stackoverflow.com/questions/8012176/how-to-change-the-highlight-color-in-autocomplete-box-in-eclipse

Mercurial HG setup

Installing mercurial
Here, we will setup & run “Mercurial HG” locally in linux. First we install mercurial hg itself.

sudo apt-get install mercurial

Try out the mercuial to check if installation was success by :

hg --version

The output should be something like this in your terminal.

Mercurial Distributed SCM (version 2.2.2)
(see http://mercurial.selenic.com for more information)

Copyright (C) 2005-2012 Matt Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Mercurial configuration
We will create a configuration files under ‘/usr/sw/mercurial’ directory(you can choose any directory) named : hgweb.config, hgweb.wsgi & hg-wsgi.conf

hgweb.config file will be like,

[web]
allow_push=*
push_ssl=false
allow_archive=gz, zip, bz2
[collections]
/usr/sw/mercurial/repo=/usr/sw/mercurial/repo

“/usr/sw/mercurial/repo=/usr/sw/mercurial/repo” tells mercurial where all our repository will be located.

Our hgweb.wsgi file should contain,

# Path to repo or hgweb config to serve (see 'hg help hgweb')
config = "/usr/sw/mercurial/hgweb.config"

# enable demandloading to reduce startup time
from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb import hgweb
application = hgweb(config)

hg-wsgi.conf file will be like,

WSGIScriptAlias /hg /usr/sw/mercurial/hgweb.wsgi

<Directory "/usr/sw/mercurial/">

Options ExecCGI FollowSymlinks
AddHandler wsgi-script .wsgi

AllowOverride All
Order allow,deny
Allow from all

AuthType Basic
AuthName "Mercurial Repositories"
AuthUserFile /usr/sw/mercurial/hgusers
Require valid-user

</Directory>

Basically this file will create a alias named ‘hg’ for all our repositories. It also defines permission, authentication type and repository access to hg users.


Adding users to mercurial
User can be added to mercurial by using ‘htpasswd’ command. The ‘-c’ option will be used to create user for first time in newly setup mercurial. For adding further users, just remove ‘-c’ option and execute the command.

sudo htpasswd -c /usr/sw/mercurial/hgusers superadmin

Provide the desired password for user named ‘superadmin’. All users credential will ge stored in “hguser” file located in “/usr/sw/mercurial/”.

Running mercurial
To run mercurial web GUI, we should add following text to “etc/apache2/sites-available/default” and restart the server.

<VirtualHost *:80>
/* commmented to increase readibility */
include /usr/sw/mercurial/hg-wsgi.conf
</VirtualHost>

Now, if we enter url “http://localhost:80/hg” in browser, we should get popup asking for username and password to procced. The popup is due to line “AthType Basic” in hg-wsgi.conf file.

Local mercurial repository

Local mercurial repository

Creating repository
Creating repository can be done in few steps. First we have to make directory with project name inside path “/usr/sw/mercurial/repo/” and hg init the project directory.

Recover GRUB2 after reinstalling winxp/Vista/win7

After reinstalling Windows in the computer dual boot with both Windows and Ubuntu Linux,you need restore grub because mbr has been rewritten.This tutorial shows how to restore grub 2.

Update:Today I ghost my XP system,but after restore grub,I cannot boot into XP by clicking the old windows option in grub menu.So you’d better to run this command to renew the grub2 boot list after trying following method:

sudo update-grub

Using Ubuntu LiveCD/LiveUSB and boot-repair (gui tool).

If you have the LiveCD or LiveUSB you installed Ubuntu from, insert into your machine and boot from it. Then run 3 commands into terminal (Ctrl+Alt+T) to install boot-repair:

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install -y boot-repair

If you don’t, directly download boot-repair.iso file and burn into liveCD or liveUSB (UNetbootin) then boot from it.

Open boot-repair, click Recommended repair to repair grub2 with one click. Click Advanced options for details.

Tooltip fix in ubuntu

In unbuntu system, we often encounter that the tooltip background is black in color. In eclipse,
we see that all the documentation appear in black background which is unpleasant for eye.

Through googling I find that there are many ways to tackle this problem. But for our convenience, someone has made a shell script which automatically fix this problem.
You just have to run this script and you are done.
Just copy and paste this script in a file with extension .sh and run it…

#/bin/sh
# Tooltip fix
# A script to fix themes files in Ubuntu 11.10
#  to have readable tooltips in applications such
#  as eclipse.
# The script edits the gtk.css, settings.ini and gtkrc files
# Author: Victor Pillac
# http://victorpillac.wordpress.com

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root" 1>&2
  exit 1
fi  

path=/usr/share/themes
theme=Ambiance

if [ $# = 1 ]; then
  theme=$1
fi

echo "Fixing tooltips for theme $theme"
echo " (you can select a different theme by passing its name as argument)"
sed -i 's/tooltip_bg_color #000000/tooltip_bg_color #f5f5b5/g' $path/$theme/gtk-3.0/gtk.css
sed -i 's/tooltip_fg_color #ffffff/tooltip_fg_color #000000/g' $path/$theme/gtk-3.0/gtk.css
sed -i 's/tooltip_bg_color:#000000/tooltip_bg_color:#f5f5b5/g' $path/$theme/gtk-3.0/settings.ini
sed -i 's/tooltip_fg_color:#ffffff/tooltip_fg_color:#000000/g' $path/$theme/gtk-3.0/settings.ini
sed -i 's/tooltip_bg_color:#000000/tooltip_bg_color:#f5f5b5/g' $path/$theme/gtk-2.0/gtkrc
sed -i 's/tooltip_fg_color:#ffffff/tooltip_fg_color:#000000/g' $path/$theme/gtk-2.0/gtkrc
echo "Done"

Install deb package with unsatisfied dependencies Ubuntu

While installing software package from *.deb file, it is often that we encounter with the problem with unsatisfied dependencies. Basically, dpkg doesn’t resolves dependencies automatically. But here is a trick to fix this issue.

first install package with this command.
sudo dpkg –force-depends -i package-name.deb
sudo apt-get install -f

and you are done.

Fail to load jni library

“Failed to load JavaHL Library …” : a popup error message which, whenever I see makes me say “che”. Not really annoying but just enough to make you glare at computer screen. Earlier, whenever I had this popup message, all I did was click ok and continued. ‘Ahh.. solved the problem for this session’. How foolish, forgot the google. Cud have found atleast one solution and rest in ease for all session.

Well, I did found a one good solution (after countless ‘ok’ clicks), which if you want to know in details, then go here. The page content will explain what is ‘JavaHL’ , why do you get it in first place and how to fix the issue across different platforms. I haven’t faced this popup error on window platform but it’s common in linux (tried couple of distro, installed eclipse with subclipse plugin and got popup in each one).

To get out of this error simple set Subeclipse to use Pure Java SVN kit instead of using JavaHL that uses JNI.
For this, in eclipse IDE
Go to Windows -> Preferences
Go to Team -> SVN
Go to SVN Interface
Set Client to SVNKit (Pure Java) instead of JavaHL which requires JNI