[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

File Examination Commands

Contents Previous Topic Next Topic

We have already seen the grep command which searches a file. This topic looks at some of the commands you can use to examine one or more files, including:

diff

The diff file1 file2 command is used for comparing two (usually similar) files. Suppose I had down loaded the file SingleList.h for Project 1. I could (if I was not using RCS) copy the file as a back-up, edit the file, and then compare my changes to the original.

{ecelinux:1} cp SingleList.h SingleList.bak.h
{ecelinux:2} gvim SingleList.h   # edit the file, make some changes
{ecelinux:3} diff SingleList.h SingleList.bak.h
74c74,75
<       return count;
---
>       // enter your implementation here
>       return 0;
79c80,81
<       return head == 0;
---
>       // enter your implementation here
>       return true;
84,88c86,87
<       if ( count() == 0 ) {
<               throw new underflow();
<       }
<
<       return list_head -> retrieve();
---
>       // enter your implementation here
>       return Object();

The colour is added for explanation purposes. The command diff search through the files until it finds two lines which differ. At this point, it accumulates the differences, searching forward until it finds a similarity again, and then prints the differences by stating:

The lines in the first file which differ and the corresponding lines in the second file which also differ. This is displayed in the format 84,88c86,87 which says that "lines 84 through 88 in the first file differ from lines 86 and 87 in the second. The command diff then prints the corresponding lines in the first file (preceded with a <, indicating left or the first file) and the corresponding lines in the second file (preceded by a >, indicating right or the second file).

head

The head filename command prints the first 10 lines of the given file. The number of lines displayed may be modified by providing a -n option where n is an integer.

{ecelinux:1} head -3 index.html  # first three lines of index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
{ecelinux:2}

tail

The tail filename command prints the last 10 lines of the given file. The number of lines displayed may be modified by providing a -n option where n is an integer.

{ecelinux:1} tail -5 index.html
      </div>
    </div>
<!--#include virtual="./footer" -->
  </body>
</html>
{ecelinux:2}

wc

The wc filename (word count) command prints the lines, words, and characters in the file filename. You can change what information is printed by using the options

-c
Print the number of characters.
-l
Print the number of lines.
-w
Print the number of words (separated by white space).

Filename patterns may be used, in which case, a word count will be displayed for each file together with a total. The file name is always printed in the same manner in which it was specified (absolute or relative).

{ecelinux:1} wc ~/public_html/index.html
    123     560    5701 /home/ece250/public_html/index.html
{ecelinux:2} wc -w index.html
    560 index.html
{ecelinux:3}
Contents Previous Topic Next Topic

Copyright ©2005-2008 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]