LL3 – Viewing & Searching Files

Introduction

In this lab we will use the submission process previously established (screenshots pasted into LastnameFirstnameLL3.odt with exercise titles and BB assignment upload).  We will also use the .txt file we created in the previous lab (i.e. ciss100/Username/lastnamefirstnameLL.txt).  Also, last reminder to research all of these commands in Wikipedia and through the man pages.

3.1 Viewing file contents

cat (concatenate)

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

$ cat lastnamefirstnameLL.txt # this is loobyjamesLL.txt for me

As you can see, the file is longer than the size of the window, so it scrolls past the beginning rendering it unreadable (of course if you have a large Terminal window you will see it all).

The cat command has many options and uses as it can copy a file, create a file, display line numbers or be redirected as needed.  A really good cat introduction and resource for many things Unix & Linux is here: http://www.cyberciti.biz/faq/howto-use-cat-command-in-unix-linux-shell-script/

less

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

$ less lastnamefirstnameLL.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 ten lines of a file to the screen.  First clear the screen as presented in previous labs then type

$ head lastnamefirstnameLL.txt

Then type

$ head -5 lastnamefirstnameLL.txt

What difference did the -5 do to the head command?  Now if you have me in class you know I never miss an opportunity to reinforce material so look my use of terminology identifying the “head” command, the “-5” number of lines option and the “lastnamefirstnameLL.txt” argument.

tail

The tail command writes the last ten lines of a file to the screen. Clear the screen and type

$ tail lastnamefirstnameLL.txt

Exercise 3a.

How can you view the last 15 lines of the file? Please figure this out either by researching the topic or by trial and error and when you have succeeded, please take a screenshot and paste it into the .odt file.

3.2 Searching the contents of a file

Simple searching using less

Using less you can search though a text file for a keyword (pattern). For example, to search through lastnamefirstnameLL.txt for the word ’10’, type

$ less lastnamefirstnameLL.txt

then, still in less (i.e. don’t press [q] to quit), type a forward slash [/] followed by the word to search for so in this case enter  /10

As you can see less finds and highlights the keyword. Type [n] to search for the next occurrence of the word (I threw some Linux man page syntax at you there as items enclosed in square brackets are required so [n] meant type ‘n’ and hit enter).  Now when done type [q] if you are left with a colon prompt.

Now you’re thinking why does this guy think this is important or even useful.  I mentioned that Linux server environments may not have a graphical editor for both security (users who are not comfortable in a CLI environment cannot do anything to harm the system) and efficiency (graphical user interfaces consume resources).  Also over a network, you probably only have a CLI so on a Sunday morning at 4:30 a.m. when the CEO loses her password and cannot get into the system and calls you to reset it… why CEOs get up at 4:30 a.m. on a Sunday remains a mystery but thats why they are CEOs :).  Continuing on, do you want to drive to the business and reset the password at 4:30 a.m. or do you want to stumble to your computer and do this over the network.  Now that you are at the computer do you want to search for her name in the system settings line by line?

Now to illustrate the need for CLI knowledge and digress even further – 🙂 … think about movies that show high tech hacking, what are they doing?  They are all correctly working from a CLI.  Can you expect to thwart malicious hacking if you don’t have equivalent knowledge to the hackers?

grep (don’t ask why it is called grep)

grep is one of many standard UNIX/Linux system administrator utilities. It searches files for specified words or patterns. First clear the screen, then type

$ grep testing lastnamefirstnameLL.txt

As you can see, grep has printed out each line that contains the word “testing”

Or has it????

Try typing

$ grep Testing lastnamefirstnameLL.txt

The grep command is case sensitive which makes sense because Linux is case sensitive therefore it distinguishes between Testing and testing.

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

$ grep -i testing lastnamefirstnameLL.txt

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

$ grep -i ‘est’ lastnamefirstnameLL.txt

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each maching line with the line number
-c print only the total count of matched lines

Try some of them and see the different results. Don’t forget, you can use more than one option at a time, for example, the number of lines without the words testing or Testing is

$ grep -ivc testing lastnamefirstnameLL.txt

Exercise 3b:

Please take a screenshot and paste it into the .odt document.

3.3 File Utilities

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on lastnamefirstnameLL.txt type

$ wc -w lastnamefirstnameLL.txt

To find out how many lines the file has, type

$ wc -l lastnamefirstnameLL.txt

Exercise 3c:

Please take a screenshot and paste it into the .odt document.

Summary

So how does it feel to be a budding Sys Admin – :).   As usual, please research these commands in further detail on Wikipedia and the Linux man pages.

less file similar to more but more functionality
cat file display a file
more file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file

Leave a Reply