Friday, 29 May 2015

UNIX Commands

Unix Command is a program written to perform certain specific action. All such programs have a name. For example, The program that is used to print today's date in a specific manner has the name date and the program that is used create a small file or display the contents of file has the name cat and so on.
  1. All unix commands are written using lower case letters . For example, cat ,ls,who,date ,and so on.
  2. Almost all the unix commands are cryptic . For example, cat stands for concatenation, ls stands for listing and so on .
  3. Unix commands can have zero or more number of arguments associated with them
  4. Unix commands can also have format specifiers as well as option associated with them . Format specifiers, whenever present, are indicated by the + character. Options associated with format specifiers, whenever present are indicated by hyphen(-).There could be many number of associated with a command.
  5. A current Unix command can be killed by using either delete or ctrl-u command.

Types of Unix Commands

Basically there are two types of Unix commands. they are External command and Internal command
  1. External Commands A command with an independent existence in the form of a separate file is called an external command. For example, programs for the commands such as cat and ls, exist independently in a directory called the /bin directory. When such commands are given, the shell reaches these command files with the help of system variable called PATH variable and executes them. Most of the Unix commands are external commands
  2. Internal Commands A command that does not have an independent existence is called an internal command. Actually the programs for internal commands will be a part of another program. For example, the echo command is an internal command as its program will be a part of the shell's program, sh. In other words echo command is built into the shell.

Some Basic Commands

Unix has several hundreds of commands within it. Most of them are simple and are powerful. A few such commands are introduced in this site.

The echo Command

the echo command is used to display message .it takes zero , one or more number of arguments. Arguments may be given either as a series of individual symbols or as a string within a pair of double quotes (" ").

Example


  1. $ echo
                        //A blank line displayed
    $
  2. $ echo welcome to unix      shell programming
    welcome to unix programming
    $
  3. $ echo "welcome to unix     shell programming"
    welcome to unix     shell programming
    $
  4. $ echo "The home directory is " $HOME
    The home directory is /home/xyz
    $
In first example the echo command without an argument prints a blank line. In second example, even if there extra space between the arguments, They are adjusted and output is printed in a standard form, With only one blank space between the different arguments. However when the message is given in the form of a string arguments, it is printed as it is ,as you can see in example three. And in last example the message is printed along with the output of HOME command.

The printf Command

The printf command is an alternative to echo, but unlike echo, it doesn't automatically insert a newline unless the \n is used explicitly.

Example


  1. $ printf "my current shell is \n $SHELL \n"
    my current shell is
    / bin / bash
    $

The tput Command

This command is used to control the movement of the cursor on the screen as well as to add certain features like blinking, boldface and underlining to the displayed messages on the screen.

Example

Description

tput clear
The tput command along with the clear arguments clears the screen and puts the cursor at the left top of the screen.
tput cupThe tput command along with cup argument and certain co-ordinate values is used to position the cursor at any required position on the screen.
tput linesThe $tput command along with lines argument is used to know the number of rows present on the current terminal.
tput colsThe $tput command along with cols argument is used to know the number of columns present on the current terminal.

the tty Command

This command is used to know the terminal on which the user is working .In Unix, every terminal is associated with a special file, called the device file. All the device files will be present in the /dev directory. A user can know the name of device file on which he is working by using tty command.

Example


  1. $ tty
    / dev / tty01
    $
  2. $ tty
    / dev / pts / 1             //This is the output from Linux
    $
Some time you may like to change the settings of your terminal. The stty Command is used to both display and change settings of the terminal.

Example

Description

stty -a
The -a(all) option displays the current settings.
stty echoe
Whether backspacing should erase the character is decided by the keyword echoe. Since this command set here, backspacing removes the character from display. You can use the same keyword to reverse this setting. Here you need to prefix a - to the echoe keyword(stty -echoe).
stty -echoWith this setting ,keyboard entry is not echoed. You should turn it off after the entry is completed by usingstty echo.
stty intr \^cstty also sets the functions for some of the keys. For instance, if you like to use [ Ctrl-C ] as the interrupt key instead of [Delete], you'll have to use this command
stty eof \^aYou used [Ctrl-d] to terminate the terminal or to terminate input. This is eof character is also selectable. Instead of [Ctrl-d], you can use [Ctrl-a] as eof character.

The who Command

Unix maintains an account of all users who are logged on to the system. The user can know login details of all the current users by using the who command provides a list of all the current users.

Example


  1. $ who
    mgv      pts / 0      2014-07-29 23:09 (:0)             //Login details of all current users
    dvg      pts / 1      2014-07-29 22:04 (:1)
    $
  2. $ who am i
    mgv      pts / 0      2014-07-29 23:09 (:0)             //Self-login details
    $
  3. $ who -Hu
    NAME      LINE     TIME                              IDLE     PID     COMMENTS             //Login details of all users with proper headers for the columns
    mgv      pts / 0     2014-07-30 17:19          0:48     1475     (:0)
    dvm      pts / 1     2014-07-30 17:25          0:33     2172     (:1)
    $
From first example, The who command displays the login details of all the current users. From second example, The who am i command is used to display self-login details. As seen from the last example -H option provides headers for the columns and the -u option provides more details like idle time, PID and comments.

the uname Command

When this command is used, it gives the name of Unix variants. Certain option like r,v,m and a, can be used with this command.

Example


  1. $ uname
    Linux
    $
  2. $ uname -r
    3.13.0-24-generic             //release details
    $
  3. $ uname -m
    i686                                   //machine details
    $
Some other options like -v gives the version of the system being used. The use of the -a option gives all the details of the system. The -n option display either the host name or the complete domain name.

The date Command

This command is used to display the current date along with the time. This is one of the very few commands that allow the use of format specifiers as arguments. Format specifiers are single characters using which, one can print the date in a specific manner. Each format specifier is preceded by a + symbol followed by the % operator.

Example


  1. $ date
    Wed Jul 30 19:51:17 IST 2014
    $
  2. $ date +%m             //format specifier m displays current month in the numeric form.
    07
    $
  3. $ date +%h             //format specifier h displays the name of current month
    Jul
    $
  4. $ date "Today's date is +%d"             //format specifier d display the day of current month in dd format
    Today's date is 30
    $
  5. $ date +%D             //format specifier D display the day of current month in mm/dd/yy format
    07/30/2014
    $
  6. $ date +%Y             //format specifier Y gives all the four digits of the year.
    2014
    $
Some other format specifiers such as H, M and S stand for hour, minute and second, respectively. you can also use number of options like u, r, R, f with this command.

The cal Command

The cal command is used to print the calendar of a specific month or a specific year. when this command is used without any arguments, the calendar of the current month of the current year will be printed.

Example


  1. $ cal
                                                         July 2014
    SuMoTuWeThFrSa
        1 2 3 
     4 5 6 7 8 9 10
     11 12 13 14 15 16 17
     18 19 20 21 22 23 24
     25 26 27 28 29 30 31
           
    $
When two numeric arguments are given to cal command (for example $ cal 01 2014 or $ cal Jan 2014), the first argument will be considered as the month, the second argument will be considered as the year and the calendar for that month of that year will be printed on the screen. And when single numeric argument is given, the complete calendar for the entire year represented by the numeric argument will be printed on the screen.

The calendar Command

The calendar utility checks the current directory or the directory specified by the CALENDER_DIR environment variable for a file named calendar and displays lines that begins with either today's date or tomorrow’s.

Example


  1. $ calendar
    Aug 06       Jonathan B. Postel is born in Altadena, California, 1943
    Aug 06       Caricom in Barbados
    Aug 06       Cy Young pitches first game, 1890
    Aug 06       Bank Holiday in British Columbia, Fiji, Iceland, Ireland, Ontario
    Aug 06       Emancipation Day in Bahamas
    Aug 06       Independence Day in Bolivia
    Aug 06       John-Mark Gurney born in Detroit, Michigan, United States, 1978
    Aug 06       Soleil rouge en août, C'est de la pluie partout.
    Aug 07       Battle of Boyaca in Colombia
    Aug 07       Jonathan Mini born in San Mateo, California, United States, 1979
    Aug 07       Aujourd'hui, c'est la St(e) Gaëtan.
    Aug 07       Gründung der Sozialdemokratischen Arbeiterpartei in Eisenach unter der Führung von August Bebel und Wilhelm Liebknecht, 1869
    $
  2. $ calendar -A 00
    Aug 06       Jonathan B. Postel is born in Altadena, California, 1943
    Aug 06       Caricom in Barbados
    Aug 06       Cy Young pitches first game, 1890
    Aug 06       Bank Holiday in British Columbia, Fiji, Iceland, Ireland, Ontario
    Aug 06       Emancipation Day in Bahamas
    Aug 06       Independence Day in Bolivia
    Aug 06       John-Mark Gurney born in Detroit, Michigan, United States, 1978
    Aug 06       Soleil rouge en août, C'est de la pluie partout.
    $
The options are as follows

Option

Description

-A num
Print lines from today and next num days. As shown in example2(here num is equal to zero).
-a
Process the “calendar” files of all users and mail the results to them. This requires super user privileges.
-B num
Print lines from today and previous num days (backward, past).
-b
Enforce special date calculation mode for KOI8 calendars.
-l num
Print lines from today and next num days (forward, future). Defaults to one. (same as -A)
-w num
Print lines from today and next num days, only if today is Friday (forward, future). Defaults to two, which causes calendar to print entries through the weekend on Fridays.
-f
calendarfile Use calendarfile as the default calendar file.
-t
-t [[[cc]yy]mm]dd Act like the specified value is “today” instead of using the current date. If yy is specified, but cc is not, a value for yy Between 69 and 99 results in a cc value of 19. Otherwise, a cc value of 20 is used.

The passwd Command

User can change his or her Unix System password using this command

Example


  1. $ passwd
    ( current ) UNIX password :       //Enter your current password.
    Enter new UNIX password :       //create new password.
    Retype new UNIX password :     //Retype new password.
    password changed
    $

The bc Command

This command is used to perform all the usual arithmetic operations as well as change of bases in the range of base 2-16.

Example


  1. $ bc        // simple addition using bc command.
    n = 5 + 5
    n + 5
    15
    quit        // A session with bc is terminated using quit command.
    $
  2. $ bc
    sqrt(15)
    3
    scale = 3        // The precision is set to 3 using scale function.
    sqrt(15)
    3.872
    quit
    $
  3. $ bc
    ibase = 10
    obase = 2
    7             // The base of 7 is 10.
    111        // 7 is converted to binary(base 2).
    quit
    $

The script command

This command lets you record your terminal session in a file. You’ll find it useful to store in a file all keystrokes as well as output and error messages. You can later view this file.

Example


  1. $ script
    Script started, file is typescript
    $ echo "Now it’s recording my session"
    Now its recording my session
    $ exit
    Script done, file is typescript
    $ cat typescript
    Script started on Wednesday 06 August 2014 08 : 56 : 28 PM IST
    $ echo "Now it’s recording my session"
    Now its recording my session
    $ exit
    Script done on Wednesday 06 August 2014 08 : 56 : 29 PM IST
    $
As shown in the above example. When script command is executed and once the prompt returns all keystrokes that are entered get recorded in the file typescript. Recording is terminated using exit command. typescript can be viewed using catcommand. Script overwrites any previous typescript that may exist. If you want to append to it, or want to use a different log file, following options will help you.
script -a            //appends to the existing typescript.
script logfile        //Logs activates to logfile.

The man Command

The details pertaining to a command or a utility can be seen using man command. Thus the details of the bc command can be obtained using a command line as shown below.

Example


  1. man bc
    bc(1)                           General Command Manual                                    bc(1)
    NAME
             bc - An arbitrary precision calculator language
    SYNTAX
             bc [ -hlwsqv ] [long - option ] [ file . . . ]
    DESCRIPTION
The very first line in a page begins and ends with the name of the command. A numeral appears within a pair of parenthesis that refers to the section of the manual in which this entry is found. NAME refers to the name of the command and a one-line description of the command. Following the NAME appears a SYNOPSIS or for some command SYNTAX of the different formats that can be used with the command. The explanation of what happens when the command is used is given withinDESCRIPTION. Additional explanation, if any, will be present within NOTES.
For certain command. the output is very large and it scroll off fast and the user will be able to see only the last screen full of information. In such cases the output can be piped to the more command (for example : $ man command-name | more ) .

How to give multiple commands in a single line

Normally a single command is given to shell at its prompt. You can give multiple commands in a single command line using semicolon (;) between successive commands as shown below.

Example


  1. echo " i am giving multiple commands in a single line " ; date +%D ; who am i
    i am giving multiple commands in a single line
    08/12/14
    mgv-pc pts/1       2014-08-12 10:02 (:0)
    $


No comments:

Post a Comment