Posts

Showing posts from January, 2011

Script--Color & hide charaters

$ ls -A .bash_history .bash_logout .bash_profile .bashrc archive check-orders.sh orders.txt The filenames can be printed in color to show the kind of file they are.The colors are defined in a file /etc/DIR_COLORS.You can customize the colors using a .dir_colors file in your own directory.The format of the file is described in the /etc/DIR_COLORS file. To display the files without color and with symbols instead, use the --color and --classify (or -F) switches. (On most Linux distributions, this feature is turned on using aliases.) $ ls --color=never --classify archive/ check-orders.sh* orders.txt The –classify symbols are directories (/), programs (*), symbolic links (@), pipes (|), and Unix domain socket files (=).These symbols are not a part of the name:They are hints as to the type of file. In this example, archive is a directory and check-orders.sh is a program. Another very important switch is --hide-control-chars (or -q). Linux filenames can contain any character, even control cha...

Save Efforts while extracting

We can extract our tar archived file to our required destination with using copy and all in single shot. cd /to/where/archived/file.gz tar -xzf filename.gz -C /destnation/dir/ ; cd /destination/dir/ it will extract the file in the path given like /destination/dir/ and cd will change your dir to that dir. e.g cd Downloads tar -xzf zabbix-2.6.gz -C /opt/ ; cd /opt/zabbix-2.6/ pwd /opt/zabbix-2.6

Command chaining Or multiple commands at same time

#pwd;echo "Hello World" [davsingh@noisecover-dr Scripts]$ ls -l && pwd ---> If first run then second will execute otherwise it will not work total 16 -rw-r--r-- 1 davsingh users 0 Jan 21 12:52 a -rw-r--r-- 1 davsingh users 0 Jan 20 14:18 ABC.txt.txt -rw-r--r-- 1 davsingh users 0 Jan 21 12:52 b -rw-r--r-- 1 davsingh users 0 Jan 21 12:52 c -rw-r--r-- 1 davsingh users 0 Jan 20 11:29 junk1.data.txt.txt -r--r--r-- 1 davsingh users 5956 Jan 20 11:29 passwd.txt.txt drwxr-xr-x 2 davsingh users 4096 Jan 21 12:58 test -rwxr-xr-x 1 davsingh users 166 Jan 20 14:21 upper-lower.sh.txt /home/davsingh/Scripts [davsingh@noisecover-dr Scripts]$ cat abc && pwd ----> first fails so second command not work cat: abc: No such file or directory [davsingh@noisecover-dr Scripts]$ cat abc || pwd --> If first fails then second runs cat: abc: No such file or directory /home/davsingh/Scripts

Exit Status

#cd /tmp #echo $? 0 --> it means successful we have 0-255 exit codes where "0" is only for successful. [root@noisecover-dr ~]# pzw -bash: pzw: command not found [root@noisecover-dr ~]# echo $? 127 --> it is reserved for command not found. 126--> permission issue

unalias

#alias df='df -TH' [root@noisecover-dr ~]# df Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/sys-root ext3 241G 31G 198G 14% / /dev/sda1 ext3 104M 19M 80M 19% /boot tmpfs tmpfs 2.1G 0 2.1G 0% /dev/shm /home/davsingh/Desktop/rhel-server-5.5-i386-dvd.iso iso9660 3.2G 3.2G 0 100% /rhel5.5/ISO #unalias df [root@noisecover-dr ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/sys-root 234410632 29446100 192864948 14% / /dev/sda1 101086 18176 77691 19% /boot tmpfs 2022912 0 2022912 0% /dev/shm /home/davsingh/Desktop/rhel-server-5.5-i386-dvd.iso 3038672 3038672 0 100% /rhel5.5/ISO

Create alias permanently

# vi /etc/bashrc alias df='df -TH' --> paste at last for user specific setting add in /home/username/.bashrc

How to create/delete multiple files

[davsingh@noisecover-dr test]$ touch test{1,2,3} {a,b,c} [davsingh@noisecover-dr test]$ ls a b c test1 test2 test3 [davsingh@noisecover-dr test]$ rm -rf test* [davsingh@noisecover-dr test]$ rm -rf {a,b,c} [davsingh@noisecover-dr test]$ touch test{1,2,3}{a,b,c} [davsingh@noisecover-dr test]$ ls test1a test1b test1c test2a test2b test2c test3a test3b test3c

Remove the variable value in memory --unset command

#answer=ind [davsingh@noisecover-dr Scripts]$ set | grep -e "answer" -e "lastname" answer=ind lastname=singh #use the unset command [davsingh@noisecover-dr Scripts]$ unset answer [davsingh@noisecover-dr Scripts]$ set | grep -e "answer" -e "lastname" lastname=singh [davsingh@noisecover-dr Scripts]$ set | grep answer [davsingh@noisecover-dr Scripts]$

GREP command to search multiple strings

[davsingh@noisecover-dr Scripts]$ set | grep -e "answer" -e "lastname" answer=ind lastname=singh For not equal to [davsingh@noisecover-dr Scripts]$ seq 10 | grep -v -e "1" -e "5" 2 3 4 6 7 8 9

Scripts examples with read command

[davsingh@noisecover-dr Scripts]$ read test davinder singh [davsingh@noisecover-dr Scripts]$ echo $test davinder singh [davsingh@noisecover-dr Scripts]$ read firstname lastname davinder singh [davsingh@noisecover-dr Scripts]$ echo $firstname davinder [davsingh@noisecover-dr Scripts]$ echo $lastname singh [davsingh@noisecover-dr Scripts]$ echo $firstname $lastname davinder singh

Script to convert upper to lower characters

#script to change charaters from upper to lower #!/bin/bash echo $0; for i in `ls -A` do newname= echo $i|tr A-Z a-z echo $i $newname mv $i $i.txt done ~

why #!/bin/bash is required

It is called the Shebang rotation #which bash /bin/bash #sh script_name.sh ---> here we no need to mention !/bin/bash line in script but we want to give as below then we reuired the line so that it can understand it will be bash #./script_name more details check the below URL http://en.wikipedia.org/wiki/Shebang_%28Unix%29

Hello World

#!/bin/bash echo "Hello World"

Example sort, cut & uniq command

cut -f1 -d: passwd |sort|uniq it will cut the first field in passwd file & sort with according to alphabets & only show the unique names. cut -f3 -d: passwd |sort -n|uniq it will cut the 3rd field in passwd file & sort according to the numeric numbers & show the unique numbers only.

how to find the current shell

echo $0 for example [davsingh@noisecover-dr Scripts]$ ksh $ echo $0 ksh $ exit [davsingh@noisecover-dr Scripts]$ echo $0 -bash

How to mount the ISO image

mount -o loop /home/davsingh/Desktop/rhel-server-5.5-i386-dvd.iso /rhel5.5/ISO/

Split a file in to multiple files

If we have a file with 1-300 lines are there & need to create separate files for 20 lines... means need to create 15 files to separate the 20 lines in each file. mkdir test cd test split -l 20 ../server.txt bash-3.2$ ls -l total 128 -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xaa -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xab -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xac -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xad -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xae -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xaf -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xag -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xah -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xai -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xaj -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xak -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xal -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xam -rw-r--r-- 1 davsingh staff 500 Jan 10 15:21 xan -rw-r--r-- 1 davsingh staff 500 ...