Lab 9

Back Up Next

horizontal rule

Home
Lab 1
Lab 2
Lab 3
Lab 4
Lab 5
Lab 6
Lab 7
Lab 8
Lab 9
Lab 10
Lab 11
Lab 12
Lab 13
Lab 14
Lab 15
Lab 16
Lab 17
Lab 18
Lab 19
Lab 20

Lab 9

(Chapter 9)

 

 


 

Learning Objectives

  1. To learn how to display contents of ‘text’ files
  2. To learn how to copy, append, move, and remove files
  3. To learn how to combine files
  4. To learn how to determine the size of a file
  5. To learn how to compare ‘text’ files
  6. To learn to use the various printer related commands

Lab Work

In this lab, you will practice the basic file related commands in UNIX. For this purpose, you will make a few files and use then in this and some of the subsequent chapters.

  1. Log on to your UNIX system
  2. Create a few small to large size ‘text’ files for use in this and subsequent chapters. You can create a medium size ‘text’ file by executing the man cat > mediumFile command and a large size ‘text’ file by using the man bash > largeFile command. Finally, you can prepare a small size file by entering several lines of student data in a file called smallFile. A line in this file is of the following format, with fields separated by tabs:

FirstName        LastName        Major   GPA    Email    Phone

Here is the minimum amount of data that you will have in this file.

John        Doe               ECE     3.54     doe@jd.home.org              111.222.3333

James      Davis             ECE     3.71     davis@jd.work.org             111.222.1111

Al            Davis             CS       2.63     davis@a.lakers.org             111.222.2222

Ahmad    Rashid           MBA   3.04     ahmad@mba.org                111.222.4444

Sam        Chu               ECE     3.68     chu@sam.ab.com               111.222.5555

Arun        Roy               SS        3.86     roy@ss.arts.edu                 111.222.8888

Rick        Marsh           CS       2.34     marsh@a.b.org                   111.222.6666

James      Adam            CS       2.77     jadam@a.b.org                  111.222.7777

Art          Pohm            ECE     4.00     pohm@ap.a.org                 111.222.9999

John        Clark             ECE     2.68     clark@xyz.ab.com              111.111.5555

Nabeel    Ali                 EE        3.56     ali@ee.eng.edu                   111.111.8888

Tom        Nelson          ECE     3.81     nelson@tn.abc.org              111.111.6666

Pat          King              SS        3.77     king@pk.xyz.org                111.111.7777

Jake        Zulu               CS       3.00     zulu@jz.sa.org                    111.111.9999

John        Lee               EE        3.64     jlee@j.lee.com                   111.111.2222

Sunil        Raj                ECE     3.86     raj@sr.cs.edu                     111.111.3333

Charles    Right             EECS   3.31     right@cr.abc.edu                111.111.4444

Diane      Rover            ECE     3.87     rover@dr.xyz.edu               111.111.5555

Aziz         Inan               EECS   3.75     ainan@ai.abc.edu               111.111.1111

 

  1. Use the cat and nl commands to display the smallFile with line numbers. Outputs of both commands should look exactly the same. Show the commands that you used to accomplish the task. Are the two commands equivalent if smallFile had blank lines?

 

 

 

 

 

  1. Display the smallFile and mediumFile 18 lines at a time using the more command. Show your command; there is no need to show the output of your command.

 

 

  1. You need to display the first 12 lines of largeFile and last five lines of the smallFile. What commands will you use? What command will you use to display the tail of the smallFile in the reverse order, starting with line number 6. Show your session.

 

 

 

 

  1. The purpose of this part of the lab is to have you appreciate the working of tail command with –f option. Make a copy of the smallFile in the file called dataFile. Then create a C program file with the following program in it. Name the file ch9.c.

#include <stdio.h>

 

main()

{

    int i;

 

    i = 0;

    sleep(10);

    while (i < 5) {

        system("date");

        sleep(5);

        i++;

    }

    while (1) {

        system("date");

        sleep(10);

    }

}

Now run the following commands, in this order. The first command produces the executable code for the C program in the generate file. The second command appends the output of the date command to the contents of dataFile, initially every five seconds and then every 10 seconds. Note down the number in brackets that the system displays after you have submitted the second command to the system. The tail –f dataFile command displays the current contents of dataFile and then displays the data newly appended to dataFile.

gcc ch9.c –o generate

generate >> dataFile &

tail –f dataFile

After you have seen the working the tail –f command for a minute or so, terminate it with <Ctrl-C> and terminate the generate command by using the kill –9 12345, where 12345 is the number that you noted down above after submitting the generate command to the system. Use the tail dataFile command to see the data that was appended to the file. Show your session.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. You made a copy of the smallFile in dataFile. Use the ls –l command to see whether the modification times for the two files are the same or different. They are different; the modification time for dataFile should be the time when it was created with. What command would you use to preserve the modification time for the file? What are the inode numbers for the two files. Now move dataFile to newDataFile. What is the inode number for newDataFile? It should be the same as that of dataFile. Is it? If it is, explain why. Now move newDataFile to the /tmp directory. What is the inode number for /tmp/newDataFile? Most likely, it isn’t the same as that of newDataFile in your current directory. Why? Show your session.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Display the sizes of smallFile, mediumFile, largeFile, and /tmp/newDataFile in byes, words, and lines. The size of smallFile and /tmp/newDataFile should be the same. Can you use another command to show the size of the file in bytes? What is it? Show your session.

 

 

 

 

 

 

 

9.      Reproduce the sessions described in sections 9.6 and 9.7 (pp. 232-236). Show results of all the steps involved in these sessions. Show your session.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10.  Print two copies of smallFile with line numbers and a header at the top. What command did you use? Could you have used another command to accomplish the same task? Show your session below.

 

 

 

11.  Log out.

 


 

 

horizontal rule

Back to CS140U Homepage
This page was last modified September 26, 2004
wmorales@pcc.edu