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

Friday, August 5, 2011

How to repare 'Starting database mongodb FAIL'

rm /var/lib/mongodb/mongod.lock

/etc/init.d/mongodb start
 * Starting database mongodb OK

Wednesday, August 3, 2011

Change timezone and date ubuntu

echo "Europe/Bucharest" | sudo tee /etc/timezone
dpkg-reconfigure --frontend noninteractive tzdata

date 080315582011 ->Wed Aug  3 15:58:00 EEST 2011

Wednesday, June 29, 2011

change download directory in firefox automatically


1. go to /home/user/.mozilla/firefox/
2. find the directory ending in .default. This is your profile and will be a mix of letters and numbers.
3. Edit the file "prefs.js", adding the following line to the bottom:
user_pref("browser.download.dir", "/path/to/dir/ ");

Saturday, June 11, 2011

execute sudo commands in a bash script without password

visudo -f /etc/sudoers


user   ALL=(ALL) NOPASSWD: ALL
user   ALL=(ALL) NOPASSWD: /home/user/script

Sunday, March 6, 2011

FOREX : week 28 February - 4 March 2011

AUD_USD
EUR_CHF
EUR_GBP
EUR_JPY
EUR_USD
GBP_JPY
GBP_USD
NZD_USD
USD_CAD
USD_CHF
USD_JPY

FOREX : week 21 February - 25 February 2011

AUD_USD
EUR_CHF
EUR_GBP
EUR_JPY
EUR_USD
GBP_JPY
GBP_USD
NZD_USD
USD_CAD
USD_CHF
USD_JPY

Thursday, March 3, 2011

BASH : Combine ARRAYS

#!/bin/sh

COUNTRY=( "Austria" "Finland" "USA")
CAPITAL=( "Viena" "Helsinky" "New York")

n=${#COUNTRY[@]} #num elements in array COUNTRY

for (( i=0; i<n; i++ ))
do
COMB[$i]=${COUNTRY[$i]}"["${CAPITAL[$i]}"]"
echo ${COMB[$i]}
done


ResultsAustria[Viena]
Finland[Helsinky]
USA[New York]

BASH : Array operations


${arr[*]}         # All of the items in the array
${!arr[*]}        # All of the indexes in the array
${#arr[*]}        # Number of items in the array
${#arr[0]}        # Length of item zero
#!/bin/bash

array=(one two three four [5]=five)

echo "Array size: ${#array[*]}"

echo "Array items:"
for item in ${array[*]}
do
    printf "   %s\n" $item
done

echo "Array indexes:"
for index in ${!array[*]}
do
    printf "   %d\n" $index
done

echo "Array items and indexes:"
for index in ${!array[*]}
do
    printf "%4d: %s\n" $index ${array[$index]}
done
Running it produces the following output:
Array size: 5
Array items:
   one
   two
   three
   four
   five
Array indexes:
   0
   1
   2
   3
   5
Array items and indexes:
   0: one
   1: two
   2: three
   3: four
   5: five
#!/bin/bash

array=("first item" "second item" "third" "item")

echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
    printf "   %s\n" "${array[$ix]}"
done
echo

arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done
echo

arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done
echo

arr=("${array[@]}")
echo "After @ quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
    printf "   %s\n" "${arr[$ix]}"
done
When run it outputs:
Number of items in original array: 4
   first item
   second item
   third
   item

After unquoted expansion: 6
   first
   item
   second
   item
   third
   item

After * quoted expansion: 1
   first item second item third item

After @ quoted expansion: 4
   first item
   second item
   third
   item

BASH : Pass arrays as argument

#!/bin/bash


array1=(1 2 3 4 5 6 7 8 9)
array2=(10 20 30 40 50 60 70 80 90)


printArrays()
{
    declare -a argAry1=("${!1}")
    echo "${argAry1[@]}"


    declare -a argAry2=("${!2}")
    echo "${argAry2[@]}"
}


printArrays array1[@] array2[@]

BASH : Pass array as argument

array2=(10 20 30 40 50 60 70 80 90)
function ParseArray
{
for i in ${@}
do
echo $i
done
}


ParseArray ${array2[@]} 

Monday, February 28, 2011

linux commnads

 Command Description
apropos whatis Show commands pertinent to string. See alsothreadsafe
man -t ascii | ps2pdf - > ascii.pdf make a pdf of a manual page
  which command Show full path name of command
  time command See how long a command takes
time cat Start stopwatch. Ctrl-d to stop. See also sw
dir navigation
cd - Go to previous directory
cd Go to $HOME directory
  (cd dir && command) Go to dir, execute command and return to current dir
pushd . Put current dir on stack so you can popd back to it
file searching
alias l='ls -l --color=auto' quick dir listing
ls -lrt List files by date. See alsonewest and find_mm_yyyy
ls /usr/bin | pr -T9 -W$COLUMNS Print in 9 columns to width of terminal
  find -name '*.[ch]' | xargs grep -E 'expr' Search 'expr' in this dir and below. See alsofindrepo
  find -type f -print0 | xargs -r0 grep -F 'example' Search all regular files for 'example' in this dir and below
  find -maxdepth 1 -type f | xargs grep -F 'example' Search all regular files for 'example' in this dir
  find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done Process each item with multiple commands (in while loop)
find -type f ! -perm -444 Find files not readable by all (useful for web site)
find -type d ! -perm -111 Find dirs not accessible by all (useful for web site)
locate -r 'file[^/]*\.txt' Search cached index for names. This re is like glob *file*.txt
look reference Quickly search (sorted) dictionary for prefix
grep --color reference /usr/share/dict/words Highlight occurances of regular expression in dictionary
archives and compression
  gpg -c file Encrypt file
  gpg file.gpg Decrypt file
  tar -c dir/ | bzip2 > dir.tar.bz2 Make compressed archive of dir/
  bzip2 -dc dir.tar.bz2 | tar -x Extract archive (use gzip instead of bzip2 for tar.gz files)
  tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg' Make encrypted archive of dir/ on remote machine
  find dir/ -name '*.txt' | tar -c --files-from=- | bzip2 > dir_txt.tar.bz2 Make archive of subset of dir/ and below
  find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ --parents Make copy of subset of dir/ and below
  ( tar -c /dir/to/copy ) | ( cd /where/to/ && tar -x -p ) Copy (with permissions) copy/ dir to /where/to/ dir
  ( cd /dir/to/copy && tar -c . ) | ( cd /where/to/ && tar -x -p ) Copy (with permissions) contents of copy/ dir to /where/to/
  ( tar -c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar -x -p' Copy (with permissions) copy/ dir to remote:/where/to/ dir
  dd bs=1M if=/dev/sda | gzip | ssh user@remote 'dd of=sda.gz' Backup harddisk to remote machine
rsync (Network efficient file copier: Use the --dry-run option for testing)
  rsync -P rsync://rsync.server.com/path/to/file file Only get diffs. Do multiple times for troublesome downloads
  rsync --bwlimit=1000 fromfile tofile Locally copy with rate limit. It's like nice for I/O
  rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html' Mirror web site (using compression and encryption)
  rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/ Synchronize current directory with remote one
ssh (Secure SHell)
  ssh $USER@$HOST command Run command on $HOST as $USER (default command=shell)
ssh -f -Y $USER@$HOSTNAME xeyes Run GUI command on $HOSTNAME as $USER
  scp -p -r $USER@$HOST: file dir/ Copy with permissions to $USER's home directory on $HOST
  scp -c arcfour $USER@$LANHOST: bigfile Use faster crypto for local LAN. This might saturate GigE
  ssh -g -L 8080:localhost:80 root@$HOST Forward connections to $HOSTNAME:8080 out to $HOST:80
  ssh -R 1434:imap:143 root@$HOST Forward connections from $HOST:1434 in to imap:143
  ssh-copy-id $USER@$HOST Install public key for $USER@$HOST for password-less log in
wget (multi purpose download tool)
(cd dir/ && wget -nd -pHEKk http://www.pixelbeat.org/cmdline.html) Store local browsable version of a page to the current dir
  wget -c http://www.example.com/large.file Continue downloading a partially downloaded file
  wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/dir/ Download a set of files to the current directory
  wget ftp://remote/file[1-9].iso/ FTP supports globbing directly
wget -q -O- http://www.pixelbeat.org/timeline.html | grep 'a href' | head Process output directly
  echo 'wget url' | at 01:00 Download url at 1AM to current dir
  wget --limit-rate=20k url Do a low priority download (limit to 20KB/s in this case)
  wget -nv --spider --force-html -i bookmarks.html Check links in a file
  wget --mirror http://www.example.com/ Efficiently update a local copy of a site (handy from cron)
networking (Note ifconfig, route, mii-tool, nslookup commands are obsolete)
  ethtool eth0 Show status of ethernet interface eth0
  ethtool --change eth0 autoneg off speed 100 duplex full Manually set ethernet interface speed
  iwconfig eth1 Show status of wireless interface eth1
  iwconfig eth1 rate 1Mb/s fixed Manually set wireless interface speed
iwlist scan List wireless networks in range
ip link show List network interfaces
  ip link set dev eth0 name wan Rename interface eth0 to wan
  ip link set dev eth0 up Bring interface eth0 up (or down)
ip addr show List addresses for interfaces
  ip addr add 1.2.3.4/24 brd + dev eth0 Add (or del) ip and mask (255.255.255.0)
ip route show List routing table
  ip route add default via 1.2.3.254 Set default gateway to 1.2.3.254
host pixelbeat.org Lookup DNS ip address for name or vice versa
hostname -i Lookup local ip address (equivalent to host `hostname`)
whois pixelbeat.org Lookup whois info for hostname or ip address
netstat -tupl List internet services on a system
netstat -tup List active connections to/from system
windows networking (Note samba is the package that provides all this windows specific networking support)
smbtree Find windows machines. See also findsmb
  nmblookup -A 1.2.3.4 Find the windows (netbios) name associated with ip address
  smbclient -L windows_box List shares on windows machine or samba server
  mount -t smbfs -o fmask=666,guest //windows_box/share /mnt/share Mount a windows share
  echo 'message' | smbclient -M windows_box Send popup to windows machine (off by default in XP sp2)
text manipulation (Note sed uses stdin and stdout. Newer versions support inplace editing with the -i option)
  sed 's/string1/string2/g' Replace string1 with string2
  sed 's/\(.*\)1/\12/g' Modify anystring1 to anystring2
  sed '/ *#/d; /^ *$/d' Remove comments and blank lines
  sed ':a; /\\$/N; s/\\\n//; ta' Concatenate lines with trailing \
  sed 's/[ \t]*$//' Remove trailing spaces from lines
  sed 's/\([`"$\]\)/\\\1/g' Escape shell metacharacters active within double quotes
seq 10 | sed "s/^/      /; s/ *\(.\{7,\}\)/\1/" Right align numbers
  sed -n '1000{p;q}' Print 1000th line
  sed -n '10,20p;20q' Print lines 10 to 20
  sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q' Extract title from HTML web page
  sed -i 42d ~/.ssh/known_hosts Delete a particular line
  sort -t. -k1,1n -k2,2n -k3,3n -k4,4n Sort IPV4 ip addresses
echo 'Test' | tr '[:lower:]' '[:upper:]' Case conversion
tr -dc '[:print:]' < /dev/urandom Filter non printable characters
tr -s '[:blank:]' '\t' </proc/diskstats | cut -f4 cut fields separated by blanks
history | wc -l Count lines
set operations (Note you can export LANG=C for speed. Also these assume no duplicate lines within a file)
  sort file1 file2 | uniq Union of unsorted files
  sort file1 file2 | uniq -d Intersection of unsorted files
  sort file1 file1 file2 | uniq -u Difference of unsorted files
  sort file1 file2 | uniq -u Symmetric Difference of unsorted files
  join -t'\0' -a1 -a2 file1 file2 Union of sorted files
  join -t'\0' file1 file2 Intersection of sorted files
  join -t'\0' -v2 file1 file2 Difference of sorted files
  join -t'\0' -v1 -v2 file1 file2 Symmetric Difference of sorted files
math
echo '(1 + sqrt(5))/2' | bc -l Quick math (Calculate φ). See also bc
echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bc More complex (int) e.g. This shows max FastE packet rate
echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | python Python handles scientific notation
echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot -persist Plot FastE packet rate vs packet size
echo 'obase=16; ibase=10; 64206' | bc Base conversion (decimal to hexadecimal)
echo $((0x2dec)) Base conversion (hex to dec) ((shell arithmetic expansion))
units -t '100m/9.58s' 'miles/hour' Unit conversion (metric to imperial)
units -t '500GB' 'GiB' Unit conversion (SI to IECprefixes)
units -t '1 googol' Definition lookup
seq 100 | (tr '\n' +; echo 0) | bc Add a column of numbers. See also add and funcpy
calendar
cal -3 Display a calendar
cal 9 1752 Display a calendar for a particular month year
date -d fri What date is it this friday. See also day
[ $(date -d "tomorrow" +%d) = "01" ] || exit exit a script unless it's the last day of the month
date --date='25 Dec' +%A What day does xmas fall on, this year
date --date='@2147483647' Convert seconds since the epoch (1970-01-01 UTC) to date
TZ='America/Los_Angeles' date What time is it on west coast of US (use tzselect to find TZ)
date --date='TZ="America/Los_Angeles" 09:00 next Fri' What's the local time for 9AM next Friday on west coast US
locales
printf "%'d\n" 1234 Print number with thousands grouping appropriate to locale
BLOCK_SIZE=\'1 ls -l Use locale thousands grouping in ls. See also l
echo "I live in `locale territory`" Extract info from locale database
LANG=en_IE.utf8 locale int_prefix Lookup locale info for specific country. See alsoccodes
locale -kc $(locale | sed -n 's/\(LC_.\{4,\}\)=.*/\1/p') | less List fields available in locale database
recode (Obsoletes iconv, dos2unix, unix2dos)
recode -l | less Show available conversions (aliases on each line)
  recode windows-1252.. file_to_change.txt Windows "ansi" to local charset (auto does CRLF conversion)
  recode utf-8/CRLF.. file_to_change.txt Windows utf8 to local charset
  recode iso-8859-15..utf8 file_to_change.txt Latin9 (western europe) to utf8
  recode ../b64 < file.txt > file.b64 Base64 encode
  recode /qp.. < file.qp > file.txt Quoted printable decode
  recode ..HTML < file.txt > file.html Text to HTML
recode -lf windows-1252 | grep euro Lookup table of characters
echo -n 0x80 | recode latin-9/x1..dump Show what a code represents in latin-9 charmap
echo -n 0x20AC | recode ucs-2/x2..latin-9/x Show latin-9 encoding
echo -n 0x20AC | recode ucs-2/x2..utf-8/x Show utf-8 encoding
CDs
  gzip < /dev/cdrom > cdrom.iso.gz Save copy of data cdrom
  mkisofs -V LABEL -r dir | gzip > cdrom.iso.gz Create cdrom image from contents of dir
  mount -o loop cdrom.iso /mnt/dir Mount the cdrom image at /mnt/dir (read only)
  cdrecord -v dev=/dev/cdrom blank=fast Clear a CDRW
  gzip -dc cdrom.iso.gz | cdrecord -v dev=/dev/cdrom - Burn cdrom image (use dev=ATAPI -scanbus to confirm dev)
  cdparanoia -B Rip audio tracks from CD to wav files in current dir
  cdrecord -v dev=/dev/cdrom -audio -pad *.wav Make audio CD from all wavs in current dir (see also cdrdao)
  oggenc --tracknum='track' track.cdda.wav -o 'track.ogg' Make ogg file from wav file
disk space (See also FSlint)
ls -lSr Show files by size, biggest last
du -s * | sort -k1,1rn | head Show top disk users in current dir. See also dutop
du -hs /home/* | sort -k1,1h Sort paths by easy to interpret disk usage
df -h Show free space on mounted filesystems
df -i Show free inodes on mounted filesystems
fdisk -l Show disks partitions sizes and types (run as root)
rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n List all packages by installed size (Bytes) on rpm distros
dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort -k1,1n List all packages by installed size (KBytes) on deb distros
dd bs=1 seek=2TB if=/dev/null of=ext3.test Create a large test file (taking no space). See also truncate
> file truncate data of file or create an empty file
monitoring/debugging
tail -f /var/log/messages Monitor messages in a log file
strace -c ls >/dev/null Summarise/profile system calls made by command
strace -f -e open ls >/dev/null List system calls made by command
strace -f -e trace=write -e write=1,2 ls >/dev/null Monitor what's written to stdout and stderr
ltrace -f -e getenv ls >/dev/null List library calls made by command
lsof -p $$ List paths that process id has open
lsof ~ List processes that have specified path open
tcpdump not port 22 Show network traffic except ssh. See alsotcpdump_not_me
ps -e -o pid,args --forest List processes in a hierarchy
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d' List processes by % cpu usage
ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS List processes by mem (KB) usage. See alsops_mem.py
ps -C firefox-bin -L -o pid,tid,pcpu,state List all threads for a particular process
ps -p 1,$$ -o etime= List elapsed wall time for particular process IDs
last reboot Show system reboot history
free -m Show amount of (remaining) RAM (-m displays in MB)
watch -n.1 'cat /proc/interrupts' Watch changeable data continuously
udevadm monitor Monitor udev events to help configure rules
system information (see also sysinfo) ('#' means root access is required)
uname -a Show kernel version and system architecture
head -n1 /etc/issue Show name and version of distribution
cat /proc/partitions Show all partitions registered on the system
grep MemTotal /proc/meminfo Show RAM total seen by the system
grep "model name" /proc/cpuinfo Show CPU(s) info
lspci -tv Show PCI info
lsusb -tv Show USB info
mount | column -t List mounted filesystems on the system (and align output)
grep -F capacity: /proc/acpi/battery/BAT0/info Show state of cells in laptop battery
# dmidecode -q | less Display SMBIOS/DMI information
# smartctl -A /dev/sda | grep Power_On_Hours How long has this disk (system) been powered on in total
# hdparm -i /dev/sda Show info about disk sda
# hdparm -tT /dev/sda Do a read speed test on disk sda
# badblocks -s /dev/sda Test for unreadable blocks on disk sda
interactive (see also linux keyboard shortcuts)
readline Line editor used by bash, python, bc, gnuplot, ...
screen Virtual terminals with detach capability, ...
mc Powerful file manager that can browse rpm, tar, ftp, ssh, ...
gnuplot Interactive/scriptable graphing
links Web browser
xdg-open . open a file or url with the registered desktop application