Saturday, December 24, 2011

GRUB installation failed the 'grub-pc' package failed to install into /target/. without the GRUB boot loader, the installed system will not boot

On windows 7 install ubuntu 10.10 in virtual box. You will get the following error
GRUB installation failed the 'grub-pc' package failed to install into /target/.  without the GRUB boot loader, the installed system will not boot
Solution
boot ubuntu cd and select recover a broker system and then open a shell 
sudo apt-get update
sudo apt-get install grub-pc
sudo grub-install --recheck /dev/sda
sudo update-grub

Saturday, December 10, 2011

Remove blank lines using grep/sed

$ grep -v '^$' input.txt > output.txt
$ sed '/^$/d' input.txt > output.txt

Friday, December 9, 2011

Wednesday, December 7, 2011

Tuesday, December 6, 2011

Record terminal actions to a file and also see the results on terminal

bash |tee record.txt

Brace Expansion

ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car, Phone, Pull}
{Car, Phone, Pull}
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}
Car Phone Pull
ubuntu1004@ubuntu1004:/tmp/folder$ echo O{Car,Phone,Pull}O
OCarO OPhoneO OPullO
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s
Car's Phone's Pull's
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s END
Car's Phone's Pull's END
ubuntu1004@ubuntu1004:/tmp/folder$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
ubuntu1004@ubuntu1004:/tmp/folder$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
ubuntu1004@ubuntu1004:/tmp/folder$ echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
ubuntu1004@ubuntu1004:/tmp/folder$ echo {A..M}
A B C D E F G H I J K L M
ubuntu1004@ubuntu1004:/tmp/folder$ echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
ubuntu1004@ubuntu1004:/tmp/folder$ echo {{A..Z},{a..z}}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
ubuntu1004@ubuntu1004:/tmp/folder$ echo {A..Z},{a..z}
A,a A,b A,c A,d A,e A,f A,g A,h A,i A,j A,k A,l A,m A,n A,o A,p A,q A,r A,s A,t A,u A,v A,w A,x A,y A,z B,a B,b B,c B,d B,e B,f B,g B,h B,i B,j B,k B,l B,m B,n B,o B,p B,q B,r B,s B,t B,u B,v B,w B,x B,y B,z C,a C,b C,c C,d C,e C,f C,g C,h C,i C,j C,k C,l C,m C,n C,o C,p C,q C,r C,s C,t C,u C,v C,w C,x C,y C,z D,a D,b D,c D,d D,e D,f D,g D,h D,i D,j D,k D,l D,m D,n D,o D,p D,q D,r D,s D,t D,u D,v D,w D,x D,y D,z E,a E,b E,c E,d E,e E,f E,g E,h E,i E,j E,k E,l E,m E,n E,o E,p E,q E,r E,s E,t E,u E,v E,w E,x E,y E,z F,a F,b F,c F,d F,e F,f F,g F,h F,i F,j F,k F,l F,m F,n F,o F,p F,q F,r F,s F,t F,u F,v F,w F,x F,y F,z G,a G,b G,c G,d G,e G,f G,g G,h G,i G,j G,k G,l G,m G,n G,o G,p G,q G,r G,s G,t G,u G,v G,w G,x G,y G,z H,a H,b H,c H,d H,e H,f H,g H,h H,i H,j H,k H,l H,m H,n H,o H,p H,q H,r H,s H,t H,u H,v H,w H,x H,y H,z I,a I,b I,c I,d I,e I,f I,g I,h I,i I,j I,k I,l I,m I,n I,o I,p I,q I,r I,s I,t I,u I,v I,w I,x I,y I,z J,a J,b J,c J,d J,e J,f J,g J,h J,i J,j J,k J,l J,m J,n J,o J,p J,q J,r J,s J,t J,u J,v J,w J,x J,y J,z K,a K,b K,c K,d K,e K,f K,g K,h K,i K,j K,k K,l K,m K,n K,o K,p K,q K,r K,s K,t K,u K,v K,w K,x K,y K,z L,a L,b L,c L,d L,e L,f L,g L,h L,i L,j L,k L,l L,m L,n L,o L,p L,q L,r L,s L,t L,u L,v L,w L,x L,y L,z M,a M,b M,c M,d M,e M,f M,g M,h M,i M,j M,k M,l M,m M,n M,o M,p M,q M,r M,s M,t M,u M,v M,w M,x M,y M,z N,a N,b N,c N,d N,e N,f N,g N,h N,i N,j N,k N,l N,m N,n N,o N,p N,q N,r N,s N,t N,u N,v N,w N,x N,y N,z O,a O,b O,c O,d O,e O,f O,g O,h O,i O,j O,k O,l O,m O,n O,o O,p O,q O,r O,s O,t O,u O,v O,w O,x O,y O,z P,a P,b P,c P,d P,e P,f P,g P,h P,i P,j P,k P,l P,m P,n P,o P,p P,q P,r P,s P,t P,u P,v P,w P,x P,y P,z Q,a Q,b Q,c Q,d Q,e Q,f Q,g Q,h Q,i Q,j Q,k Q,l Q,m Q,n Q,o Q,p Q,q Q,r Q,s Q,t Q,u Q,v Q,w Q,x Q,y Q,z R,a R,b R,c R,d R,e R,f R,g R,h R,i R,j R,k R,l R,m R,n R,o R,p R,q R,r R,s R,t R,u R,v R,w R,x R,y R,z S,a S,b S,c S,d S,e S,f S,g S,h S,i S,j S,k S,l S,m S,n S,o S,p S,q S,r S,s S,t S,u S,v S,w S,x S,y S,z T,a T,b T,c T,d T,e T,f T,g T,h T,i T,j T,k T,l T,m T,n T,o T,p T,q T,r T,s T,t T,u T,v T,w T,x T,y T,z U,a U,b U,c U,d U,e U,f U,g U,h U,i U,j U,k U,l U,m U,n U,o U,p U,q U,r U,s U,t U,u U,v U,w U,x U,y U,z V,a V,b V,c V,d V,e V,f V,g V,h V,i V,j V,k V,l V,m V,n V,o V,p V,q V,r V,s V,t V,u V,v V,w V,x V,y V,z W,a W,b W,c W,d W,e W,f W,g W,h W,i W,j W,k W,l W,m W,n W,o W,p W,q W,r W,s W,t W,u W,v W,w W,x W,y W,z X,a X,b X,c X,d X,e X,f X,g X,h X,i X,j X,k X,l X,m X,n X,o X,p X,q X,r X,s X,t X,u X,v X,w X,x X,y X,z Y,a Y,b Y,c Y,d Y,e Y,f Y,g Y,h Y,i Y,j Y,k Y,l Y,m Y,n Y,o Y,p Y,q Y,r Y,s Y,t Y,u Y,v Y,w Y,x Y,y Y,z Z,a Z,b Z,c Z,d Z,e Z,f Z,g Z,h Z,i Z,j Z,k Z,l Z,m Z,n Z,o Z,p Z,q Z,r Z,s Z,t Z,u Z,v Z,w Z,x Z,y Z,z
ubuntu1004@ubuntu1004:/tmp/folder$ echo {z..a}
z y x w v u t s r q p o n m l k j i h g f e d c b a
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s
Car's Phone's Pull's
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s END
Car's Phone's Pull's END
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s\ END
Car's END Phone's END Pull's END
ubuntu1004@ubuntu1004:/tmp/folder$ echo {Car,Phone,Pull}\'s\ .
Car's . Phone's . Pull's .
ubuntu1004@ubuntu1004:/tmp/folder$ echo -e {Car,Phone,Pull}\'s\ END."\n"
Car's END.
 Phone's END.
 Pull's END.

ubuntu1004@ubuntu1004:/tmp/folder$ echo -e "\b"{Car,Phone,Pull}\'s\ END."\n"
Car's END.
Phone's END.
Pull's END.


ubuntu1004@ubuntu1004:/tmp/folder$ echo {0..100..10}
0 10 20 30 40 50 60 70 80 90 100
ubuntu1004@ubuntu1004:/tmp/folder$ echo {50..100..5}
50 55 60 65 70 75 80 85 90 95 100
ubuntu1004@ubuntu1004:/tmp/folder$ echo {01..10}
01 02 03 04 05 06 07 08 09 10
ubuntu1004@ubuntu1004:/tmp/folder$ echo {01..100}
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

Remove files and folders eficiently

ubuntu1004@ubuntu1004:/tmp/folder$ ll
total 16
drwxr-xr-x  4 ubuntu1004 ubuntu1004 4096 2011-12-06 22:51 ./
drwxrwxrwt 14 root       root       4096 2011-12-06 22:51 ../
-rw-r--r--  1 ubuntu1004 ubuntu1004    0 2011-12-06 22:51 file1
-rw-r--r--  1 ubuntu1004 ubuntu1004    0 2011-12-06 22:51 file2
-rw-r--r--  1 ubuntu1004 ubuntu1004    0 2011-12-06 22:51 file3
drwxr-xr-x  2 ubuntu1004 ubuntu1004 4096 2011-12-06 22:51 folder1/
drwxr-xr-x  2 ubuntu1004 ubuntu1004 4096 2011-12-06 22:51 folder2/
ubuntu1004@ubuntu1004:/tmp/folder$ find . -type f
./file2
./file1
./file3
ubuntu1004@ubuntu1004:/tmp/folder$ find . -type f | xargs
./file2 ./file1 ./file3
ubuntu1004@ubuntu1004:/tmp/folder$ find . -type f | xargs ls -lh
-rw-r--r-- 1 ubuntu1004 ubuntu1004 0 2011-12-06 22:51 ./file1
-rw-r--r-- 1 ubuntu1004 ubuntu1004 0 2011-12-06 22:51 ./file2
-rw-r--r-- 1 ubuntu1004 ubuntu1004 0 2011-12-06 22:51 ./file3
ubuntu1004@ubuntu1004:/tmp/folder$ find . -type f | xargs rm -f
ubuntu1004@ubuntu1004:/tmp/folder$ ll
total 16
drwxr-xr-x  4 ubuntu1004 ubuntu1004 4096 2011-12-06 23:13 ./
drwxrwxrwt 14 root       root       4096 2011-12-06 22:51 ../
drwxr-xr-x  2 ubuntu1004 ubuntu1004 4096 2011-12-06 22:51 folder1/
drwxr-xr-x  2 ubuntu1004 ubuntu1004 4096 2011-12-06 22:51 folder2/
ubuntu1004@ubuntu1004:/tmp/folder$ find . -type d | xargs rm -rf
rm: cannot remove directory `.'
ubuntu1004@ubuntu1004:/tmp/folder$ ll
total 8
drwxr-xr-x  2 ubuntu1004 ubuntu1004 4096 2011-12-06 23:14 ./
drwxrwxrwt 14 root       root       4096 2011-12-06 22:51 ../

Grep for multiple words

grep 'A\|B' file
A B C D
B C D E
ubuntu1004@ubuntu1004:~$ cat -n file
     1 A B C D
     2 B C D E
     3 F G H I
     4 G I R T
ubuntu1004@ubuntu1004:~$ grep -rn 'A\|B' file
1:A B C D
2:B C D E

Get current ubuntu version

ubuntu1004@ubuntu1004:~$cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.04
DISTRIB_CODENAME=natty
DISTRIB_DESCRIPTION="Ubuntu 11.04"

ubuntu1004@ubuntu1004:~$cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d\= -f2
natty
ubuntu1004@ubuntu1004:~$grep DISTRIB_CODENAME /etc/lsb-release | cut -d\= -f2
natty

Manage SVN log

ubuntu1004@ubuntu1004:~$svn log -vl100 http://svn.apache.org/repos/asf/subversion/trunk/ > svn.log ; cat svn.log | grep CHANGE | head -n 1 | awk '{print $2}'
/subversion/trunk/CHANGES
ubuntu1004@ubuntu1004:~$ svn log -vl100 http://svn.apache.org/repos/asf/subversion/trunk/ > svn.log ; cat svn.log | grep CHANGE
   M /subversion/trunk/CHANGES
* CHANGES: Fix issue number in 1.7.2 section.
   M /subversion/trunk/CHANGES
* CHANGES: Tweak wording and fix typos in 1.7.2 section.
   M /subversion/trunk/CHANGES
* CHANGES
   M /subversion/trunk/CHANGES
* CHANGES: Tweak 1.7.2 section to explain some changes better. Also fix a
   M /subversion/trunk/CHANGES
* CHANGES (1.7.2): Tweak, spell out subcommand, etc.

Netstat cmd

ubuntu1004@ubuntu1004:~$ netstat --tcp --numeric
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State    
tcp        0      0 192.168.1.136:53499     74.125.232.196:80       ESTABLISHED
tcp        0      0 192.168.1.136:46617     199.7.48.190:80         TIME_WAIT
tcp        0      0 192.168.1.136:45177     74.125.79.138:80        ESTABLISHED
ubuntu1004@ubuntu1004:~$ netstat --tcp --listening
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State    
tcp        0      0 *:ssh                   *:*                     LISTEN    
tcp        0      0 localhost:ipp           *:*                     LISTEN    
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN    
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN    
ubuntu1004@ubuntu1004:~$ netstat --tcp --programs
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
ubuntu1004@ubuntu1004:~$ netstat --route
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     *               255.255.255.0   U         0 0          0 eth0
link-local      *               255.255.0.0     U         0 0          0 eth0
default         DD-WRT          0.0.0.0         UG        0 0          0 eth0
ubuntu1004@ubuntu1004:~$ netstat --statistics
Ip:
    1819 total packets received
    0 forwarded
    0 incoming packets discarded
    1817 incoming packets delivered
    1308 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
    ICMP input histogram:
    0 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
Tcp:
    38 active connections openings
    0 passive connection openings
    6 failed connection attempts
    0 connection resets received
    0 connections established
    1513 segments received
    1216 segments send out
    2 segments retransmited
    0 bad segments received.
    41 resets sent
Udp:
    94 packets received
    0 packets to unknown port received.
    0 packet receive errors
    94 packets sent
UdpLite:
TcpExt:
    7 TCP sockets finished time wait in fast timer
    17 delayed acks sent
    1013 packet headers predicted
    127 acknowledgments not containing data payload received
    3 predicted acknowledgments
    2 congestion windows recovered without slow start after partial ack
    2 other TCP timeouts
    11 connections reset due to unexpected data
IpExt:
    InMcastPkts: 15
    OutMcastPkts: 17
    InBcastPkts: 216
    InOctets: 1911507
    OutOctets: 132874
    InMcastOctets: 2879
    OutMcastOctets: 2959
    InBcastOctets: 43434
ubuntu1004@ubuntu1004:~$ netstat --tcp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State    

column command for formatting data

ubuntu1004@ubuntu1004:~$ cat -n file.cvs
     1 Cars $5000 10
     2 Beer $5 1000
     3 Computers $500 100
ubuntu1004@ubuntu1004:~$ column -t file.cvs
Cars       $5000  10
Beer       $5     1000
Computers  $500   100
ubuntu1004@ubuntu1004:~$ cat file.cvs |column -t
Cars       $5000  10
Beer       $5     1000
Computers  $500   100
ubuntu1004@ubuntu1004:~$ pico file.cvs
ubuntu1004@ubuntu1004:~$ cat -n file.cvs
     1 Old Cars $5000 10
     2 Beer $5 1000
     3 Computers $500 100
ubuntu1004@ubuntu1004:~$ cat file.cvs |column -t
Old        Cars  $5000  10
Beer       $5    1000
Computers  $500  100
ubuntu1004@ubuntu1004:~$ pico !!:1
pico file.cvs
ubuntu1004@ubuntu1004:~$ cat -n !!:1
cat -n file.cvs
     1 Cars|$5000|10
     2 Beer|$5|1000
     3 Computers|$500|100
ubuntu1004@ubuntu1004:~$ column -t -s "|" file.cvs
Cars       $5000  10
Beer       $5     1000
Computers  $500   100
ubuntu1004@ubuntu1004:~$ pico !-2:2
pico file.cvs
ubuntu1004@ubuntu1004:~$ !-3
cat -n file.cvs
     1 Old Cars|$5000|10
     2 Beer|$5|1000
     3 Computers|$500|100
ubuntu1004@ubuntu1004:~$ !-3
column -t -s "|" file.cvs
Old Cars   $5000  10
Beer       $5     1000
Computers  $500   100

Grep & Cut

cat -n file.txt
     1 a11,a12,a13,a14
     2 a21,a22,a23,a24
     3 a31,a32,a22,a34
     4 a41,a42,a43,a44

ubuntu1004@ubuntu1004:~$ grep a22 file.txt
a21,a22,a23,a24
a31,a32,a22,a34
ubuntu1004@ubuntu1004:~$ grep -rn a22 file.txt
2:a21,a22,a23,a24
3:a31,a32,a22,a34
ubuntu1004@ubuntu1004:~$ grep a22 file.txt | cut -d\, -f1
a21
a31
ubuntu1004@ubuntu1004:~$ grep a22 file.txt | cut -d\, -f1,3
a21,a23
a31,a22

Monday, December 5, 2011

Using awk like grep

ubuntu1004@ubuntu1004:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              16G  3.5G   11G  25% /
none                  496M  260K  496M   1% /dev
none                  500M  256K  500M   1% /dev/shm
none                  500M   84K  500M   1% /var/run
none                  500M     0  500M   0% /var/lock
none                  500M     0  500M   0% /lib/init/rw
ubuntu1004@ubuntu1004:~$ df -h|grep sda1
/dev/sda1              16G  3.5G   11G  25% /
ubuntu1004@ubuntu1004:~$ df -h|grep sda1|awk '{print $5}'
25%
ubuntu1004@ubuntu1004:~$ df -h|awk '/sda1/'
/dev/sda1              16G  3.5G   11G  25% /
ubuntu1004@ubuntu1004:~$ df -h|awk '/sda1/ {print $5}'
25%
ubuntu1004@ubuntu1004:~$ df -h|awk '/sda1/ {print $4}'
11G
ubuntu1004@ubuntu1004:~$ df -h|awk '/sda1/ {print $4" "$5}'
11G 25%

Replace the end of a string

ubuntu1004@ubuntu1004:~$ cat -n replace_end.sh
     1 #!/bin/bash
     2 echo -n "Type a word:"
     3 read text1
     4 let len1=${#text1}
     5
     6 echo -n "Type the end to substitute:"
     7 read text2
     8
     9 let len2=${#text2}
    10
    11 difference=$len1-$len2
    12
    13 if [ $len2 -gt $len1 ]
    14 then
    15 echo "This is not possible!!!"
    16 exit
    17 else
    18 echo "${text1:0:$difference}$text2"
    19 fi
ubuntu1004@ubuntu1004:~$ ./replace_end.sh
Type a word:ubuntu1004
Type the end to substitute:0804
ubuntu0804

Sub strings

ubuntu1004@ubuntu1004:~$ mystr="ubuntu1004"
ubuntu1004@ubuntu1004:~$ echo $mystr
ubuntu1004
ubuntu1004@ubuntu1004:~$ echo ${#mystr}
10
ubuntu1004@ubuntu1004:~$ echo ${mystr:0:6}
ubuntu
ubuntu1004@ubuntu1004:~$ echo ${mystr:1:6}
buntu1
ubuntu1004@ubuntu1004:~$ echo ${mystr:1:5}
buntu
ubuntu1004@ubuntu1004:~$ echo ${mystr:6:10}
1004

Split sentences in arrays

ubuntu1004@ubuntu1004:~$ read str
my first string      
ubuntu1004@ubuntu1004:~$ echo $str
my first string
ubuntu1004@ubuntu1004:~$ read -a str
This is an array
ubuntu1004@ubuntu1004:~$ echo ${str[0]}
This
ubuntu1004@ubuntu1004:~$ echo ${str[2]}
an
ubuntu1004@ubuntu1004:~$ echo ${str[@]}
This is an array
ubuntu1004@ubuntu1004:~$ echo ${#str[@]}
4
ubuntu1004@ubuntu1004:~$ read -a str <<<'This is another string'
ubuntu1004@ubuntu1004:~$ echo ${#str[@]}
4
ubuntu1004@ubuntu1004:~$ echo ${str[@]}
This is another string
ubuntu1004@ubuntu1004:~$ echo ${str[2]}
another

print text files word by word

ubuntu1004@ubuntu1004:~$ cat file.txt
BEGIN
This is first line.
This is the second line.
This a third line.
END
ubuntu1004@ubuntu1004:~$ read -a file <<<$(cat file.txt)
ubuntu1004@ubuntu1004:~$ echo ${file[@]}
BEGIN This is first line. This is the second line. This a third line. END
ubuntu1004@ubuntu1004:~$ echo ${#file[@]}
15
ubuntu1004@ubuntu1004:~$ echo -n ${file[@]}
BEGIN This is first line. This is the second line. This a third line. END
ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo "$i";done
BEGIN
This
is
first
line.
This
is
the
second
line.
This
a
third
line.
END
ubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo -n "$i";done
BEGINThisisfirstline.Thisisthesecondline.Thisathirdline.ENDubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo -n "$i";done
ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo -n "$i ";done
BEGIN This is first line. This is the second line. This a third line. END ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo -n "$i ";sleep .1;done
BEGIN This is first line. This is the second line. This a third line. END ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$
ubuntu1004@ubuntu1004:~$ for i in ${file[@]};do echo -n "$i ";sleep .05;done
BEGIN This is first line. This is the second line. This a third line. END ubuntu1004@ubuntu1004:~$

String length

ubuntu1004@ubuntu1004:~$ str=abcdef
ubuntu1004@ubuntu1004:~$ echo ${#str}
6
ubuntu1004@ubuntu1004:~$ echo `expr length $str`
6
ubuntu1004@ubuntu1004:~$ echo `expr "$str" : '.*'`
6

Remove a folder except some files

mkdir folder1
touch file1, file2, file3, file4
rm -rf -- !(@(file1|file2)*)

VBOX check the state of a VM

text=`vboxmanage showvminfo MACHINE_NAME | grep State: | awk '{print $2}'` ; if [ $text == "running" ] ; then echo OK ; else echo "NOTOK" ; fi

Saturday, December 3, 2011

“cd -” to toggle between the last two directories

ubuntu1004@ubuntu1004:~$ cd /tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ cd /tmp/tmp10/tmp20/tmp30
ubuntu1004@ubuntu1004:/tmp/tmp10/tmp20/tmp30$ cd -
/tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ cd -
/tmp/tmp10/tmp20/tmp30
ubuntu1004@ubuntu1004:/tmp/tmp10/tmp20/tmp30$ cd -
/tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$

mkdir on subfolders and cd

Add in .bash_profile
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }


ubuntu1004@ubuntu1004:~$ mkdircd /tmp/tmp1/tmp2/tmp3
ubuntu1004@ubuntu1004:/tmp/tmp1/tmp2/tmp3$ pwd
/tmp/tmp1/tmp2/tmp3

CDPATH defines base directory for cd command

ubuntu1004@ubuntu1004:~$ pwd
/home/ubuntu1004
ubuntu1004@ubuntu1004:~$ cd init.d
bash: cd: init.d: No such file or directory
ubuntu1004@ubuntu1004:~$ export CDPATH=/etc
ubuntu1004@ubuntu1004:~$ cd init.d
/etc/init.d
ubuntu1004@ubuntu1004:/etc/init.d$ pwd
/etc/init.d
ubuntu1004@ubuntu1004:/etc/init.d$

- change permanent, add export CDPATH=/etc to your ~/.bash_profile
- you can add more than one directory entry in the CDPATH variable, separating them with ':' 
export CDPATH=.:~:/etc:/var