Viewing and Searching Files

Note: These tutorials assume you have a copy of the tutorial.txt file, which can be downloaded from the link below and saved in your Unix home area on the U: Drive. (Right-click > Save As).tutorial.txt
Text Document • 1.07 KB

Viewing filescat

cat (concatenate)

The cat command can be used to display the contents of a file on the screen. Type the following:

% cat tutorial.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.

less

The less command writes the contents of a file onto the screen a page at a time. Type the following:

% less tutorial.txt

Press the space bar if you want to see another page, type q if you want to quit reading. As you can see, less is used in preference to cat for long files.

head

The head command writes the first 10 lines of a file to the screen. Clear the screen first, then type the following:

% head tutorial.txt

Then type the following to write the first 5 lines out:

% head -5 tutorial.txt

tail

The tail command writes the last 10 lines of a file to the screen. Clear the screen first, then type the following:

% tail tutorial.txt

How can you view the last 15 lines of the file?

Searching through filesSimple searching using less

Using the less command, allows you to search though a text file for a keyword (pattern). For example, to search through tutorial.txt for the word tutorial, type the following:

% less tutorial.txt

then, still in less (i.e. don’t press q to quit), type a forward slash followed by the word to search:

/tutorial

As you can see, /tutorial finds and highlights the keyword. Type n to search for the next occurrence of the word.

grep

The grep command is one of many standard Unix utilities. It searches files for specified words or patterns. Clear the screen first, then type the following: % grep tutorial tutorial.txt

As you can see, grep has printed out each line containing the word tutorial. Try typing the following:

% grep tutorial tutorial.txt

The grep command is case sensitive. Therefore it distinguishes between ‘tutorial’ and ‘Tutorial’.

To ignore the upper/lower case distinctions, use the -i option, i.e. type the following:

% grep -i tutorial tutorial.txt

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type the following:

% grep -i ‘spinning top’ tutorial.txt

Some of the other options of grep are as follows:
-v display those lines that do NOT match

-n precede each matching line with the line number

-c print only the total count of matched lines

wc

wc (word count)

The wc command is a handy little utility, which is short for word count. To do a word count on tutorial.txt, type the following:

% wc -w tutorial.txt

To find out how many lines the file has, type the following:

% wc -l tutorial.txt

Summary

man command – view the man pages (help files)

checkquota – shows how much disk space you have left.

clear – clears the screen

uname – prints name of current system