Lab04 - Linux Utilities

4.1.1 Check File System Usage

In this session, you use the df command to determine the usage of the file systems on your hard drive. Log in to the user account for this and the following the procedures.

  1. Change to your home directory by typing cd ~, and use pwd to make sure the current directory is in your home directory.
  2. Type sudo df, the system may ask your password, then enter your password. Record your output:
  3. You can specify a file system as an argument. The statistics for that file system alone appear on the screen. Type df /dev/sda1 (or another partitioned disk appropriate to your system), and press Enter. You see the disk statistics for that volume only. Record your observation:
  4. The –h option causes the numbers to print in a human-readable form. Instead of displaying raw numbers for size, the amount of disk space used, and the amount of space available, the statistics are printed in a kilobyte, megabyte, or gigabyte format. Type df –h and then press Enter. Record your observation:
  5. Type clear and press Enter to clear the screen.

4.1.2 Check Disk Use

When you want to see directory, subdirectory, and file size information, use the du command. In this section, you use the du command to learn about your disk use.

  1. To receive a report on disk usage starting at your home directory, type du | more, and then press Enter. Record your output:
  2. The output shows the number of blocks used in each subdirectory. Type q to exit the more command.
  3. To view a similar report on disk usage by the number of bytes instead of by 512-byte blocks, start at your home directory, type du –b | more, and then press Enter.
  4. Type q to exit the more command.
  5. Like the df command, the du command supports the –h option to display statistics in a human-readable format. Type du –h | more and then press Enter. Record your output:
  6. Type clear and press Enter.

4.1.3 Search file

In this section, you use the find command to search for and delete all occurrences of a.out and core. The steps that follow assume you have a source directory in your home directory. The test files, a.out and core, are quickly created using the touch command, which, when followed by the file names, creates empty files. The tilde (~) ensures that these files go into your home and source directories. You should already have a source directory for this section. If not, create a source directory under your home directory before you start.

  1. Create some garbage files, core and a.out, and place them in your home directory and in the source subdirectory under your home directory. First type touch ~/core; touch ~/a.out and press Enter to create the files in your home directory. Next type touch ~/source/core; touch ~/source/a.out and press Enter.
  2. Verify that you are in your home directory, type find . "(" -name a.out -o -name core ")" and then press Enter. You should see a listing of the four files you created in step 1, the core and a.out files in the home directory and in the source directory under the home directory.
  3. Remove the garbage files by typing find . "(" -name a.out -o -name core ")" -exec rm {} \; and then pressing Enter.
  4. Check that the files have been removed by repeating the find command you entered in step 2. If the files have been removed, there is no output. Record your output:

4.1.4 Use top Utility

Sometimes, your system might respond slowly or seem to have delays. In these conditions, it is useful to employ the top command to monitor CPU use by processes and other system information, as you learn in this session.

  1. Display the CPU activity by typing top and then pressing Enter. Record your screen:
  2. The processes are shown in the order of the amount of CPU time they use. After looking at the display for a short time, press q to exit from the top utility.
  3. Run the top utility again. Notice that the far-left column of information, labeled PID. This column lists the process id of each process shown. Record PID of top command:
  4. Press k to initiate the kill command. The top utility asks to enter the PID to kill. Enter the PID of the top command. Press Enter to kill the process (You might have to press Enter a second time to return to a command prompt.) Type clear and press Enter to clear the lines from the screen. The top utility is no longer running.
  5. Run the top utility in secure mode by typing top -s and press Enter.
  6. Press k to initiate kill command. Because the top is running in secure mode, it displays the message "can't kill in secure mode". Record your screen:
  7. Press q to exit the top utility.
  8. Type clear and press Enter to clear the lines from the screen.

4.1.5 free command

Plan to periodically monitor memory use in your computer, particularly if the computer seems to run slowly when you use specific programs. In this project, you use the free command to monitor memory.

  1. Type free and press Enter. The command displays the amount of total, used, and free memory. It also displays the amount of shared memory, buffer memory, and cached memory. In addition, the amount of total, used, and free swap memory is shown. By default, all amounts are shown in kilobytes.
  2. Type free –m and press Enter to see the free command's output in megabytes.
  3. Type free –t and press Enter to see memory use totals:

There are times when you might want to send the results of the top and free utilities to a computer support person for help. In this session, you save the results from these utilities to different files and then print the files. To save and print the displays generated by the top and free utilities:

  1. Redirect the output of the top utility to a file in your current directory by typing top > topdata and then press Enter.
  2. Wait about 10 seconds, and then press q to exit the top utility. Type more topdata and press Enter to confirm you have written the information to the topdata file. Type q to exit the more display of the file contents.
  3. Redirect the output of the free utility to a file in your current directory by typing free > freedata and then pressing Enter. Type more freedata and press Enter to verify the contents of the freedata file. Record your output:
  4. Type clear and press Enter to clear the screen.

4.1.6 Run a Program in the background

In Linux/Unix, you can run programs in the background. In this session, you practice running the top program in the background.

  1. Experiment with running a program in the background by running the top program in the background. Type top& and press Enter.
    After you enter the command, the system reports the process id or PID (id that identifies the process) used by the program that you started in the background. Write down our PID:

    The top utility is running, but because it runs in the background, you see no other output.
  2. Continue to run the top utility in the background.

4.1.7 View Processes

In this session, you learn how to use the ps command to view processes on your system.

  1. Type ps and press Enter. The output of the ps command includes this information about each process you are running:
    • PID
    • Name of the station where the process started
    • Amount of time the process has been running
    • Name of the process
    Notice that top utility might still be running in the background, or you might see a message at the end of the ps display that top has stopped running.
  2. To see a list of all processes running on the system, type ps –a | more, and press Enter.
  3. Press the spacebar until the command finishes its output.
  4. Notice that with the command options you've used so far, it is difficult to tell the status of a process, such as whether or not it has finished. Type ps l (that is, a lower-case of letter l and nota one), and press Enter to see the long display. The WCHAN column shows the status of a process, such as "finish".
  5. Now consider that you want to know who is running a particular process, by user name. Type ps –a u | more and press Enter. Record your screen:

    Press the spacebar to scroll through the listing. Notice that many processes are being run by root as well as by your account.

4.1.8 Stop a Process

Sometimes, you need to stop a process, such as one running in the background or one that you no longer want to use. In this session, you use the kill command to stop a process.

  1. In this step, you terminate the top utility that you started before and that might still be running in the background. (If you received a message that it stopped, continue anyway for practice.)
    Type ps and press Enter. Look at the list of processes to find the top utility's PID.
  2. Type one of these commands (both perform the same operation): kill <process id> and press Enter, or kill %top and press Enter.
  3. Type ps l and press Enter to see a list of the processes and to check the status of the top process. Record your output:

4.1.9 Spelling Cehck

In you often work with plain-text documents, it is helpful to know about the ispell command for spell checking. You practice using ispell in this project.

  1. Use the nano or other editor to create a file called document1.
  2. Enter the following text, with misspellings:
    This is a document that describes our newest and fastest machinnery. Take the time to lern how to use each piece of equipment.
  3. Save the file and exit the editor.
  4. (Type sudo apt-get install ispell and password to install ispell if you do not have it. )
  5. Scan the file for spelling errors by typing ispell document1 and then press Enter.
  6. To correct the word "machinnery", look at the options at the bottom of the screen, and find the one that says "machinery". Type the number of that option to correct the misspelling, or you can select the r (Replace) option, retype the word after the With: Prompt, and press Enter.
    Notice that the word is then corrected in the text.
  7. Do the same for "lern".
  8. The program exists and returns you to the command line. Type cat documnet1 and press Enter. The misspelled words have been corrected. Record your screen:

4.1.10 Compare the files

In this session, you create two files and use the cmp command to compare the differences. To compare two files with the cmp command:

  1. Use the nano or other editor to create the file file1, containing this text:
    This is file 1.
    It is a practice file.
    It belongs to me.
  2. Save the file and exit the editor.
  3. Use the editor to create the file file2, containing the text:
    This is file 2.
    It is a practice file.
    It belongs to you.
  4. Save the file and exit the editor.
  5. At the command line, type cmp file1 file2, and press Enter. Your screen looks like:
  6. These results means that the two files differ, with the first point of differences at the 14th character in both files.
  7. Type clear and press Enter to clear the screen.

Include your experiences and answers to all the underlying parts in your report.