Skip to main content

Posts

Showing posts with the label UNIX

mutt command in UNIX

To send mail from UNIX terminal we can use mutt utility. Syntax : > echo " Some text " | mutt -a <Any attachment> -s "Subject line" -- email-address Example : > echo "Wish you happy birthday" | mutt -a "gift.jpg" -S "Happy birthday" -- techmail@techinfotalks.com

SSH in unix

To access another server/machine we use ssh to authenticate and do communication. Syntax : >ssh user@server -P password This will login to another machine from our local system. Sometime we may need to fetch the content from another file in remote system , in this case we can use the below form >ssh user@server -P password "cat /home/abc/1.txt | grep <Text to search>" If we echo the above we can see the content from our system Eg : echo $(ssh user@server -P password "cat /home/abc/1.txt | grep MMDDYYYY") This will fetch the line with the date format specified from the file.

basename and dirname

basename basename is a command used to get the filename or the leaf directory name from a path given. basically it will select the last string which is prefixed by /. For example: >basename /x/y/z/abc.txt abc.txt The above command will bring output as abc.txt >basename /x/y/z/ z It will bring z as output Please note , inside a script if we use basename $0 , it will bring the name of the script. It has many uses. dirname dirname will bring the path of the directory. ie the prefix for / for a leave directory name or file. For example: >dirname /x/y/z/abc.txt /x/y/z The above command will bring output as /x/y/z >dirname /x/y/z/ /x/y It will bring /x/y as output

Alias command in UNIX

Alias is used for creating alternative names for commands which have got long text as syntax. Also for commands which are very frequently used we can create alias with short letters so , can save some time in typing and getting result. How to create alias? alias ALIAS_NAME='COMMAND' For example : alias ll='ls -ltra' So now when we give ll , the result showing will be same as ls -ltra command. Another example : alias s='df -h' This will show the disk free space in the system. How to view all alias created? If we simply type 'alias' from the command prompt it will display all the alias created in the system. Another point to note is , creating an alias will be available in environment level for the particular session only. It is similar to creating a variable. When we close the session the alias is also removed. Usually we keep the alias in .profile file in the home directory, so that every time when we open a UNIX session the alias variables are also ass

UNIX : How to get record count from zipped file

Sometimes we may need to get records count from file . For that we can use wc -l , command with file name. In some situation the file will be in compressed format . wc -l will not directly work with zipped files . In this case we can do zcat the file and pipe the word count command with it. Example : Let say we have a file cricketData.dat.gz To get word count from the file use : zcat cricketData.dat.gz | wc -l This will give the record count.

UNIX : How to ignore lines with certain names

Sometimes we need to ignore multiple lines with certain words and get the list out of the file. usually it will be a log file to read . The below grep command can be used to ignore multiple words present in a text file. Lets say the file contain $ cat list.txt apple orange apple banana papaya Now we need to ignore line with orange , banana and papaya . So we can use the below grep command. $ cat list.txt | grep -Ev "orange|banana|papaya" apple apple It will ignore lines with the words in -v part of grep.

UNIX : Find all files created on particular date

Here a simple script to display or list only the the files created on a particular date in unix. Lets say there are 3 files in the directory and we need to get only the files created on september 28, $ ls -ltr total 12 -rw-rw-r-- 1 system system  8 Sep 28 23:55 list.txt -rwxrwxrwx 1 system system 89 Sep 28 23:57 script.sh -rw-rw-r-- 1 system system 10 Oct  2 08:54 list2.txt We can use the  below one liner to get it. $ ls -l | awk ' $6 == "Sep" && $7 >= 28 && $7 <= 28 { print $9 }' | xargs ls -ltr -rw-rw-r-- 1 system system  8 Sep 28 23:55 list.txt -rwxrwxrwx 1 system system 89 Sep 28 23:57 script.sh The awk part can be modified to get range of files or even files created or modified within a particular time. We can also male use of find command , but it is little difficult to specify the date range in it.

UNIX : Using case in shell scripting

The below example shows how to use case statement in bash scripting. #!/bin/bash value=123 #We can give the expected value to this variable. case ${ value } in 2) echo "2" ;; apple) echo "apple" ;; a|b|c) echo "Input value is $val" ;; [Yy][Ee][Ss]) echo "Yes" ;; [5-9]*) echo "A value between 5 and 9 inclusive" *) echo "Some other value" ;; esac

UNIX : unpack and pack using tar

Tarball or tar is a utility in linux to unpack or pack the files. Lets say we have a file name hello.tar.gz which is packed and compressed. To unpack the file use the below command > tar xvzf hello.tar.gz This is unpack the compressed tarball file to the directory hello in current location. To pack a list of files to a compressed tarball file , we can follow the below steps. Let say we have the following list of files in Hello directory. > cd Hello > ls helloworld.py README.txt Move back to parent directory and give the below command. > cd .. > tar zcvf hello.tar.gz Hello This will create the compressed tar file in the current location

UNIX : how to write function in bash

Hi . Most of the time we need to call a certain block of code multiple time. We group them together under a function and call it from where ever we need from main body of program. Here an example is shown on how to define and call a function in bash shell scripting. #/usr/bin/bash function sub_function { echo "Statement printing from function block" } echo "Example of function" echo "Before function call" sub_function echo "After function call" Output of above code snippet > bash function.bash Example of function Before function call Statement printing from function block After function call

UNIX : Using arrays in KSH

#To create a array named "ARRAY" and set 4 values > set -A ARRAY a b c d #To display the entire list of values in array > echo ${ ARRAY[@] } #To- display only value at a specific index > echo ${ ARRAY[0] } #To get number of elements in the array > echo ${# ARRAY[@] } #To traverse through the list of elements in array > for i in ${ ARRAY[@] } do echo $i done In bash shell , the array is declared as  ARRAY=(a b c d) Rest for the access is same as korn shell.

UNIX : Awk example to strip character of word

The below program will read all the first character of a line and join it together. #!/bin/bash F= "file.txt" for i in $( cat $F ) do echo $i | awk -F "_" 'BEGIN{ st="" } { for( i=1; i<=NF; i++ ) { st=st""toupper(substr($i,1,1)) } } END{ print st }' done The program will read a file with "_"(underscore) delimited content. Then all the first character of the line will be striped and joined together. Eg: File name : Word_File_Created_by_John" Output : WFCBJ Note then small case is also turned to upper case. The below awk functions are used in this program toupper() - This will convert the string to upper case substr() - This will strip the specific character(s) from the string

AWK : Printing columns

In awk each column can be represented by $n , where n can be any number 0,1,2,3 etc.We must specify the delimited of the fields in the command. By default a space or any amount of consecutive spaces is considered as delimiter. 1. To print the column 1 and 4 of the below data in awk > cat sample.txt  Tom 1990 IT 98 Joseph 1988 Mechanical 80 Mary 1991 Electronics 95 > awk '{print $1, $4 } ' sample.txt Tom 98 Joseph 80 Mary 95 2. To print the entire column of the text  > awk 'print{$0}' sample.txt Tom 1990 IT 98 Joseph 1988 Mechanical 80 Mary 1991 Electronics 95 3. To print add a text along with column values -  In the below example we are going to add text "Name:" in front of column 1 , and "Marks:" before column 4. > awk '{print "Name:"$1," Marks:"$4}' sample.txt Name:Tom  Marks:98 Name:Joseph  Marks:80 Name:Mary  Marks:95 4. To print the number of words in each

Unix : Count words in a line

The below code will count the number of occurrence of a word in a line and print its count.Here it searching for word "hai" in a line. #!/bin/bash v_file="/home/unix/test.txt" while read line do count1=`echo $line | awk 'BEGIN {count=0}          {          for(i=1;i<=NF;i++) { if($i == "hai") {count = count + 1}}         }                  END{ print count }'` echo "$line,$count1" done < $v_file Input text hai hai hello hai how are you how are you Output text hai hai hello,2 hai how are you,1 how are you,0

UNIX : What is /dev/null ?

/dev/null is a file where you can discard the output of the command executed. This file is also called as Bit Bucket. eg : >ls -ltr > /dev/null > Here the output of command ls is never shown on terminal STDOUT. It is redirected to bit bucket. Sometimes we need not display the output as well as error message of command.In this case use the following command. > command > /dev/null 2>&1 The command output is redirected to /dev/null.Also the error output, STDERR (which is called by 2) is redirected to STDOUT (called as 1 ).That means the error will be redirected to /dev/null.