[StuCo 98008]
GNU/Linux for Beginners

Session 3

The importance of text files in GNU/Linux

Handling files and folders

Important console tools

By the end of this lecture you will know

How important simple text files are

How to handle files and directories from the console

How to use the vi editor

The power of GNU textutils

Text Files Control Everything

Configuration options are saved in simple text files.

Device drivers (/etc/modules.conf)

Server options (/etc/sendmail.conf)

Application defaults (/etc/cdrecord.conf)

Why?

Easy to handle, simple, well-known format (ASCII)

Distributed vs centralized storage

Distributed information under /etc

Centralized information (M$ Windows registry)

Reading Text Files from the Console

cat <filename> : dumps contents of file to screen

Useful for quickly viewing small files

less <filename> : view a text file interactively

Scroll up/down, search for text, etc.

vi <filename> : open a file for editing

Using vi

By default, the editor starts in command mode.

Hit <I> to enter insert mode.

Insert text

Delete text

Hit <ESC> to return to command mode.

Issue commands that affect the entire document

vi commands

:wq (write changes to file and quit)

:q! (quit without saving changes)

:set nu (sets numbering of lines on)

yy (yank/copy the current line)

p (paste the copied line(s)) right below the cursor

Anything potentially harmful (e.g. quitting without saving changes, writing to a read-only file), has to be forced with an exclamation mark (!)

Copying Files and Directories

cp <source> <destination>

cp -R <source_folder> <destination>

Moving Files and Directories

mv <source> <destination>

This works for single files and entire directories alike, no need for the -R flag.

If destination doesn't exist, mv practically functions as “rename”.

Finding Files with find

Is there a file or directory called setup.log anywhere under my current directory?

Is there anything ending in .conf under /usr?

find is a real-time tool:

it searches the filesystem only when the user looks for something.

Finding Files with locate

Get me all files or directories matching the string “document”

$ locate document

Locate uses a pre-existing indexed database

cron invokes updatedb daily to update the index of files and directories found attached to the filesystem

Therefore, if you have just created a file, or downloaded a program, locate will not find it!

Searching the contents of text files

To search for the string “program” in a file

$ grep program /usr/share/doc/whois/README

Useful grep switches (parameters)

$ grep -R : search recursively the entire directory

$ grep -n : show the line number of any matches

$ grep -A 5 : show me 5 lines AFTER the match

$ grep -f <file> : read search patterns from <file>

$ grep -v : do not show matching lines

Handling fields in text files

Get only the first field of your password file

$ cut -d : -f 1 /etc/passwd

This tokenizes the file /etc/passwd using : as the delimiter (-d :), and returns the first field (-f 1)

When the delimiter is not easy to establish, use awk

$ awk '{print $2}' <file>

This returns the second token of each line of <file>

Finding unique fields, sorting, counting

sort <filename>

Alphanumeric sort (both characters and numbers)

uniq finds lines that are not repeated in a file

only works on sorted files

$ uniq sorted_file

wc counts characters, words, or lines of a file

$ wc -c <file>

$ wc -w <file>

$ wc -l <file>

Output redirection

Output is by default displayed on screen

$ ls -la

$ cat <filename>

We can redirect it to a file with the > symbol

$ ls -la > output

$ cat output > output.2

Pipes

With a pipe ( | ) we can feed the output of a tool as input to another.

$ ls -la | less

$ cat <filename>

We can redirect it to a file with the > symbol

$ ls -la > output

$ cat output > output.2

Lab

Write a command sequence that writes your current IP to the file ip

Given the files roster and addresses, answer the following questions:

How many SCS students are currently enrolled? (F03)

What are their street addresses?

Do any of the enrolled students live outside Pittsburgh?

As per

http://support.microsoft.com/support/kb/articles/Q256/9/86.ASP

The Microsoft Computer Dictionary, Fifth Edition, defines the registry as:

A central hierarchical database used in Microsoft Windows 9x, Windows CE, Windows NT, and Windows 2000 used to store information necessary to configure the system for one or more users, applications and hardware devices.

The Registry contains information that Windows continually references during operation, such as profiles for each user, the applications installed on the computer and the types of documents that each can create, property sheet settings for folders and application icons, what hardware exists on the system, and which ports are being used.

The Registry replaces most of the text-based .ini files used in Windows 3.x and MS-DOS configuration files, such as the Autoexec.bat and Config.sys. Although the Registry is common to several Windows platforms, there are some differences among them.

Registry data is stored in binary files.

cat is an extremely versatile tool. Check out http://www-106.ibm.com/developerworks/linux/library/l-tiptex2.html for a few examples of creative “cat” usage.

tuxfiles.org also has a cat/less reference page.

less has been christened this way as a mockery of “more”, a traditional UNIX file viewer. The problem with “more” is that it could only increment the screen by a full page down the document. No going back up, no line-by-line scroll. So someone decided to scratch his itch, and write a better "more".

Why VI?

Because it can be found on any UNIX derivative OS created in the last 15 years (at least).

Because it's very stable and trustworthy. In case of a system crash, power failure, etc, VI saves backup files that can be retrieved by starting VI in recovery mode (with vi -r <filename>)

Because it does syntax highlighting, automatic indentation, line numbering, has shell-escape features (you can execute other tools from within VI), etc, etc.

For more info on vi, look up its reference card:

http://www.google.com/search?q=vi%20reference%20card

# cp /home/alex/.bashrc /home/ddrew

is the same as

# cp /home/alex/.bashrc /home/ddrew/.bashrc

(given enough permissions)

cp -R means “copy everything under the source folder recursively”.

So, to copy the entire /var/log/ directory under /home/alex/, you would say:

# cp -R /var/log /home/alex

The new copy of /var/log would then be stored under /home/alex/log/

Running “find” on a directory that is very high on the filesystem tree (say, /usr for example), can take a long while, since that directory may very well contain thousands of files and folders in it)

Running “locate” is recommended before trying the more exhaustive, but relatively slow, find.

Most modern distributions run updatedb through cron (the job execution scheduler) daily.

Sometimes you suddenly listen your hard disk spinning wildly for a couple of minutes, and then it stops. Your computer hasn't gone mad – it's updatedb doing its daily chores.

An outdated index is practically useless, since it will not reflect the real contents of your filesystem. So running updatedb at least weekly is recommended.

grep is an extremely powerful tool. If you remember something mentioned in a file, but don't remember what that file was called, or where it was, you can search entire directories (recursively) for a string.

$ grep -R “I seem to forget” *

...will search all contents of the current directory, and its sub-directories, for files that contain the string “I seem to forget”

awk is a full-blown text processing interpreted programming language.

http://www.gnu.org/manual/gawk-3.1.1/gawk.html if you feel like scratching more than the surface

The pipe embodies the entire philosophy of UNIX “tools” design. Each tool is there to do one task, and do it well (efficiently, reliably). Accomplishing complex tasks is handled by chaining together many tools, that must all be able to belong to a chain of pipes.

Session 3 -Console Tools

[StuCo 98008] Introduction to GNU/Linux

gnu linux console tools grep cat ls pipe

This lecture provides an overview of the console (text-based) tools of GNU/Linux. Anything can be done from the console, without the GUI that X provides, but most people don't realize that.
Since the most important skills seem to be handling files, directories and editing text files, we will go over the tools that allow one to perform these actions:
+ cp (-a)
+ mv
+ rm (-rf)
+ mkdir (-p)
+ less
+ cat
+ vi
+ grep
+ cut
+ sort
+ uniq
+ the pipe ( | )
More advanced console tools and shell scripting will be covered in the System Administration section.

2003-09-11T14:09:36