CMPSCI 197J: HW2

 NAME: ..........................................                                            DUE DATE: Jan 12, 1999
 

Answer the following questions and write out the exact command(s) you used (100 points)

       A.                                                                                                                        25 points

    1. List all  files in your homedirectory (including dot files).
    ls -a

    2. List only the dot files in your homedirectory and recursively list the directories starting with a dot.
    ls -r .*

    3. Use the command file with the option -f to display the types of the files in your home directory with a one line command.

    ls > ff | file -f ff |  cut -f2 -d ':' | sort | uniq

    4. What is the  PID of your login shell? Explain in detail your choice of a number from the output of the command ps.
    $ ps aux | grep bash       The shell with the earliest start time info.
    araja:~$ ps aux | grep bash
    araja     8357  0.0  0.3 1.97M 384K ttyp2    S    09:22:31     0:00.30 bash
    araja     8214  0.0  0.3 1.94M 392K ttyp1    I  + 08:48:11     0:00.63 bash
    In this case its 8214.

    5. Create a file that contains the process numbers associated with at least two jobs arranged in descending order.
    example contents of output file:
    2930
    2928

    jobs -l > o2 | cut -f3 -d ' ' o2 | sort -r

    B.                                                                                                                        75 points

    1. Write a script which will write your username, your homedirectory, the type of terminal and the printer that lp uses, so that the output looks like

    Your username is chris
    Home directory is /cs/ugrad/chris
    You are using a terminal which is a vt100
    The default lineprinter is cs/p1.

    cat >myscript
    echo Your user name is $USER
    echo Home directory is  $HOME
    echo You are using a terminal which is a $TERM
    echo The default lineprinter is $PRINTER
    Control-D

    2.  Write a script which finds the oldest file in your home directory and prints name and the size of file on the screen preceded by the message

    The name and size of file are:
    xyz      475

    ls -lt | tail -n 1 | cut -c16-22,37-40

    3.  List names of files in your home directory which

    1.  start with a d;            ls d*
    2. end with an e;            ls *e
    3. start with y and ends with h;   ls y*h
    4. contains exactly 4 letters        ls ????
    5. contains at  least 4 letters       ls ????*


    4.  Use the commands ps, kill, jobs, fg, bg, grep

    1. Display the list of current processes                   ps u
    2. Displays only the active processes.                     ps u | grep "R"
    3. List all the processes(by all users) on the system    ps aux
    4. Run the command top. How will u exit it?             Control-Z, Control-C
    5. What does the command kill -9 %1 do?                 Kills job #1


    5.  Redirections and commands cat, wc, tail, head.
     

    1. How many lines are there in the file /etc/termcap? How many words? How many characters?      wc /etc/termcap      596    2425   22234
    2. Print the 18th line, the last 20 lines  head -18 /etc/termcap | tail -1, tail -20 /etc/termcap
    3. How many words are there in the lines 30-40. head -40 /etc/termcap | tail -11 | wc -w
    4. Create a file which contains the first 10 lines of termcap and another file which contains the last 10 lines.     head -10 termcap > debut, tail -10 termcap > fin
    5. Combine the contents of the two files created above and put it in a new file called extremes.              cat debut fin > extremes


    Extra Credit:                                                                    (15 points)

    Explain in detail what the following 2 commands do

    echo baz 2>"foo" >& 2
    The word 'baz' will be printed in the file  foo. We are redirecting STDERR to foo and then redirecting STDOUT TO STDERR. So all output goes to foo.
     

    echo baz >&2 2> "foo"
     Here the order is important. We are first redirecting STDOUT to STDERR and then changing STDERR to foo. The word baz will be printed on the screen and not in file foo.