Untitled Document

Chapter - 3 Running Basic Commands

In our previous chapter we have seen the commands, their usage and how shell execute the commands with the arguments provided by us . Now lets try to run some General utility commands and see how unix display the results for them .

cal : Calendar command -

Run cal command on your terminal to dispaly the calendar of any specific month or for the entire year . cal command can be run with and without argument , if run without argument it will display calendar for the current month .

cal

you can even display the calendar for entire year using cal 2014 command will dispaly calendar for entire year on your terminal.

cal 2014

If you want to dispaly the calendar for the month of August 2014 then you need to run command .

cal 08 2014

date : Displaying System Date -

The command is used to print the date & time to the nearest seconds.. Type date command - you will get the current system date and time .

date

we can even format our date results , each format is preceded by + symbol , followed by % operator. We can print only the month using the format +%m

date +%m

or month name as

date +%h

we can combine the above option to display the date in different format e.g to print date dd-mm-yy run date command as -

date +"%d-%m-%y"

For more options see man page of date command and we can format and display the date as per our choice . Here are few format specifier that are useful ones -

  • d - The day of the month (1 to 31)
  • y - The last two digits of the year .
  • H,M and S - The hour,minute and second , resp .
  • D - the date in the format mm/dd/yy
  • T - The time in the format hh:mm:ss
When you use multiple format specifiers (as shown in the previous example) , you must enclose them within quotes (single or double) and use a single symbol before it .

Displaying Message -

in our previous chapter we have seen the echo statement to print the message / text on sreen . we have one more output statement printf - which we have seen in c

bc: Calculator Command -

Unix provide a calculator which can perform arithematic opertaions by invoking bc command . This is text based calculator . There is one more - a graphical object calculator xcalc command which is available in X window system and quite easy to use . In our current text we will discuss about bc .

Type bc command on your terminal , the cursor keeps on blinking and the terminal waits for the input to be keyed in . bc command belongs to the family of (filters) that expect input from the keyboard when used without argument .

e.g you want to do the sum of two numbers 8+4 then run the command as -

  bc  [ hit enter]
  8+4 [hit enetr] 
  12  [ value displayed after computation]  
  [ctrl-d]

Remember [ctrl-d] act as the eof character . We can even run mutiple calculation on single line seperated by ; however the computation results is shown in a seperate line .

  bc 5*4 ; 10-2 [ hit enter]
  20 
  [ctrl-d]

bc does not display the results for floatng values like in / operator so to get floating values - we need to use scale , to set the results to be displayed upto two decimal places use scale=2. We can raise the scale limit upto the value of the deciaml digit we want.

  bc 15/2 [ hit enter]
  7.50 
  [ctrl-d]

script : Recording Your Session -

This command is unknown to most of the UNIX and Linux users . The command "Records" your login session in a file .Suppose you are doing some important task and you want to log all the activities taht you are running on the terminal then in such a case invoke script command .It will store and keep log of all the key strokes that you have entered on your terminal and it keeps recording until you exit from your terminal.

Run script command as soon as you login to your terminal.

script

the command will start writing into file typescript. you can view this file by using cat command . If the file is already present than the script command overwrites the already existing filescript , so make sure you should rename your alreday exixting file . To terminate the script command type [ctrl-d].

You can append your existing file by executing script command as -

script  -a

If you explicitly want to store your script command o/p into some file then you can supply that filename to script command , it will start recording in the specified file .The below command will start recording in the file log.txt

script log.txt

Sending an email -

UNIX system is a multi user system , so communication through system is natural and necessary .We know that email is a very cheap and effective media for communication in order to send files and multimedia files over network .

For sending an email user can invoke mailx command . This is how linux and unix sends an email , run the mailx command .

  mailx wasudeo ( username of recipient) [enter]
  Subject: Test Mail [enter] 
  Hello Wasudeo, This is just a Test mail
  Thanks & Regards
  Mangesh pande
  [ctrl-d]

it will prompt you for username or mail id abc@gmail.com then hit enter it will ask you for subject agian press enter and type your message body to come out from mailx prompt pres [ctrl-d] this will act as end of command . The message will be stored in users mail folder /var/mail/ (user_name)

You can even send mail non interactively e.g some times we need to send error logs of execution at that time we can directly give input as .txt or any file to message body and send to the receiver

mailx -s " error logs of execution" -c mangesh@gmail.com wasudeo < errorlog.txt

here s- subject of message -c is copy contacts and wasudeo is the person to whom we are sending message .This way it helps us to reduce the message body and we directly put the details in the mail . If we want to send the mail to multiple users just put comma (,) in -c block .

Now the intresting part is , if we put or use such logic in our shell script then the mail and its details are send by the script directly , without any human intervention ( no GUI, no mouse clicks) . This is where power of UNIX and LINUX command lies .

passwd : Changing Password -

user can change his/her password by passwd command the command will ask for current password then enter new password and confirm password .If everything goes smoothly , the new password is registered by the system .

who : Know Your User -

Unix / Linux System maintains an account of all the users who are logged in to the system . Its always good to know their details and id's for any communication . who command displays the information for the users with following listings :

who

The First column displays the usernames ( or user -id) currently logged in to the system . The Second column shows the device names of their terminals .The Third, Fourth and Fifth Column shows the date and time of the logging in . The Last Column shows the machine name from where the user logged in .

The above output of who is bit cluttered to make it in proper format with headers run the who command with header otion (-H) and when combined with u gives some more details

who -Hu

One of the users shown in the first column is obviously the user who invoked the who command . To know that specifically , use the arguments am and i with who :

who am i

uname : Know Your Machine characteristics -

The uname commad will displays certain features of your operating system . By default it simply displays the name of the operating system

uname

Unix and LINUX operating systems comes in many flavours and different version , to know your current system version run -

uname -r
The first two numbers displayed by the command are nothing but the kernel versions

uname -n commnad will display the the host name on which your machine is running if your machine is connected to network same results can be get by hostname command

uname -n
Untitled Document Scroll To Top Untitled Document