Notes: Unix Lab 08

  1. The sort command
  2. The grep command
  3. The alias command
  4. Command Redirection
  5. Command Pipes

  1. The sort command

    sort simply sorts the contents of a file or stream and displays the sorted data to the terminal. There are several options to sort which will modify the way which data is sorted by the command.

    The basic syntax is shown below:
    >> sort filename.txt

    The "-n" option causes the command to sort the data as numbers instead of alphabetically.
    >> sort -n filename.txt

    The "-r" option causes the command to reverse the order of the sorted data.
    >> sort -r filename.txt

  2. The grep command

    grep is used to filter data from a file and then display the resulting data to the terminal.

    The basic syntax is shown below:
    >> grep string filename.txt

    In the example above grep will search the file "filename.txt" and return any lines which have the text "string" in them.

    The "-v" option inverts the search:
    >>grep -v string filename.txt

    In this example grep will search the file "filename.txt" and return any lines which do NOT have the text "string" in them.

    Normally grep is case sensitive. This means that a search for the text "string" will not match "STRING" nor will it match "StRiNG". The "-i" option causes grep to ignore any case differences in its search.
    >> grep -i string filename.txt

  3. The alias command

    alias creates or changes an 'alias' for a command; it is typically used to create a shortcut to a more complex command, often with the same name as the aliased command. For example, rather than enter the command ls -F one could create an alias command (also called ls in this example) that includes the option -F . In that case, entering the command ls woudl actually invoke the command ls -F.

    The basic syntax for the bash shell is shown below:
    >> alias myname='name -flags'
    In the C shell, do not use the '=' sign, just a space.

    In the example above myname will invoke the command name with the option -flags. The apostrophes are used if there is a whitespace in the aliased command.

    The alias command with no arguments lists all aliases that are set:
    >>alias
    alias l.='ls -d .* --color=tty'
    alias ll='ls -l --color=tty'
    alias ls='ls --color=tty'
    alias myname='name -flags'
    alias vi='vim'

    The unalias command will remove the alias:
    >> unalias myname

  4. Redirection

    • Basic redirection: Normally the output from unix commands will be directed to the terminal so the user of the system can read the contents. Unix shells have the capability of allowing that output, known as "standard out", to a file or another command. Basic redirection uses the > symbol to redirect output to the specified file.
       
        
      >>
      >> echo Something > file.out
      >> cat file.out
      Something
      >> 
      
      Normally the echo command would cause the terminal to display the text "Something". With the redirection the output is not sent to the terminal. Instead, the text is sent to the file "file.out". If the file does not exist, it will be created. If it did exist, its contents would be deleted and replaced with the redirected text. Another example using an absolute path reference for the output.
       
        
      >>
      >> df > /mnt/homes/tuckerm/df.out
      >> cat ~/df.out
      Filesystem           1k-blocks      Used Available Use% Mounted on
      /dev/hda3              4806572   4231116    331268  93% /
      /dev/hda6              8277240   6879728    977014  88% /software
      /dev/hdb1            118160964  37351852  74806428  34% /data
      /dev/hda2              4883604   3502264   1381340  72% /mnt/other
      /dev/hda1             19606554     16422  18594104   1% /data/misc
      omega:/var/data/ldm  172297760 107148368  51365568  68% /mnt/data
      omega:/var/data/arch  48082320  43248592    987136  98% /mnt/arch
      omega:/var/data/metadmin
                           172297760 107148368  51365568  68% /mnt/metadmin
      omega:/export/homes   20644848   9267136   9726128  49% /mnt/homes
      /data/temp/slackware-9.0-install.iso
                              678688    678688         0 100% /mnt/loop
      //alpha/mht06220      84371456  76660736   7710720  91% /mnt/z-drives/mark
      
      >>
      

    • With the double greater-than symbol (>>) the redirection still occurs but will append any text to the file it is redirected.
       
        
      >>
      >> who -H >> who.out
      >> cat who.out
      USER     LINE     LOGIN-TIME   FROM
      mark     :0       Feb  9 12:30
      tuckerm  pts/5    Feb 11 18:50
      >>
      
      The text output from the who command will be added on to the end of the existing file "file.out". If the file did not exist, it would be created by the redirection.

    • Redirection can be used to combine the contents of multiple files into one using the cat utility.
       
        
      >>
      >> cat df.out who.out > new.out
      >> cat new.out
      Filesystem           1k-blocks      Used Available Use% Mounted on
      /dev/hda3              4806572   4231116    331268  93% /
      /dev/hda6              8277240   6879728    977014  88% /software
      /dev/hdb1            118160964  37351852  74806428  34% /data
      /dev/hda2              4883604   3502264   1381340  72% /mnt/other
      /dev/hda1             19606554     16422  18594104   1% /data/misc
      omega:/var/data/ldm  172297760 107148368  51365568  68% /mnt/data
      omega:/var/data/arch  48082320  43248592    987136  98% /mnt/arch
      omega:/var/data/metadmin
                           172297760 107148368  51365568  68% /mnt/metadmin
      omega:/export/homes   20644848   9267136   9726128  49% /mnt/homes
      /data/temp/slackware-9.0-install.iso
                              678688    678688         0 100% /mnt/loop
      //alpha/mht06220      84371456  76660736   7710720  91% /mnt/z-drives/mark
      USER     LINE     LOGIN-TIME   FROM
      mark     :0       Feb  9 12:30
      tuckerm  pts/5    Feb 11 18:50
      >>
      
      Note that the newly created file contains the contents of both files in the order that they were entered on the command line.

    • Redirected input:
      Unix commands can take their input from a redirection as well. Normally, the shell provides the input, known as "stardard input", or simply as "stdin". In the case of redirected input, the file specified after the "<" symbol is opened and its contents are streamed to the specified command.
       
        
      >>
      >> cat < new.out
      Filesystem           1k-blocks      Used Available Use% Mounted on
      /dev/hda3              4806572   4231116    331268  93% /
      /dev/hda6              8277240   6879728    977014  88% /software
      /dev/hdb1            118160964  37351852  74806428  34% /data
      /dev/hda2              4883604   3502264   1381340  72% /mnt/other
      /dev/hda1             19606554     16422  18594104   1% /data/misc
      omega:/var/data/ldm  172297760 107148368  51365568  68% /mnt/data
      omega:/var/data/arch  48082320  43248592    987136  98% /mnt/arch
      omega:/var/data/metadmin
                           172297760 107148368  51365568  68% /mnt/metadmin
      omega:/export/homes   20644848   9267136   9726128  49% /mnt/homes
      /data/temp/slackware-9.0-install.iso
                              678688    678688         0 100% /mnt/loop
      //alpha/mht06220      84371456  76660736   7710720  91% /mnt/z-drives/mark
      USER     LINE     LOGIN-TIME   FROM
      mark     :0       Feb  9 12:30
      tuckerm  pts/5    Feb 11 18:50
      >>
      
      This command will stream the contents of the file "new.out" to the cat command. cat then does its operations to display the contents of the redirected data to the screen.

    • Combining redirected input and output:
       
        
      >>
      >> cat < new.out > new2.out
      >>
      

  5. Command Pipes

    Pipes are similar to redirection in that they capture the standard output that would normally go to the terminal but they direct this output to other programs instead of to files. The pipe symbol "|" is used to signify that the output should be sent to the following command.

     
      
    >>     
    >> ls -l |less
    >>
    
    This will capture the output from the ls command and will feed it to the less command. less will treat this data as if it were read from a file.

    Example:
     
      
    >>
    >> last | grep tuckerm
    >>
    
    This command line combination will capture the output from the last command and will only display out any lines which contain the text "tuckerm"

    Another example:
     
      
    >>
    >> last | grep tuckerm | head -5
    >>
    
    This command line combination will capture the output from the last command and will only display out any lines which contain the text "tuckerm". Piping the data into head with the "-5" option will cause only the first five lines of the output to be displayed.

    Combining pipes and redirection:
     
      
    >>
    >> last | grep tuckerm | head -5 > logins.list
    >>
    
    This command line combination will capture the output from the last command and will only display out any lines which contain the text "tuckerm". Piping the data into head with the "-5" option will limit the output to only the first five lines. The final output in this command will not go to the screen but will be redirected to the file names "logins.list".