Skip to main content Skip to navigation

3.1.4 Examining content of files

Although emacs is a really useful editor for opening and creating files, there are times when you don't actually want to open a file to look at it. If you want to check some output file that is being written to at the time of checking, you especially don't want to open a file just to look at it. In linux, there are ways to sneak a peek at a file without opening it.

1. Using cat

The first of these commands is cat . This command concatentates the contents of a file (or files) and copies these contents to the standard input (ie, your x-term) - unless you redirect otherwise. The command syntax is

cat file 1 file 2 ....etc


The user harry has two files (output1 and output2) that he would like to look at, but not open. He does this by typing
 cat output1 output2 
this pages the contents of the 2 files to standard output. To rather place these contents into another file called total , he types
 cat output1 output2 > total 

You can always place text in a file using cat in the following way:
first type
 cat > file 
then type in your text (including newlines if you wish), and follow this with Ctrl-C.

to append the contents of file1 and then file2 to the end of file3, just type
 cat file1 file2 >> file3 

Back to top

2. Using tail

sometimes when you're running a calculation, your output file is being constantly updated, with extra output being written to the end of the file. To track the progress of your output file, you can use the command tail . This command extracts the end of the file. Again, without redirects, the output of tail will go to the standard output. tail can be used with 2 types of options - with either the -f option or with sign n option, where n is a number, and sign is plus or minus.

Using the sign n option
In this case the copied portion of the file starts either n lines from the top of the file (if sign is + ) or n lines from the end of the file (if sign is - ).

Putting it into Practice:

Download this file lines.txt - it is 500 lines long, where each line is numbered.
Look at the last 10 lines of this file by typing
 tail -10 lines.txt 
Look at the last 300 lines of this file by typing
 tail +200 lines.txt 

Using the -f option
This allows continuous, real-time updating of the file as it is being written to (by your program). An example using this option for a file called output is :
 tail -f output 

There is also a useful command called head that does a similar job to tail, except it is used for grabbing and displaying the contents at start of a file. Consult the man pages about this command.

Back to top

3. Using less

Under linux, you can examine contents of a file one page at a time, using the less command. You can move down the file one page at a time using the space bar. There are many useful aspects to less , in particular the ability to search for a string in your file (both up and down the page). While less is active, by typing a forward slash (/), you can perform a downwards search for strings in your file. Similary, by typing a question mark, you can perform an upwards search for strings in your file.

Putting it into Practice:

We'll be using the file lines.txt again. To look at it, type
 less lines.txt 
try paging down a few pages by hitting the space bar a few times
Now do a forwards search for the string 461 by typing
and hitting return
This should take you to the first instance of a line that contains the number 461
Now do a backwards search for the string 51 by typing
and hitting return
This should take you to the first instance of a line that contains the number 51, in this case, line 451
Now hit the "n" key. less will take you to the next instance of a line containing the string 51. In this case, it will be line 351
Keep hitting "n" - what happens after you've reached line 51?
You'll find that hitting "n" also works in the forward search direction as well.

You should notice that the online manual pages also operate under less - so you can search man pages using / and ?.

Finally, you should be aware that there is an older version of less, called more . It is similar to less, but has less functionality.

Back to top

4. Using grep

Instead of looking at the entire contents of a file, it's useful to search for strings (patterns) in a file, without even having to use less on a file. We can search for strings in a file from the shell prompt using a command called grep . The general syntax is:
grep options pattern file

As in many other commands, you don't have to use any option - below is an example of this situation.


The user phillipa wants to search for the pattern margherita in her file called pizza.types . To do this she types
 grep margherita pizza.types

You can try a simple example for yourself:

Putting it into Practice:

We'll be using the file lines.txt again. To look for the string 51 type
 grep 51 lines.txt 
The output will give you all instances (lines) that contain this string.

You must be careful in how you format the pattern you wish to search for. The format of this pattern is known as a regular expression . There are certain characters in regular expressions that are special (since they serve another purpose) - you need to pay particular attention to the following set of special characters
. or * or / or ] appearing ANYWHERE in your pattern
^ appearing at the beginning of your pattern
$ appearing at the end of your pattern

If you need to search for a string that contains these characters, then you must precede that character with a backslash (\). Furthermore, if you need to include whitespace in your pattern, you should enclose your pattern in quotation marks.


The user harry wants to search for the pattern cheap at $5.99 in his file called bargains . To do this he types
 grep "cheap at $5\.99" bargains 
Note that the backslash is used to precede the dot, since a dot is a special character.
Note also that there is no backslash to precede the dollar sign, since it is not at the end of the pattern (and therefore not special).

There is much more to find out about regular expressions. Consult any linux textbook for a more in-depth treatment.

There are many useful options for grep . Use the man pages to find out about the -c , -v and -i options in particular - they come in very handy.

Back to top

5. Using wc

You can count the number of words, lines or characters in a file without having to open it, using the wc command. The general syntax for this command is :

wc options file

where the options are -c to count characters, -l to count lines and -w to count words. Without any option, the default is -wcl . If no file is specified , then this command will expect to count items from the standard input (ended with Crtl-C).

Putting it into Practice:

We'll be using the file lines.txt again. To count the number of lines in this file type
 wc -l lines.txt

Back to top