SlideShare una empresa de Scribd logo
1 de 83
Descargar para leer sin conexión
SEES: Using Linux Tools

                                    FOSSEE

                         Department of Aerospace Engineering
                                     IIT Bombay




FOSSEE (IIT Bombay)                 Using Linux Tools          1 / 83
Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)       Using Linux Tools     2 / 83
Introduction


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)         Using Linux Tools   3 / 83
Introduction




What is the Linux OS?
   Free Open Source Operating System
           Free Free as in Free Speech, not Free Beer
   Open-Source Permit modifications and redistribution of source
                 code
    Unix-inspired
    Linux Kernel + Application software
    Runs on a variety of hardware
    Also called GNU/Linux




   FOSSEE (IIT Bombay)        Using Linux Tools              4 / 83
Introduction


Why Linux?


    Free as in Free Beer
    Secure & versatile

Why Linux for Scientific Computing?
    Free as in Free Speech
    Can run for ever
    Libraries
    Parallel Computing




   FOSSEE (IIT Bombay)          Using Linux Tools   5 / 83
Getting Started


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)            Using Linux Tools   6 / 83
Getting Started


Logging in



   GNU/Linux does have a GUI
   Command Line for this module
   Hit Ctrl + Alt + F1
   Login
   logout command logs you out




  FOSSEE (IIT Bombay)           Using Linux Tools   7 / 83
Getting Started


Where am I?




   Logged in. Where are we?
   pwd command gives the present working directory

     $ pwd
     /home/user




  FOSSEE (IIT Bombay)           Using Linux Tools    8 / 83
Getting Started


What is in there?


   ls command lists contents of pwd

     $ ls
     jeeves.rst psmith.html blandings.html Music

   Can also pass directory as argument

     $ ls Music
     one.mp3 two.mp3 three.mp3

   the Unix world is case sensitive



  FOSSEE (IIT Bombay)           Using Linux Tools   9 / 83
Getting Started


New folders


   mkdir creates new directories

     $ mkdir sees
     $ ls

   Special characters need to be escaped OR quoted

     $ mkdir software engineering
     $ mkdir "software engg"

   Generally, use hyphens or underscores instead of spaces in
   names


  FOSSEE (IIT Bombay)           Using Linux Tools               10 / 83
Getting Started


Moving around
   cd command changes the pwd

     $ cd sees
     $ pwd
     /home/user/sees/

   Alternately written as cd ./sees
   Specifying path relative to pwd
   .. takes one level up the directory structure

     $ cd ..

   We could use absolute path instead of relative

     $ cd /home/user/sees/
  FOSSEE (IIT Bombay)           Using Linux Tools   11 / 83
Getting Started


New files



   touch command creates a blank file

     $ pwd
     /home/user
     $ cd sees
     $ touch first
     $ ls
     first




  FOSSEE (IIT Bombay)           Using Linux Tools   12 / 83
Getting Help


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)         Using Linux Tools   13 / 83
Getting Help


What does a command do?

   whatis gives a quick description of a command

     $ whatis touch
     touch (1) - change file timestamps

   man command gives more detailed description

     $ man touch

   Shows all tasks that the command can perform
   Hit q to quit the man page
   For more, see the man page of man

     $ man man

  FOSSEE (IIT Bombay)        Using Linux Tools     14 / 83
Getting Help


Using additional options

   -h or -help give summary of command usage

     $ ls --help

   List out all files within a directory, recursively

     $ ls -R

   Create a new directory along with parents, if required

     $ pwd
     /home/user/
     $ ls sees/
     $ mkdir -p sees/linux-tools/scripts

  FOSSEE (IIT Bombay)          Using Linux Tools            15 / 83
Getting Help


Searching for a command



   apropos searches commands based on their descriptions

     $ apropos remove

   Returns a list of all commands that contain the search term
   In this case, we are interested in rm, rmdir




  FOSSEE (IIT Bombay)        Using Linux Tools                   16 / 83
Basic File Handling


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)               Using Linux Tools   17 / 83
Basic File Handling


Removing files

   rm is used to delete files

     $ rm foo

   rm works only for files; not directories

   Additional arguments required to remove a directory
   -r stands for recursive.
   Removes directory and all of it’s content

     $ rm -r bar

   rmdir can also be used; Explore

  FOSSEE (IIT Bombay)               Using Linux Tools    18 / 83
Basic File Handling


Copying Files

   cp copies files from one location to another

     $ cp linux-tools/scripts/foo linux-tools/

   New file-name can be used at target location
   foo copied to new location with the name bar

     $ cp linux-tools/scripts/foo linux-tools/bar

   cp overwrites files, unless explicitly asked not to
   To prevent this, use the -i flag

     $ cp -i linux-tools/scripts/foo linux-tools/b
     cp: overwrite ‘bar’?

  FOSSEE (IIT Bombay)               Using Linux Tools   19 / 83
Basic File Handling


Copying Directories




   -r is required to copy a directory and all it’s content
   Copying directories is similar to copying files

     $ cd /home/user
     $ cp -ir sees course




  FOSSEE (IIT Bombay)               Using Linux Tools        20 / 83
Basic File Handling


Moving Files
   cp and rm would be one way
   mv command does the job
   Also takes -i option to prompt before overwriting

     $ cd /home/user
     # Assume we have course directory already cre
     $ mv -i sees/ course/
   No prompt! Why?

     $ ls course
   sees became a sub-directory of course
   mv command doesn’t over-write directories
   -i option is useful when moving files around
   mv to rename — move to same location with new name
  FOSSEE (IIT Bombay)               Using Linux Tools   21 / 83
Linux File Hierarchy, Permissions & Ownership


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)                           Using Linux Tools   22 / 83
Linux File Hierarchy, Permissions & Ownership


Linux File Hierarchy




   / is called the root directory
   It is the topmost level of the hierarchy
   For details man hier




  FOSSEE (IIT Bombay)                           Using Linux Tools   23 / 83
Linux File Hierarchy, Permissions & Ownership


Permissions and Access control

   In a multi-user environment, access control is vital
   Look at the output of ls -l

     drwxr-xr-x                       5 root users                  4096 Jan 21 20:07

   The first column shows the permission information
   First character specifies type of the file
   Files have -; Directories have d
   3 sets of 3 characters — for user, group and others
   r, w, x — for read, write, execute
   Either the corresponding character or - is present


  FOSSEE (IIT Bombay)                           Using Linux Tools                 24 / 83
Linux File Hierarchy, Permissions & Ownership


Changing the permissions


   Permissions can be changed by owner of the file
   chmod command is used
   -R option to recursively change for all content of a directory

   Change permissions of foo.sh from -rw-r--r-- to
   -rwxr-xr--

     $ ls -l foo.sh
     $ chmod ug+x foo.sh
     $ ls -l foo.sh



  FOSSEE (IIT Bombay)                           Using Linux Tools   25 / 83
Linux File Hierarchy, Permissions & Ownership


Symbolic modes

Reference        Class          Description
u                user           the owner of the file
g                group          users who are members of the file’s group
o                others         users who are not hte owner of the file or members of
a                all            all three of the above; is the same as ugo

Operator      Description
+             adds the specified modes to the specified classes
-             removes the specified modes from the specified classes
=             the modes specified are to be made the exact modes for the spe

       Mode         Name              Description
       r            read              read a file or list a directory’s contents
       w            write             write to a file or directory
       x            execute           execute a file or recurse a directory tree

  FOSSEE (IIT Bombay)                           Using Linux Tools                 26 / 83
Linux File Hierarchy, Permissions & Ownership


Changing Ownership of Files



   chown changes the owner and group
   By default, the user who creates file is the owner
   The default group is set as the group of the file

     $ chown alice:users wonderland.txt

   Did it work? Not every user can change ownership
   Super-user or root user alone is empowered




  FOSSEE (IIT Bombay)                           Using Linux Tools   27 / 83
Looking at files


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)            Using Linux Tools   28 / 83
Looking at files


cat


  Displays the contents of files

    $ cat foo.txt

  Concatenates the text of multiple files

    $ cat foo.txt bar.txt

  Not-convenient to view long files




 FOSSEE (IIT Bombay)           Using Linux Tools   29 / 83
Looking at files


less


  View contents of a file one screen at a time

    $ less wonderland.txt

  q: Quit
  Arrows/Page Up/Page Down/Home/End: Navigation
  ng: Jump to line number n
  /pattern: Search. Regular expressions can be used
  h: Help




 FOSSEE (IIT Bombay)           Using Linux Tools      30 / 83
Looking at files


wc



     Statistical information about the file
     the number of lines in the file
     the number of words
     the number of characters

      $ wc wonderland.txt




 FOSSEE (IIT Bombay)              Using Linux Tools   31 / 83
Looking at files


head & tail
  let you see parts of files, instead of the whole file
  head – start of a file; tail – end of a file
  show 10 lines by default

    $ head wonderland.txt

  -n option to change the number of lines

    $ head -n 1 wonderland.txt

  tail is commonly used to monitor files
  -f option to monitor the file
  Ctrl-C to interrupt

    $ tail -f /var/log/dmesg
 FOSSEE (IIT Bombay)           Using Linux Tools        32 / 83
Looking at files


cut

  Allows you to view only certain sections of lines
  Let’s take /etc/passwd as our example

    root:x:0:0:root:/root:/bin/bash

  View only user names of all the users (first column)

    $ cut -d : -f 1 /etc/passwd

  -d specifies delimiter between fields (default TAB)
  -f specifies the field number
  Multiple fields by separating field numbers with comma

    $ cut -d : -f 1,5,7 /etc/passwd

 FOSSEE (IIT Bombay)           Using Linux Tools         33 / 83
Looking at files


cut


  Allows choosing on the basis of characters or bytes
  Example below gets first 4 characters of /etc/passwd

    $ cut -c 1-4 /etc/passwd

  One of the limits of the range can be dropped
  Sensible defaults are assumed in such cases

    $ cut -c -4 /etc/passwd
    $ cut -c 10- /etc/passwd



 FOSSEE (IIT Bombay)           Using Linux Tools        34 / 83
Looking at files


paste
  Joins corresponding lines from two different files
                       students.txt                    marks.txt
                       Hussain                         89 92 85
                       Dilbert                         98 47 67
                       Anne                            67 82 76
                       Raul                            78 97 60
                       Sven                            67 68 69

    $ paste students.txt marks.txt
    $ paste -s students.txt marks.txt

  -s prints content, one below the other
  If first column of marks file had roll numbers? How do we get a
  combined file with the same output as above (i.e. without roll
  numbers). We need to use cut & paste together. But how?
 FOSSEE (IIT Bombay)               Using Linux Tools               35 / 83
The Command Shell


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)              Using Linux Tools   36 / 83
The Command Shell


Redirection and Piping


        $ cut -d " " -f 2- marks1.txt 
        > /tmp/m_tmp.txt
        $ paste -d " " students.txt m_tmp.txt
or
        $ cut -d " " -f 2- marks1.txt 
        | paste -d " " students.txt -

      The first solution used Redirection
      The second solution uses Piping




     FOSSEE (IIT Bombay)              Using Linux Tools   37 / 83
The Command Shell


Redirection


   The standard output (stdout) stream goes to the display
   Not always, what we need
   First solution, redirects output to a file
   > states that output is redirected; It is followed by location to
   redirect

     $ command > file1

   > creates a new file at specified location
   » appends to a file at specified location



  FOSSEE (IIT Bombay)              Using Linux Tools                   38 / 83
The Command Shell


Redirection . . .


   Similarly, the standard input (stdin) can be redirected

     $ command < file1

   input and the output redirection could be combined

     $ command < infile > outfile

   Standard error (stderr) is the third standard stream
   All error messages come through this stream
   stderr can also be redirected



  FOSSEE (IIT Bombay)              Using Linux Tools         39 / 83
The Command Shell


Redirection . . .

   Following example shows stderr redirection
   Error is printed out in the first case
   Error message is redirected, in the second case

     $ cut -d " " -c 2- marks1.txt 
       > /tmp/m_tmp.txt

     $ cut -d " " -f 2- marks1.txt 1> 
       /tmp/m_tmp.txt 2> /tmp/m_err.txt

   1> redirects stdout; 2> redirects stderr

     $ paste -d " " students.txt m_tmp.txt

  FOSSEE (IIT Bombay)              Using Linux Tools   40 / 83
The Command Shell


Piping


     $ cut -d " " -f 2- marks1.txt 
       | paste -d " " students.txt -

   - instead of FILE asks paste to read from stdin
   cut command is a normal command
   the | seems to be joining the two commands
   Redirects output of first command to stdin, which becomes
   input to the second command
   This is called piping; | is called a pipe




  FOSSEE (IIT Bombay)              Using Linux Tools      41 / 83
The Command Shell


Piping



   Roughly same as – 2 redirects and a temporary file

     $ command1 > tempfile
     $ command2 < tempfile
     $ rm tempfile

   Any number of commands can be piped together




  FOSSEE (IIT Bombay)              Using Linux Tools   42 / 83
The Command Shell    Features of the Shell


Tab-completion


   Hit tab to complete an incompletely typed word
   Tab twice to list all possibilities when ambiguous completion
   Bash provides tab completion for the following.
      1   File Names
      2   Directory Names
      3   Executable Names
      4   User Names (when they are prefixed with a ˜)
      5   Host Names (when they are prefixed with a @)
      6   Variable Names (when they are prefixed with a $)




  FOSSEE (IIT Bombay)              Using Linux Tools                 43 / 83
The Command Shell    Features of the Shell


History




   Bash saves history of commands typed
   Up and down arrow keys allow to navigate history
   Ctrl-r searches for commands used




  FOSSEE (IIT Bombay)              Using Linux Tools                 44 / 83
The Command Shell    Features of the Shell


Shell Meta Characters


   “meta characters” are special command directives
   File-names shouldn’t have meta-characters
   /<>!$%^&*|{}[]"’‘~;

     $ ls file.*

   Lists file.ext files, where ext can be anything

     $ ls file.?

   Lists file.ext files, where ext is only one character



  FOSSEE (IIT Bombay)              Using Linux Tools                 45 / 83
More text processing


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)                Using Linux Tools   46 / 83
More text processing


sort


  sort can be used to get sorted content
  Command below prints student marks, sorted by name

    $ cut -d " " -f 2- marks1.txt 
      | paste -d " " students.txt - 
      | sort

  The default is sort based on the whole line
  sort can sort based on a particular field




 FOSSEE (IIT Bombay)                Using Linux Tools   47 / 83
More text processing


sort . . .


   The command below sorts based on marks in first subject

     $ cut -d " " -f 2- marks1.txt 
       | paste -d " " students.txt -
       | sort -t " " -k 2 -rn

   -t specifies the delimiter between fields
   -k specifies the field to use for sorting
   -n to choose numerical sorting
   -r for sorting in the reverse order



  FOSSEE (IIT Bombay)                Using Linux Tools      48 / 83
More text processing


grep



  grep is a command line text search utility
  Command below searches & shows the marks of Anne alone

    $ cut -d " " -f 2- marks1.txt 
    | paste -d " " students.txt -
    | grep Anne

  grep is case-sensitive by default




 FOSSEE (IIT Bombay)                Using Linux Tools   49 / 83
More text processing


grep . . .

   -i for case-insensitive searches

     $ cut -d " " -f 2- marks1.txt 
     | paste -d " " students.txt -
     | grep -i Anne

   -v inverts the search
   To see everyone’s marks except Anne’s

     $ cut -d " " -f 2- marks1.txt 
     | paste -d " " students.txt -
     | grep -iv Anne


  FOSSEE (IIT Bombay)                Using Linux Tools   50 / 83
More text processing


tr

     tr translates or deletes characters
     Reads from stdin and outputs to stdout
     Given, two sets of characters, replaces one with other
     The following, replaces all lower-case with upper-case

      $ cat students.txt | tr a-z A-Z

     -s compresses sequences of identical adjacent characters in
     the output to a single one
     Following command removes empty newlines

      $ tr -s ’n’ ’n’


 FOSSEE (IIT Bombay)                Using Linux Tools          51 / 83
More text processing


tr . . .

    -d deletes all specified characters
    Only a single character set argument is required
    The following command removes carriage return characters
    (converting file in DOS/Windows format to the Unix format)

      $ cat foo.txt | tr -d ’r’ > bar.txt

    -c complements the first set of characters
    The following command removes all non-alphanumeric
    characters

      $ tr -cd ’[:alnum:]’


   FOSSEE (IIT Bombay)                Using Linux Tools         52 / 83
More text processing


uniq


  uniq command removes duplicates from sorted input

    $ sort items.txt | uniq

  uniq -u gives lines which do not have any duplicates
  uniq -d outputs only those lines which have duplicates
  -c displays the number of times each line occurs

    $ sort items.txt | uniq -u
    $ sort items.txt | uniq -dc



 FOSSEE (IIT Bombay)                Using Linux Tools      53 / 83
Simple Shell Scripts


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)                Using Linux Tools   54 / 83
Simple Shell Scripts


Shell scripts



   Simply a sequence of shell commands in a file
   To save results of students in results.txt in marks dir

     #!/bin/bash
     mkdir ~/marks
     cut -d " " -f 2- marks1.txt 
     | paste -d " " students.txt - 
     | sort > ~/marks/results.txt




  FOSSEE (IIT Bombay)                Using Linux Tools       55 / 83
Simple Shell Scripts


Shell scripts . . .


    Save the script as results.sh
    Make file executable and then run

      $ chmod u+x results.sh
      $ ./results.sh

    What does the first line in the script do?
    Specify the interpreter or shell which should be used to
    execute the script; in this case bash




   FOSSEE (IIT Bombay)                Using Linux Tools        56 / 83
Simple Shell Scripts


Variables & Comments



     $ name=FOSSEE
     $ count=‘wc -l wonderland.txt‘
     $ echo $count # Shows the value of count

   It is possible to create variables in shell scripts
   Variables can be assigned with the output of commands
   NOTE: There is no space around the = sign
   All text following the # is considered a comment




  FOSSEE (IIT Bombay)                Using Linux Tools     57 / 83
Simple Shell Scripts


echo


  echo command prints out messages

    #!/bin/bash
    mkdir ~/marks
    cut -d " " -f 2- marks1.txt 
    | paste -d " " students.txt - 
    | sort > ~/marks/results.txt
    echo "Results generated."




 FOSSEE (IIT Bombay)                Using Linux Tools   58 / 83
Simple Shell Scripts


Command line arguments
   Shell scripts can be given command line arguments
   Following code allows to specify the results file

     #!/bin/bash
     mkdir ~/marks
     cut -d " " -f 2- marks1.txt 
     | paste -d " " students.txt - 
     | sort > ~/marks/$1
     echo "Results generated."

   $1 corresponds to first command line argument
   $n corresponds to nth command line argument
   It can be run as shown below

     $ ./results.sh grades.txt
  FOSSEE (IIT Bombay)                Using Linux Tools   59 / 83
Simple Shell Scripts


PATH


  The shell searches in a set of locations, for the command
  Locations are saved in “environment” variable called PATH
  echo can show the value of variables

    $ echo $PATH

  Put results.sh in one of these locations
  It can then be run without ./




 FOSSEE (IIT Bombay)                Using Linux Tools         60 / 83
Control structures and Operators


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)                            Using Linux Tools   61 / 83
Control structures and Operators


Control Structures


   if-else
   for loops
   while loops

   test command to test for conditions
   A whole range of tests that can be performed
       STRING1 = STRING2 – string equality
       INTEGER1 -eq INTEGER2 – integer equality
       -e FILE – existence of FILE
   man page of test gives list of various tests



  FOSSEE (IIT Bombay)                            Using Linux Tools   62 / 83
Control structures and Operators


if


     Print message if directory exists in pwd

      #!/bin/bash
      if test -d $1
      then
      echo "Yes, the directory" 
      $1 "is present"
      fi




 FOSSEE (IIT Bombay)                            Using Linux Tools   63 / 83
Control structures and Operators


if-else


  Checks whether argument is negative or not

    #!/bin/bash
    if test $1 -lt 0
    then
    echo "number is negative"
    else
    echo "number is non-negative"
    fi

    $ ./sign.sh -11



 FOSSEE (IIT Bombay)                            Using Linux Tools   64 / 83
Control structures and Operators


[ ] - alias for test

   Square brackets ([]) can be used instead of test


     #!/bin/bash
     if [ $1 -lt 0 ]
     then
     echo "number is negative"
     else
     echo "number is non-negative"
     fi

   spacing is important, when using the square brackets


  FOSSEE (IIT Bombay)                            Using Linux Tools   65 / 83
Control structures and Operators


if-else

  An example script to greet the user, based on the time

    #!/bin/sh
    # Script to greet the user
    # according to time of day
    hour=‘date | cut -c12-13‘
    now=‘date +"%A, %d of %B, %Y (%r)"‘
    if [ $hour -lt 12 ]
    then
    mess="Good Morning 
    $LOGNAME, Have a nice day!"
    fi


 FOSSEE (IIT Bombay)                            Using Linux Tools   66 / 83
Control structures and Operators


if-else . . .

     if [ $hour -gt 12 -a $hour -le 16 ]
     then
     mess="Good Afternoon $LOGNAME"
     fi
     if [ $hour -gt 16 -a $hour -le 18 ]
     then
     mess="Good Evening $LOGNAME"
     fi
     echo -e "$messnIt is $now"

   $LOGNAME has login name (env. variable)
   backquotes store commands outputs into variables


  FOSSEE (IIT Bombay)                            Using Linux Tools   67 / 83
Control structures and Operators


for


Problem
Given a set of .mp3 files, that have names beginning with numbers
followed by their names — 08 - Society.mp3 — rename the
files to have just the names. Also replace any spaces in the name
with hyphens.

    Loop over the list of files
    Process the names, to get new names
    Rename the files




   FOSSEE (IIT Bombay)                            Using Linux Tools   68 / 83
Control structures and Operators


for
  A simple example of the for loop

    for animal in rat cat dog man
    do
    echo $animal
    done

  List of animals, each animal’s name separated by a space
  Loop over the list; animal is a dummy variable
  Echo value of animal — each name in list

    for i in {10..20}
    do
    echo $i
    done
 FOSSEE (IIT Bombay)                            Using Linux Tools   69 / 83
Control structures and Operators


for

  Let’s start with echoing the names of the files

    for i in ‘ls *.mp3‘
    do
    echo "$i"
    done

  Spaces in names cause trouble!
  The following works better

    for i in *.mp3
    do
    echo "$i"
    done

 FOSSEE (IIT Bombay)                            Using Linux Tools   70 / 83
Control structures and Operators


tr & cut
    Replace all spaces with hyphens using tr -s
    Use cut & keep only the text after the first hyphen

  for i in *.mp3
  do
  echo $i|tr -s " " "-"|cut -d - -f 2-
  done
Now mv, instead of just echoing
      for i in *.mp3
      do
      mv $i ‘echo $i|tr -s " " "-"
      |cut -d - -f 2-‘
      done

   FOSSEE (IIT Bombay)                            Using Linux Tools   71 / 83
Control structures and Operators


while

  Continuously execute a block of commands until condition
  becomes false

  program that takes user input and prints it back, until the input
  is quit

    while [ "$variable" != "quit" ]
    do
    read variable
    echo "Input - $variable"
    done
    exit 0


 FOSSEE (IIT Bombay)                            Using Linux Tools   72 / 83
Control structures and Operators


Environment Variables

   Pass information from shell to programs running in it
   Behavior of programs can change based on values of variables
   Environment variables vs. Shell variables
   Shell variables – only current instance of the shell
   Environment variables – valid for the whole session
   Convention – environment variables are UPPER CASE

     $ echo $OSTYPE
     linux-gnu
     $ echo $HOME
     /home/user


  FOSSEE (IIT Bombay)                            Using Linux Tools   73 / 83
Control structures and Operators


Environment Variables . . .


   The following commands show values of all the environment
   variables

     $ printenv | less
     $ env

   Use export to change Environment variables
   The new value is available to all programs started from the
   shell

     $ export PATH=$PATH:$HOME/bin



  FOSSEE (IIT Bombay)                            Using Linux Tools   74 / 83
Miscellaneous Tools


Outline
1    Introduction
2    Getting Started
3    Getting Help
4    Basic File Handling
5    Linux File Hierarchy, Permissions & Ownership
6    Looking at files
7    The Command Shell
       Features of the Shell
8    More text processing
9    Simple Shell Scripts
10   Control structures and Operators
11   Miscellaneous Tools
     FOSSEE (IIT Bombay)               Using Linux Tools   75 / 83
Miscellaneous Tools


find


  Find files in a directory hierarchy
  Offers a very complex feature set
  Look at the man page!

  Find all .pdf files, in current dir and sub-dirs
                $ find . -name ‘‘*.pdf’’
  List all the directory and sub-directory names
                $ find . -type d




 FOSSEE (IIT Bombay)               Using Linux Tools   76 / 83
Miscellaneous Tools


cmp


  Compare two files

    $ find . -name quick.c
    ./Desktop/programs/quick.c
    ./c-folder/quick.c
    $ cmp Desktop/programs/quick.c 
    c-folder/quick.c

  No output when the files are exactly the same
  Else, gives location where the first difference occurs




 FOSSEE (IIT Bombay)               Using Linux Tools      77 / 83
Miscellaneous Tools


diff


  We know the files are different, but want exact differences

    $ diff Desktop/programs/quick.c 
    c-folder/quick.c

  line by line difference between files
  > indicates content only in second file
  < indicates content only in first file




 FOSSEE (IIT Bombay)               Using Linux Tools           78 / 83
Miscellaneous Tools


tar




  tarball – essentially a collection of files
  May or may not be compressed
  Eases the job of storing, backing-up & transporting files




 FOSSEE (IIT Bombay)               Using Linux Tools         79 / 83
Miscellaneous Tools


Extracting an archive



$   mkdir extract
$   cp allfiles.tar extract/
$   cd extract
$   tar -xvf allfiles.tar

     -x — Extract files within the archive
     -f — Specify the archive file
     -v — Be verbose




    FOSSEE (IIT Bombay)               Using Linux Tools   80 / 83
Miscellaneous Tools


Creating an archive




$ tar -cvf newarchive.tar *.txt

   -c — Create archive
   Last argument is list of files to be added to archive




  FOSSEE (IIT Bombay)               Using Linux Tools     81 / 83
Miscellaneous Tools


Compressed archives


   tar can create and extract compressed archives
   Supports compressions like gzip, bzip2, lzma, etc.
   Additional option to handle compressed archives
                       Compression Option
                       gzip          -z
                       bzip2         -j
                       lzma          --lzma

     $ tar -cvzf newarchive.tar.gz *.txt




  FOSSEE (IIT Bombay)               Using Linux Tools   82 / 83
Miscellaneous Tools


Customizing your shell



   Bash reads /etc/profile, ~/.bash_profile,
   ~/.bash_login, and ~/.profile in that order, when
   starting up as a login shell.
   ~/.bashrc is read, when not a login shell
   Put any commands that you want to run when bash starts, in
   this file.




  FOSSEE (IIT Bombay)               Using Linux Tools           83 / 83

Más contenido relacionado

La actualidad más candente

La actualidad más candente (18)

Unix files
Unix filesUnix files
Unix files
 
Lpi Part 1 Linux Fundamentals
Lpi Part 1 Linux FundamentalsLpi Part 1 Linux Fundamentals
Lpi Part 1 Linux Fundamentals
 
Basic Unix
Basic UnixBasic Unix
Basic Unix
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
 
Introduction to UNIX Command-Lines with examples
Introduction to UNIX Command-Lines with examplesIntroduction to UNIX Command-Lines with examples
Introduction to UNIX Command-Lines with examples
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentials
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line concepts
 
Basics of Linux
Basics of LinuxBasics of Linux
Basics of Linux
 
Shell intro
Shell introShell intro
Shell intro
 
Linux introductory-course-day-1
Linux introductory-course-day-1Linux introductory-course-day-1
Linux introductory-course-day-1
 
Introduction to linux day1
Introduction to linux day1Introduction to linux day1
Introduction to linux day1
 
Linux Command Suumary
Linux Command SuumaryLinux Command Suumary
Linux Command Suumary
 
Linux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScITLinux practicals T.Y.B.ScIT
Linux practicals T.Y.B.ScIT
 
Unix(introduction)
Unix(introduction)Unix(introduction)
Unix(introduction)
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
 
Linux command for beginners
Linux command for beginnersLinux command for beginners
Linux command for beginners
 
Linux administration classes in mumbai
Linux administration classes in mumbaiLinux administration classes in mumbai
Linux administration classes in mumbai
 

Similar a 1 using linux_tools

Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdfharikrishnapolaki
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.pptKiranMantri
 
Linux day 2.ppt
Linux day  2.pptLinux day  2.ppt
Linux day 2.pptKalkey
 
(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorialjayaramprabhu
 
Introduction to linux2
Introduction to linux2Introduction to linux2
Introduction to linux2Gourav Varma
 
Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools Vu Hung Nguyen
 
linux-lecture1.ppt
linux-lecture1.pptlinux-lecture1.ppt
linux-lecture1.pptNikhil Raut
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linuxGourav Varma
 
Lesson 1 Linux System Fundamentals
Lesson 1 Linux System Fundamentals  Lesson 1 Linux System Fundamentals
Lesson 1 Linux System Fundamentals Sadia Bashir
 
linux-file-system01.ppt
linux-file-system01.pptlinux-file-system01.ppt
linux-file-system01.pptMeesanRaza
 

Similar a 1 using linux_tools (20)

Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 
beginner.en.print
beginner.en.printbeginner.en.print
beginner.en.print
 
Linux day 2.ppt
Linux day  2.pptLinux day  2.ppt
Linux day 2.ppt
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
21bUc8YeDzZpE
21bUc8YeDzZpE21bUc8YeDzZpE
21bUc8YeDzZpE
 
(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial(Ebook) linux shell scripting tutorial
(Ebook) linux shell scripting tutorial
 
60761 linux
60761 linux60761 linux
60761 linux
 
Introduction to linux2
Introduction to linux2Introduction to linux2
Introduction to linux2
 
Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools Nguyễn Vũ Hưng: Basic Linux Power Tools
Nguyễn Vũ Hưng: Basic Linux Power Tools
 
linux-lecture1.ppt
linux-lecture1.pptlinux-lecture1.ppt
linux-lecture1.ppt
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Linux basic commands tutorial
Linux basic commands tutorialLinux basic commands tutorial
Linux basic commands tutorial
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Lesson 1 Linux System Fundamentals
Lesson 1 Linux System Fundamentals  Lesson 1 Linux System Fundamentals
Lesson 1 Linux System Fundamentals
 
linux-file-system01.ppt
linux-file-system01.pptlinux-file-system01.ppt
linux-file-system01.ppt
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentals
 

1 using linux_tools

  • 1. SEES: Using Linux Tools FOSSEE Department of Aerospace Engineering IIT Bombay FOSSEE (IIT Bombay) Using Linux Tools 1 / 83
  • 2. Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 2 / 83
  • 3. Introduction Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 3 / 83
  • 4. Introduction What is the Linux OS? Free Open Source Operating System Free Free as in Free Speech, not Free Beer Open-Source Permit modifications and redistribution of source code Unix-inspired Linux Kernel + Application software Runs on a variety of hardware Also called GNU/Linux FOSSEE (IIT Bombay) Using Linux Tools 4 / 83
  • 5. Introduction Why Linux? Free as in Free Beer Secure & versatile Why Linux for Scientific Computing? Free as in Free Speech Can run for ever Libraries Parallel Computing FOSSEE (IIT Bombay) Using Linux Tools 5 / 83
  • 6. Getting Started Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 6 / 83
  • 7. Getting Started Logging in GNU/Linux does have a GUI Command Line for this module Hit Ctrl + Alt + F1 Login logout command logs you out FOSSEE (IIT Bombay) Using Linux Tools 7 / 83
  • 8. Getting Started Where am I? Logged in. Where are we? pwd command gives the present working directory $ pwd /home/user FOSSEE (IIT Bombay) Using Linux Tools 8 / 83
  • 9. Getting Started What is in there? ls command lists contents of pwd $ ls jeeves.rst psmith.html blandings.html Music Can also pass directory as argument $ ls Music one.mp3 two.mp3 three.mp3 the Unix world is case sensitive FOSSEE (IIT Bombay) Using Linux Tools 9 / 83
  • 10. Getting Started New folders mkdir creates new directories $ mkdir sees $ ls Special characters need to be escaped OR quoted $ mkdir software engineering $ mkdir "software engg" Generally, use hyphens or underscores instead of spaces in names FOSSEE (IIT Bombay) Using Linux Tools 10 / 83
  • 11. Getting Started Moving around cd command changes the pwd $ cd sees $ pwd /home/user/sees/ Alternately written as cd ./sees Specifying path relative to pwd .. takes one level up the directory structure $ cd .. We could use absolute path instead of relative $ cd /home/user/sees/ FOSSEE (IIT Bombay) Using Linux Tools 11 / 83
  • 12. Getting Started New files touch command creates a blank file $ pwd /home/user $ cd sees $ touch first $ ls first FOSSEE (IIT Bombay) Using Linux Tools 12 / 83
  • 13. Getting Help Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 13 / 83
  • 14. Getting Help What does a command do? whatis gives a quick description of a command $ whatis touch touch (1) - change file timestamps man command gives more detailed description $ man touch Shows all tasks that the command can perform Hit q to quit the man page For more, see the man page of man $ man man FOSSEE (IIT Bombay) Using Linux Tools 14 / 83
  • 15. Getting Help Using additional options -h or -help give summary of command usage $ ls --help List out all files within a directory, recursively $ ls -R Create a new directory along with parents, if required $ pwd /home/user/ $ ls sees/ $ mkdir -p sees/linux-tools/scripts FOSSEE (IIT Bombay) Using Linux Tools 15 / 83
  • 16. Getting Help Searching for a command apropos searches commands based on their descriptions $ apropos remove Returns a list of all commands that contain the search term In this case, we are interested in rm, rmdir FOSSEE (IIT Bombay) Using Linux Tools 16 / 83
  • 17. Basic File Handling Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 17 / 83
  • 18. Basic File Handling Removing files rm is used to delete files $ rm foo rm works only for files; not directories Additional arguments required to remove a directory -r stands for recursive. Removes directory and all of it’s content $ rm -r bar rmdir can also be used; Explore FOSSEE (IIT Bombay) Using Linux Tools 18 / 83
  • 19. Basic File Handling Copying Files cp copies files from one location to another $ cp linux-tools/scripts/foo linux-tools/ New file-name can be used at target location foo copied to new location with the name bar $ cp linux-tools/scripts/foo linux-tools/bar cp overwrites files, unless explicitly asked not to To prevent this, use the -i flag $ cp -i linux-tools/scripts/foo linux-tools/b cp: overwrite ‘bar’? FOSSEE (IIT Bombay) Using Linux Tools 19 / 83
  • 20. Basic File Handling Copying Directories -r is required to copy a directory and all it’s content Copying directories is similar to copying files $ cd /home/user $ cp -ir sees course FOSSEE (IIT Bombay) Using Linux Tools 20 / 83
  • 21. Basic File Handling Moving Files cp and rm would be one way mv command does the job Also takes -i option to prompt before overwriting $ cd /home/user # Assume we have course directory already cre $ mv -i sees/ course/ No prompt! Why? $ ls course sees became a sub-directory of course mv command doesn’t over-write directories -i option is useful when moving files around mv to rename — move to same location with new name FOSSEE (IIT Bombay) Using Linux Tools 21 / 83
  • 22. Linux File Hierarchy, Permissions & Ownership Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 22 / 83
  • 23. Linux File Hierarchy, Permissions & Ownership Linux File Hierarchy / is called the root directory It is the topmost level of the hierarchy For details man hier FOSSEE (IIT Bombay) Using Linux Tools 23 / 83
  • 24. Linux File Hierarchy, Permissions & Ownership Permissions and Access control In a multi-user environment, access control is vital Look at the output of ls -l drwxr-xr-x 5 root users 4096 Jan 21 20:07 The first column shows the permission information First character specifies type of the file Files have -; Directories have d 3 sets of 3 characters — for user, group and others r, w, x — for read, write, execute Either the corresponding character or - is present FOSSEE (IIT Bombay) Using Linux Tools 24 / 83
  • 25. Linux File Hierarchy, Permissions & Ownership Changing the permissions Permissions can be changed by owner of the file chmod command is used -R option to recursively change for all content of a directory Change permissions of foo.sh from -rw-r--r-- to -rwxr-xr-- $ ls -l foo.sh $ chmod ug+x foo.sh $ ls -l foo.sh FOSSEE (IIT Bombay) Using Linux Tools 25 / 83
  • 26. Linux File Hierarchy, Permissions & Ownership Symbolic modes Reference Class Description u user the owner of the file g group users who are members of the file’s group o others users who are not hte owner of the file or members of a all all three of the above; is the same as ugo Operator Description + adds the specified modes to the specified classes - removes the specified modes from the specified classes = the modes specified are to be made the exact modes for the spe Mode Name Description r read read a file or list a directory’s contents w write write to a file or directory x execute execute a file or recurse a directory tree FOSSEE (IIT Bombay) Using Linux Tools 26 / 83
  • 27. Linux File Hierarchy, Permissions & Ownership Changing Ownership of Files chown changes the owner and group By default, the user who creates file is the owner The default group is set as the group of the file $ chown alice:users wonderland.txt Did it work? Not every user can change ownership Super-user or root user alone is empowered FOSSEE (IIT Bombay) Using Linux Tools 27 / 83
  • 28. Looking at files Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 28 / 83
  • 29. Looking at files cat Displays the contents of files $ cat foo.txt Concatenates the text of multiple files $ cat foo.txt bar.txt Not-convenient to view long files FOSSEE (IIT Bombay) Using Linux Tools 29 / 83
  • 30. Looking at files less View contents of a file one screen at a time $ less wonderland.txt q: Quit Arrows/Page Up/Page Down/Home/End: Navigation ng: Jump to line number n /pattern: Search. Regular expressions can be used h: Help FOSSEE (IIT Bombay) Using Linux Tools 30 / 83
  • 31. Looking at files wc Statistical information about the file the number of lines in the file the number of words the number of characters $ wc wonderland.txt FOSSEE (IIT Bombay) Using Linux Tools 31 / 83
  • 32. Looking at files head & tail let you see parts of files, instead of the whole file head – start of a file; tail – end of a file show 10 lines by default $ head wonderland.txt -n option to change the number of lines $ head -n 1 wonderland.txt tail is commonly used to monitor files -f option to monitor the file Ctrl-C to interrupt $ tail -f /var/log/dmesg FOSSEE (IIT Bombay) Using Linux Tools 32 / 83
  • 33. Looking at files cut Allows you to view only certain sections of lines Let’s take /etc/passwd as our example root:x:0:0:root:/root:/bin/bash View only user names of all the users (first column) $ cut -d : -f 1 /etc/passwd -d specifies delimiter between fields (default TAB) -f specifies the field number Multiple fields by separating field numbers with comma $ cut -d : -f 1,5,7 /etc/passwd FOSSEE (IIT Bombay) Using Linux Tools 33 / 83
  • 34. Looking at files cut Allows choosing on the basis of characters or bytes Example below gets first 4 characters of /etc/passwd $ cut -c 1-4 /etc/passwd One of the limits of the range can be dropped Sensible defaults are assumed in such cases $ cut -c -4 /etc/passwd $ cut -c 10- /etc/passwd FOSSEE (IIT Bombay) Using Linux Tools 34 / 83
  • 35. Looking at files paste Joins corresponding lines from two different files students.txt marks.txt Hussain 89 92 85 Dilbert 98 47 67 Anne 67 82 76 Raul 78 97 60 Sven 67 68 69 $ paste students.txt marks.txt $ paste -s students.txt marks.txt -s prints content, one below the other If first column of marks file had roll numbers? How do we get a combined file with the same output as above (i.e. without roll numbers). We need to use cut & paste together. But how? FOSSEE (IIT Bombay) Using Linux Tools 35 / 83
  • 36. The Command Shell Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 36 / 83
  • 37. The Command Shell Redirection and Piping $ cut -d " " -f 2- marks1.txt > /tmp/m_tmp.txt $ paste -d " " students.txt m_tmp.txt or $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - The first solution used Redirection The second solution uses Piping FOSSEE (IIT Bombay) Using Linux Tools 37 / 83
  • 38. The Command Shell Redirection The standard output (stdout) stream goes to the display Not always, what we need First solution, redirects output to a file > states that output is redirected; It is followed by location to redirect $ command > file1 > creates a new file at specified location » appends to a file at specified location FOSSEE (IIT Bombay) Using Linux Tools 38 / 83
  • 39. The Command Shell Redirection . . . Similarly, the standard input (stdin) can be redirected $ command < file1 input and the output redirection could be combined $ command < infile > outfile Standard error (stderr) is the third standard stream All error messages come through this stream stderr can also be redirected FOSSEE (IIT Bombay) Using Linux Tools 39 / 83
  • 40. The Command Shell Redirection . . . Following example shows stderr redirection Error is printed out in the first case Error message is redirected, in the second case $ cut -d " " -c 2- marks1.txt > /tmp/m_tmp.txt $ cut -d " " -f 2- marks1.txt 1> /tmp/m_tmp.txt 2> /tmp/m_err.txt 1> redirects stdout; 2> redirects stderr $ paste -d " " students.txt m_tmp.txt FOSSEE (IIT Bombay) Using Linux Tools 40 / 83
  • 41. The Command Shell Piping $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - - instead of FILE asks paste to read from stdin cut command is a normal command the | seems to be joining the two commands Redirects output of first command to stdin, which becomes input to the second command This is called piping; | is called a pipe FOSSEE (IIT Bombay) Using Linux Tools 41 / 83
  • 42. The Command Shell Piping Roughly same as – 2 redirects and a temporary file $ command1 > tempfile $ command2 < tempfile $ rm tempfile Any number of commands can be piped together FOSSEE (IIT Bombay) Using Linux Tools 42 / 83
  • 43. The Command Shell Features of the Shell Tab-completion Hit tab to complete an incompletely typed word Tab twice to list all possibilities when ambiguous completion Bash provides tab completion for the following. 1 File Names 2 Directory Names 3 Executable Names 4 User Names (when they are prefixed with a ˜) 5 Host Names (when they are prefixed with a @) 6 Variable Names (when they are prefixed with a $) FOSSEE (IIT Bombay) Using Linux Tools 43 / 83
  • 44. The Command Shell Features of the Shell History Bash saves history of commands typed Up and down arrow keys allow to navigate history Ctrl-r searches for commands used FOSSEE (IIT Bombay) Using Linux Tools 44 / 83
  • 45. The Command Shell Features of the Shell Shell Meta Characters “meta characters” are special command directives File-names shouldn’t have meta-characters /<>!$%^&*|{}[]"’‘~; $ ls file.* Lists file.ext files, where ext can be anything $ ls file.? Lists file.ext files, where ext is only one character FOSSEE (IIT Bombay) Using Linux Tools 45 / 83
  • 46. More text processing Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 46 / 83
  • 47. More text processing sort sort can be used to get sorted content Command below prints student marks, sorted by name $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort The default is sort based on the whole line sort can sort based on a particular field FOSSEE (IIT Bombay) Using Linux Tools 47 / 83
  • 48. More text processing sort . . . The command below sorts based on marks in first subject $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort -t " " -k 2 -rn -t specifies the delimiter between fields -k specifies the field to use for sorting -n to choose numerical sorting -r for sorting in the reverse order FOSSEE (IIT Bombay) Using Linux Tools 48 / 83
  • 49. More text processing grep grep is a command line text search utility Command below searches & shows the marks of Anne alone $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep Anne grep is case-sensitive by default FOSSEE (IIT Bombay) Using Linux Tools 49 / 83
  • 50. More text processing grep . . . -i for case-insensitive searches $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep -i Anne -v inverts the search To see everyone’s marks except Anne’s $ cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | grep -iv Anne FOSSEE (IIT Bombay) Using Linux Tools 50 / 83
  • 51. More text processing tr tr translates or deletes characters Reads from stdin and outputs to stdout Given, two sets of characters, replaces one with other The following, replaces all lower-case with upper-case $ cat students.txt | tr a-z A-Z -s compresses sequences of identical adjacent characters in the output to a single one Following command removes empty newlines $ tr -s ’n’ ’n’ FOSSEE (IIT Bombay) Using Linux Tools 51 / 83
  • 52. More text processing tr . . . -d deletes all specified characters Only a single character set argument is required The following command removes carriage return characters (converting file in DOS/Windows format to the Unix format) $ cat foo.txt | tr -d ’r’ > bar.txt -c complements the first set of characters The following command removes all non-alphanumeric characters $ tr -cd ’[:alnum:]’ FOSSEE (IIT Bombay) Using Linux Tools 52 / 83
  • 53. More text processing uniq uniq command removes duplicates from sorted input $ sort items.txt | uniq uniq -u gives lines which do not have any duplicates uniq -d outputs only those lines which have duplicates -c displays the number of times each line occurs $ sort items.txt | uniq -u $ sort items.txt | uniq -dc FOSSEE (IIT Bombay) Using Linux Tools 53 / 83
  • 54. Simple Shell Scripts Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 54 / 83
  • 55. Simple Shell Scripts Shell scripts Simply a sequence of shell commands in a file To save results of students in results.txt in marks dir #!/bin/bash mkdir ~/marks cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/results.txt FOSSEE (IIT Bombay) Using Linux Tools 55 / 83
  • 56. Simple Shell Scripts Shell scripts . . . Save the script as results.sh Make file executable and then run $ chmod u+x results.sh $ ./results.sh What does the first line in the script do? Specify the interpreter or shell which should be used to execute the script; in this case bash FOSSEE (IIT Bombay) Using Linux Tools 56 / 83
  • 57. Simple Shell Scripts Variables & Comments $ name=FOSSEE $ count=‘wc -l wonderland.txt‘ $ echo $count # Shows the value of count It is possible to create variables in shell scripts Variables can be assigned with the output of commands NOTE: There is no space around the = sign All text following the # is considered a comment FOSSEE (IIT Bombay) Using Linux Tools 57 / 83
  • 58. Simple Shell Scripts echo echo command prints out messages #!/bin/bash mkdir ~/marks cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/results.txt echo "Results generated." FOSSEE (IIT Bombay) Using Linux Tools 58 / 83
  • 59. Simple Shell Scripts Command line arguments Shell scripts can be given command line arguments Following code allows to specify the results file #!/bin/bash mkdir ~/marks cut -d " " -f 2- marks1.txt | paste -d " " students.txt - | sort > ~/marks/$1 echo "Results generated." $1 corresponds to first command line argument $n corresponds to nth command line argument It can be run as shown below $ ./results.sh grades.txt FOSSEE (IIT Bombay) Using Linux Tools 59 / 83
  • 60. Simple Shell Scripts PATH The shell searches in a set of locations, for the command Locations are saved in “environment” variable called PATH echo can show the value of variables $ echo $PATH Put results.sh in one of these locations It can then be run without ./ FOSSEE (IIT Bombay) Using Linux Tools 60 / 83
  • 61. Control structures and Operators Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 61 / 83
  • 62. Control structures and Operators Control Structures if-else for loops while loops test command to test for conditions A whole range of tests that can be performed STRING1 = STRING2 – string equality INTEGER1 -eq INTEGER2 – integer equality -e FILE – existence of FILE man page of test gives list of various tests FOSSEE (IIT Bombay) Using Linux Tools 62 / 83
  • 63. Control structures and Operators if Print message if directory exists in pwd #!/bin/bash if test -d $1 then echo "Yes, the directory" $1 "is present" fi FOSSEE (IIT Bombay) Using Linux Tools 63 / 83
  • 64. Control structures and Operators if-else Checks whether argument is negative or not #!/bin/bash if test $1 -lt 0 then echo "number is negative" else echo "number is non-negative" fi $ ./sign.sh -11 FOSSEE (IIT Bombay) Using Linux Tools 64 / 83
  • 65. Control structures and Operators [ ] - alias for test Square brackets ([]) can be used instead of test #!/bin/bash if [ $1 -lt 0 ] then echo "number is negative" else echo "number is non-negative" fi spacing is important, when using the square brackets FOSSEE (IIT Bombay) Using Linux Tools 65 / 83
  • 66. Control structures and Operators if-else An example script to greet the user, based on the time #!/bin/sh # Script to greet the user # according to time of day hour=‘date | cut -c12-13‘ now=‘date +"%A, %d of %B, %Y (%r)"‘ if [ $hour -lt 12 ] then mess="Good Morning $LOGNAME, Have a nice day!" fi FOSSEE (IIT Bombay) Using Linux Tools 66 / 83
  • 67. Control structures and Operators if-else . . . if [ $hour -gt 12 -a $hour -le 16 ] then mess="Good Afternoon $LOGNAME" fi if [ $hour -gt 16 -a $hour -le 18 ] then mess="Good Evening $LOGNAME" fi echo -e "$messnIt is $now" $LOGNAME has login name (env. variable) backquotes store commands outputs into variables FOSSEE (IIT Bombay) Using Linux Tools 67 / 83
  • 68. Control structures and Operators for Problem Given a set of .mp3 files, that have names beginning with numbers followed by their names — 08 - Society.mp3 — rename the files to have just the names. Also replace any spaces in the name with hyphens. Loop over the list of files Process the names, to get new names Rename the files FOSSEE (IIT Bombay) Using Linux Tools 68 / 83
  • 69. Control structures and Operators for A simple example of the for loop for animal in rat cat dog man do echo $animal done List of animals, each animal’s name separated by a space Loop over the list; animal is a dummy variable Echo value of animal — each name in list for i in {10..20} do echo $i done FOSSEE (IIT Bombay) Using Linux Tools 69 / 83
  • 70. Control structures and Operators for Let’s start with echoing the names of the files for i in ‘ls *.mp3‘ do echo "$i" done Spaces in names cause trouble! The following works better for i in *.mp3 do echo "$i" done FOSSEE (IIT Bombay) Using Linux Tools 70 / 83
  • 71. Control structures and Operators tr & cut Replace all spaces with hyphens using tr -s Use cut & keep only the text after the first hyphen for i in *.mp3 do echo $i|tr -s " " "-"|cut -d - -f 2- done Now mv, instead of just echoing for i in *.mp3 do mv $i ‘echo $i|tr -s " " "-" |cut -d - -f 2-‘ done FOSSEE (IIT Bombay) Using Linux Tools 71 / 83
  • 72. Control structures and Operators while Continuously execute a block of commands until condition becomes false program that takes user input and prints it back, until the input is quit while [ "$variable" != "quit" ] do read variable echo "Input - $variable" done exit 0 FOSSEE (IIT Bombay) Using Linux Tools 72 / 83
  • 73. Control structures and Operators Environment Variables Pass information from shell to programs running in it Behavior of programs can change based on values of variables Environment variables vs. Shell variables Shell variables – only current instance of the shell Environment variables – valid for the whole session Convention – environment variables are UPPER CASE $ echo $OSTYPE linux-gnu $ echo $HOME /home/user FOSSEE (IIT Bombay) Using Linux Tools 73 / 83
  • 74. Control structures and Operators Environment Variables . . . The following commands show values of all the environment variables $ printenv | less $ env Use export to change Environment variables The new value is available to all programs started from the shell $ export PATH=$PATH:$HOME/bin FOSSEE (IIT Bombay) Using Linux Tools 74 / 83
  • 75. Miscellaneous Tools Outline 1 Introduction 2 Getting Started 3 Getting Help 4 Basic File Handling 5 Linux File Hierarchy, Permissions & Ownership 6 Looking at files 7 The Command Shell Features of the Shell 8 More text processing 9 Simple Shell Scripts 10 Control structures and Operators 11 Miscellaneous Tools FOSSEE (IIT Bombay) Using Linux Tools 75 / 83
  • 76. Miscellaneous Tools find Find files in a directory hierarchy Offers a very complex feature set Look at the man page! Find all .pdf files, in current dir and sub-dirs $ find . -name ‘‘*.pdf’’ List all the directory and sub-directory names $ find . -type d FOSSEE (IIT Bombay) Using Linux Tools 76 / 83
  • 77. Miscellaneous Tools cmp Compare two files $ find . -name quick.c ./Desktop/programs/quick.c ./c-folder/quick.c $ cmp Desktop/programs/quick.c c-folder/quick.c No output when the files are exactly the same Else, gives location where the first difference occurs FOSSEE (IIT Bombay) Using Linux Tools 77 / 83
  • 78. Miscellaneous Tools diff We know the files are different, but want exact differences $ diff Desktop/programs/quick.c c-folder/quick.c line by line difference between files > indicates content only in second file < indicates content only in first file FOSSEE (IIT Bombay) Using Linux Tools 78 / 83
  • 79. Miscellaneous Tools tar tarball – essentially a collection of files May or may not be compressed Eases the job of storing, backing-up & transporting files FOSSEE (IIT Bombay) Using Linux Tools 79 / 83
  • 80. Miscellaneous Tools Extracting an archive $ mkdir extract $ cp allfiles.tar extract/ $ cd extract $ tar -xvf allfiles.tar -x — Extract files within the archive -f — Specify the archive file -v — Be verbose FOSSEE (IIT Bombay) Using Linux Tools 80 / 83
  • 81. Miscellaneous Tools Creating an archive $ tar -cvf newarchive.tar *.txt -c — Create archive Last argument is list of files to be added to archive FOSSEE (IIT Bombay) Using Linux Tools 81 / 83
  • 82. Miscellaneous Tools Compressed archives tar can create and extract compressed archives Supports compressions like gzip, bzip2, lzma, etc. Additional option to handle compressed archives Compression Option gzip -z bzip2 -j lzma --lzma $ tar -cvzf newarchive.tar.gz *.txt FOSSEE (IIT Bombay) Using Linux Tools 82 / 83
  • 83. Miscellaneous Tools Customizing your shell Bash reads /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, when starting up as a login shell. ~/.bashrc is read, when not a login shell Put any commands that you want to run when bash starts, in this file. FOSSEE (IIT Bombay) Using Linux Tools 83 / 83