An Introduction To The Utilities

Utility programs allow you to work with the Linux system and manipulate the files you create.
This section describes several other file manipulation utilities that allow you to find out who is logged in,
communicate with other users, display system documentation, print files and perform other useful functions.
Some of the utilities included in this section were chosen because you can learn to use them easily and they allow
you to communicate with other people using the system. Others were chosen because they were designed to
help beginners learn the system and they are basis for some later examples.

Who is using the system?

There are several utilities that you can use to find out who is using the computer system each varying in details they
provide and the options they support. The oldest utility, who, produces a short list of user names along with the
terminal connection each person is using and the time the person logged into the system.

Two newer utilities, w, and finger, show more details (such as user's full name and the command line each user is
running). The finger utility can also be used to retrieve information about users on remote systems if your computer is attached to a local area network (LAN) or even on the Internet.

Using Who

The who utility displays a list of users currently logged in.
An example would be:

$ who

root        ttypo        Aug.    11    08:47
 
 

The first column shows root is currently logged in. The second column shows the designation of the terminal that root
is using. The third column shows the date and time root is logged in. To find out which terminal you are using, or to
see what time you logged in, give the command who am i.

Using Finger

You can use the finger utility to display a list of people who are currently using the system. In addition to login names,
finger supplies each user's full name, along with the information about which terminal line the person is using,
how recently the user typed something on the keyboard, when the user logged in, and information about where the user is located (if the terminal line appears in a system database). If the user has logged in over a network, the name of the remote
system is also shown.

You can change your own finger information using the chfn command. If you want to have your plans and projects
displayed along with the finger information, create a file on your home directory, .plan and .project. Any text entered into these files will be shown as your plan and project, respectively, on your finger information.

an example for the finger utility would be:

$ finger

Login        Name                TTY        Idle       When            Where

jam        Jamie Desiderio    *p0            1        Wed    8:30    croftj
 

The above example shows that Jamie's terminal (TTY) line indicates that she has blocked others from sending
messages directly to her terminal. ( You will find out more about this using the mesg utility).

You can also use the finger command to learn more about a particular individual by specifying more information on the
command line.
 

$ finger jam

Login Name : Jam                    In real life: Jamie Desiderio
Directory: /home/jam               Shell: /bin/csh
On since Aug. 11 9:15:37 on tty06
New Mail received Wed Aug. 11 9:13:14 2000;
    Unread since Wed Aug. 11 9:01:40 2000
Plan:
For appointments, contact jam@linuxlots.com

.
.
.

The above example shows that Jamie is currently logged in and actively using her terminal (if she were not, finger would
report how long she had been idle), some new mail has arrived for Jamie that she has not yet read, and if you want to set up a meeting with her, you should email her.  Most of these information collected by finger was from the system files. The information shown after the heading Plan:, however was supplied by Jamie.

The finger utility searched for a file named .plan in Jamie's home directory and displayed the contents. You may find it helpful
to create a .plan file for yourself. It can contain any information you choose, such as your typical schedule, interests,
phone numbers, addresses, etc. You can also use .project file the same way with the .plan file. Finger will attempt to read the .project file right after it has read the .plan file.

If Jamie had not been logged in, the finger utility would have reported the last time he had used the system. If you do not
know a person's login name, you can use the finger utility to learn it. For example you might not know that Jamie's family name is Desiderio, but you might not guess that her login name is Jam.

Using W

The W utility displays a list of the users currently logged in. On some other systems, W may not be installed.
An example for the W utility would be:

$ w
    9:19 am    up 15 days, 12:24,     4 users,     load average:0.15, 0.05, 0.01

User        TTY        Login@        Idle        JCPU        PCPU        what
root        ttyp0        8:34AM        1            11                1                vi .profile
jam        ttyp03        Tue 3 PM    23:48        16                1                -sh
 

The above example shows the first line that w utility displays us the same as that provided bu the uptime command.
This report included the current day, how long the computer has been running, how many users are logged in, and how busy the system is. The three load average numbers represent the number of jobs waiting to run, averaged over the past minute and 15 minutes.

The who utility displays a report similar to that produced by w, but it includes less information. If the person is logged in and recently active, you can use talk or write (more on this below) to establish communication with someone immediately. If w does not show that the person is logged in, or if you do not need to communicate immediately, you can send that person a Linux mail.
 

Using talk to Communicate With Another User

You can use the talk utility to carry on a two-way conversation with another person who is logged in on your system. If your system is connected to a network, you can also use talk to communicate with someone on a different computer. When you sue talk,  your display screen will be split into two sections; once you establish contact with the other person, the messages you type will appear in the top half of your screen and the message from the other person will be displayed in the bottom half. In this example, Jamie need some information from root.

$ talk root

Jamie's display is immediately spilt into two sections, and the following message appears at the top of the screen:

[ Waiting for your party to respond ]

Meanwhile, this message appears on root's screen, and she responds:

    Message from Talk_Deamon@croftj at 9:22 ...
        talk: connection requested by jam@croftj
        talk: respond with: talk jam@croftj

$ talk jam@croftj

Jam and root are both using a computer named croftj; jam@croftj is Jamie's network address, which will be described in more detail later.

To  end the talk session, one person interrupts by pressing delete  or control c and the following message appears before a new shell prompt is displayed:

[ Connection closing.  Exiting ]

If you see the following message when you try to use the  talk   to reach someone, he/she has used the mesg  to block out interruptions:

[ Your party is refusing messages ]

Before the talk utility was available, people on Unix systems used the write command to interact with each other on the same computer. The talk utility has a few advantages over write: with talk, the other other person's messages appear on your screen letter by letter as they are typed; write, on the other hand, sends only a whole line at a time. if you use write, sometimes you are not sure whether the other person is still connected at the other end (or just a slow typist). Also, unlike write, talk has been extended to support communication over the network. Still, if you need to exchange a quick message with another person logged in on your system, without disrupting your current screen display, you may use the write command instead.
 

Using write to Send a Message

You can use the write utility to send a message to another user who is logged in. When the other user also uses write to send you a message, you establish a two-way communication.

When you give a write command, it displays a banner in the other user's terminal saying that you are about to send a message. The format of a write command line is as follows: write destination user [terminal]

$ write jam
Hi Jamie, are you there?
    Message from jam on croftj (ti) [Mon. May 23 15:08] ...
Yes root I am here.
 

The destination user is the login name of the user that you want to communicate with. You can find out the login names of the users who are logged in by using the who utility.

If the person you want to write to is logged in on more than one terminal, you can direct write to send your message to a specific terminal by including the terminal designation. Replace terminal on your command line with the terminal designation who  displays. Do not enter the square brackets--they just indicate that  the terminal part of the command is optional.

To establish a two-way communication with another user, you and the other user must each execute write specifying the other's login name as the destination user. The write utility then copies text, line by line, from one terminal to the other. When you want to stop communicating with the other user, type control-D at the beginning of a line. Pressing control-D tells write to quit, displays EOT (end of transmission) on the other user's terminal, and returns you to the shell. The other user must do the same.

It is helpful to establish a protocol for carrying out on communication using write.  Try ending each message with o (for over) and ending the transmission with oo (for over and out). This protocol gives each user time to think and to enter a complete message, without the other user wondering if the first user is finished. because the write copies one line at a time, if you write several short lines of text rather than one long time, the other user will be reassured that you are still there.

$ write jam
Hi jamie, are you there? o
    Message from Jam on croftj (tty1) [Mon May 23 15:08]...
Yes Root, I am here. o
.
.
Thank you Jamie. Bye. oo
control-D
$Bye, root oo
<EOT>
(return)
$

The following example shows how one side of a two-way communication using write appears to root. It shows root initiating communication by calling the write command and specifying jam as the destination user. Root enters a message terminated by o and then waits for a reply.

As soon as jam has the chance to respond and execute write, the utility sends a banner to receive root's message.

Following the protocol that jan and root have established, jam terminates her message with o.

At this point, root and jam can communicate back and forth. Each tome one of them types a line and presses return, the line appears on the other's terminal. When they are done, root enters a final message terminated by oo and presses the control-D (as the first and the only thing on a line) to sign off. The shell then appears. Then jam signs off, and root sees the EOT that results from jam pressing the  control-D.

Jam's final message appears after root's shell prompt. because root did not give any commands, jam can display another shell prompt by simply pressing the return key.

Throughout this communication, jam and root followed the convention of using the o after each message. This is just a convention and is not recognize by write. You can use any convention you please or none at all.
 

Using mesg to Deny or Accept Messages

If you do not want to receive messages at all, you can give the following command:

$ mesg n

After giving this command, another user cannot send you messages using write or talk.

If  jam had given the preceding command before root tried to send him a message, she would have seen the following:

$ write alex
Permission Denied.

You can allow messages again by entering mesg y.

If you want to know if someone can write to you, give the command mesg by itself. The mesg utility will respond with a y for yes or n for no.
 

Using mailx to Send and Receive E-mail

You can use the mail utility programs to send and receive electronic mail. Electronic mail (or the e-mail) is similar to post office mail except that it is quicker and does not involve any paper or stamps. You can use it ti send and receive letters, memos, reminders, invitations, and even junk mail.

You can use electronic mail to communicate with users on your system and, if your installation is part of a network, with other users on the network. Many Unix/Linux system are attached to networks that allow worldwide communication as in the Internet.

The mail utilities differ from the write utility. Although the mail utilities allow you to send a message to a user whether or not that the user is logged in on the system, write allows you to send message only if the user is logged in and willing to receive messages.

There are several mail utilities available on a Unix/Linux, and their names can lead to confusion. The original mail utility called mail had been limited to functionality and a primitive user interface. The most popular mail program supplied with Unix/Linux systems today was created at Berkeley, and is known as Mail  (with a capital M). This section describes mailx which is patterned on Mail.

The Linux operating system mail utilities have two distinct functions: sending and receiving messages. The following example demonstrates these two functions. Try it on your system, replacing root with your login name. First, root  uses mailx to send himself a message:

$ mailx root
Subject: test message
This is a test message that i am sending myself.
Control-D
$

After he starts mailx by typing mailx root, root is prompted to enter the subject of his message. On your system, mailx may or may not not prompt you for a subject, depending on how it is set up. After he types in the subject (test message) and presses return, root types his message. he ends each line with a return and when he is finished, he enters a control-D on a line by itself. The mailx utility sends the message and returns root to the shell prompt.

Sometimes mailx is set up to prompt you for a copy to list after your press control-D. At that time, you can enter Th. login names of users who should receive copies of the message. After you finish the list and presses return, the mailx utility will send the mail and return you to the shell.

To read his mail, root can run mailx without a user name. The mailx utility displays two line of information about mailx that include the version number of the mailx program he is using, instructions for getting help, the mailbox that is being read, and the number of messages. The string /var/mail/root identifies the location of root's mailbox in the hierarchical filesystem (more on this later). Following the header information is a list of any messages that are waiting for him. The list includes a header for each message, composed of the status of the message (N means new), the name of the person who sent it, the date it was sent, its length (the number of lines/number of characters), and the subject of the message.

$ mailx
mailx version 4.0        Type ? for help.
"/var/mail/root": 2 messages  2 new
> N 1 root                Wed Aug. 10 10:49    6/120    test message
   N 2 jam                 Wed Aug. 10 10:50    6/115    lunch?

Root has two messages, the one it just sent and one from jam. The greater than sign to the left of the first header indicates that the message from root is the current message. After the list of headers, mailx displays a prompt usually a question mark or ampersand indicating  that it is waiting for root to give a command. Root can simply press the return key to look at the current message. Root could have used the t for type command with a message number argument such as t 1  to print a specific message. The t command used without an argument always prints the current message as does return itself.

? (return)
Message 1:
From root Wed Aug. 10 10:49 PDT 1994
To: root
Subject: test message
Status: R

This is a test message that I am sending myself.

? d (return)

The first four lines of the message are header lines. They list who the message is from, who it is to, the subject, and the status. depending on how your system is setup, and depending on whether the message was sent to you from your machine or from another machine, the header may be as simple as the one above or much more complicated. In any case, the To, From, Subject lines are the ones you are interested.

After reading his message, root uses a command followed by a return to delete the message. If root did not delete the message, mailx would have saved the message in a file named mbox in root's home directory.

As the first line of the mailx display indicates (type ? for help), mailx has built in help feature. Root can enter ? in response to the mailx prompt (?) to get a list of other commands he can use.

After root pressed d and return, mailx displays the mail prompt. Root types 2 then return to see message no. 2.

? 2 (return)
Message 2:
From Jam Wed Aug. 10 10:50 PDT 1994
To: root
Subject: Lunch?
Status: R

Root, can you meet me for lunch today at noon?

? (return)
At EOF
? q (return)
Saved 1 message in /home/root/mbox
 

In response to this message, root presses the return. because he has no more messages, the mailx utility displays At EOF indicating that it is at the end of the file and that one message has been saved in the mailbox file. If there were any message left in his mailbox that root had not looked at, they would be there for him to read the next time he read his mail.

You can send mail to more than one person at a time. Below, jam sends a reminder to root, admin, and operator. The characters :-) in the message represent a smiley face. because it can be difficult to tell when the writer of an electronic message is saying something in jest or in a humorously sarcastic way, electronic mail users often uses smilleys.

$ mailx root admin operator
Subject: Meeting reminder
Please remember to bring your notes from our last meeting to the meeting on Friday at 9:00 am
in my office.

And do not forget the doughnuts :-) ...

jam
Control -D
$

When root, admin, and operator log in, the system will tell them that they have mail.

If your system is a part of a network, you can send mail to and receive mail from users on other systems. If you are on Local Area Network, you can send mail to a user on a system that is linked to yours by preceding the user's name with the name of the remote system and an exclamation point (which is commonly refereed to as bang) If your system is connected to a wide area network such as the Internet, you need to proceed the user's name with @ followed by the domain name server.

The following examples shows you how to send mail to remote system on a Local Area Network.

$ mailx croftj!jam

If you are using the C shell, you must quote the exclamation point by preceding it with a backslash:

% mailx croftj\!jam

You can also send mail to a user on a system that is not directly networked to your computer as long as your system has a link to a system that in turn has a direct or indirect link to the destination systems. Your system may have a database that specifies how to reach remote systems, or it may use a name service if it is attached to a local area network, you will need to specify the path your message should take by listing all the systems in order on the command line. For example, if you have a login on a system that is connected to the Internet, you can send the author the following command. The @symbol is a part of the address that the Internet uses.

$ mailx thomas!jam@linuxlots.com

To deliver the message to the author using the address, your system will first contact the system named croftj.net over the Internet That system in turn recognizes that the message needs to be passed along to the system named thomas using the local area network the two connected to each other. When the message arrives on thomas, it will be delivered to jam's mailbox. You can send directly to the a user by:

$ mailx jam@linuxlots.com

This time, it doesn't have to pass along any third party system.

You can also mix the names of users on your system and on the other system on one command line.

$ mailx root jam linuxlots.com!admin operator
 

Incoming mail notification

If root checks his mail after having already looked at all his messages, he will see the following:

$ mailx
No mail for root.

However, you do not have to mailx to find out whether you have mail. At specified intervals (usually every after 10 mins) while you are logged in, the shell checks to see whether new mail has arrived since the last time you read your mail. If new mail has arrived, the shell presents the following message before the next prompt:

You have new mail.
$
 

Using man to Display the System Manual

The man (manual utility) displays pages from the system documentation on the terminal. This documentation is useful if you do not know what utility you want to use but have forgotten exactly how to use it. Because the descriptions in the system documentation are often quite terse, they are most helpful if you already understand basically what a utility does. If a utility is new to you, the descriptions provided are typically easy to understand.

To find out more about a utility, give the command man followed by the name of the utility. The following command displays information about the who utility. If the information man displays runs off the top of the screen, give the second form of the command, which uses a pipe (the | symbol--pipes are explained later) and pg to cause the output to pause after each screenful.

$ man who

or

$ man who | pg

You can use the command man man (or man man | pg ) to find out more about the man utility itself.

When you use the preceding format for man, the utility displays a  prompt at the bottom of the screen after each screenful of text and waits for you to request another screenful. When you press the (return) pg displays a new screenful of information about the man utility. Pressing (delete) stops man and gives you a shell prompt.
 

Organization of the System Manual

Traditionally, the Unix system (where Linux is based upon)  manual has been divided into eight sections that describe related tools:
 
  • 1 user command
  • 2 system calls
  • 3 c library
  • 4 devices and network interfaces
  • 5 file formats
  • 6 games and demos
  • 7 environments, tables, and troff macros
  • 8 system maintenance

  •  

     
     
     
     
     
     
     
     
     

    Most users find the information they need in sections 1,6, and 7; programmers and system administrators frequently need to consult the other sections. In some cases, you will find that there are manual entries for different tools with the same name. For example, if you type the following command, you will see more than one manual page:

    $ man write

    If you wanted to see just the manual page for the write utility, you would enter

    $ man 1 write

    The manual page for the user command write is contained in section 1. To see the manual page for the write system call from section 2, you would run:

    $ man 2 write

    If you do not know the name of the command you need to carry out a particular task, you can use the man utility to search for it, using a keyboard. For example, to find a utility that you can use to sort for a list of words, use the -k (keyboard) option.

    $ man -k sort
     

    Using Echo to Display Text on the Terminal

    The echo utility copies to the terminal anything you put on the command line after echo. The echo utility is a good tool for learning about the shell and other UNIX/LINUX programs.

    $ echo Hi
    Hi

    $ echo This is a sentence.
    This is a sentence.
     

    Using cp to Copy a File

    The cp uitlity makes a copy of a file. it can copy any file, including text and executable program files. Among other uses, you can use cp to make a backup copy of a file or a copy to experiment with.

    A cp command line specifies source and destination files. The format is as follows:

    cp source-file-destination file

    The source-file is the name of the file that cp is going to copy. The destination-file is the name that cp assigns to the resulting copy of the file.

    CAUTION:
                        If the destination-file exists before you give a cp command, cp overwrites it. Because cp overwrites (and destroys the contents of) an existing destination file without warning you, you must take care not to cause cp to overwrite a file that you need.

    The following command line makes a copy of the file named output. The copy is named outputs. The initial ls command below shows that output is the only file in the directory. After cp command, the second ls shows both files, output and output in the directory.

    $ ls
    output

    $ cp output outputb

    $ ls
    output outputb

    Sometimes, it is useful to incorporate the date in the name of a copy of a file. In the following example, the period is part of the filename--just another character:

    $ cp memo memo.0310

    Although the date has no significance to Linux, it can help you to find a version of a file that you saved on a certain date. It can also help you avoid overwriting existing files by providing a unique filename each day.
     

    Using mv to Change the Name of a File

    If you want to rename a file without making a duplicate copy of it, you can use the mb (move) utility. An mv command line specifies an existing file and a new filename. The format is as follows:

    mv existing-file new-file

    The following command line changes the name of the file memo to memo.0130. The initial ls command below shows that memo is the only file in the current directory. Following the mv command, memo.0130 is the only file in the directory.

    $ ls
    memo

    $ mv memo memo.0310

    $ ls
    memo.0310
     

    Using lp to Print a File

    So that several people or jobs can use s single printer, the Linux system provides a means for querying a printer output so that only one job gets printed at a time. The lp (line printer) utility places a file in the printer queque for printing.

    The following command line prints the file named report:

    $ lp report
    request id is printer_1-450 (1 file)

    The lp utility may display a line of information that contains a request number each time you ask it to print a file (the lines that begin with the word request in the examples). You can use the these request numbers to check on the progress of or cancel a printing job.

    You can send more than one file to the printer with a single command line. The following command line prints three files:

    $ lp memo letter test
    request id is printer_1-451 (3 files)
     

    Using grep to Find a String

    The grep (Global regular Expression Print) utitlity searches through the a file to see if it contains a specified string of characters. This utility does not change the file it searches through but displays each line that contains the string.

    Example:

    $ cat memo
    Jamie

    In our meeting on June 6th, we discussed the issue of credit. Have you had any further thoughts about it?

                    Root

    $ grep 'credit' memo
    discussed the issue of credit.
     

    The grep command above searches though the file memo for the lines that contain the string credit, and displays a single line.

    If memo contained words like discredit, creditor, and accreditation, grep would have displayed those lines as well because they contain the string it was searching for.

    You do not need to enclose in a single quotation marks the string you are searching for, but doing so allows you to put spaces and special characters in the search string.
     

    Using head to Look at the Top of a File

    The head utility displays the first ten lines of a file. It is useful for reminding yourself about what a particular file contains. If you have a file named months that contains the twelve months of the year in order, head will display January through October.

    $ cat months
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec

    $ head months
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct

    The head utility can display any number of lines so you can use it to look ar only the first line or at a screenful or more. To specify the number of lines head will display, include a hyphen followed by the number of lines in the head command. For example, the following command displays only the first line of months.

    $ head -1 months
    Jan

    If the head utility is not available on your system, you can use the sed command to look ar the top of the file. The following command displays the first ten lines of the file months.

    $ sed 10q months

    The first argument to sed tells sed to display the ten lines and quit. You can specify any number of lines for sed to display. The next command displays only the first line of months.
     

    Using tail to Look at the End of a File

    In opposite to head, the tail  command displays the the last ten lines of the file. The command syntax is the same as that of head.

    $ cat months
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec

    $ tail months
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Sep
    Oct
    Nov
    Dec

    Using sort to Display a File in Order.

    The sort  command displays the contents of a file in order by lines. If you have a file named days that contains the names of each of the days of the week on separate lines, sort displays the file in alphabetical order. The sort utility is useful for putting lists in order. Within certain limits, sort can be used to order a list of numbers.
     

    Using uniq to Remove Duplicate Lines in a File

    The uniq utility displays a file, skipping adjacent duplicate lines. if the file names phone_list has two successive entries for the same person, uniq skips the extra lines.

    If the file has been sorted before uniq  is used,  uniq ensures that the two lines of a file are the same.

    $ cat Phone_list
    Alex    856-4362
    Jenny    451-7339
    Jenny    451-7339
    Barbara    328-3078
    Helen    249-0348
    Jenny    451-7339

    $ uniq Phone_list
    Alex    856-3462
    Jenny    451-7339
    Barbara    328-3078
    Helen    249-0348
    Jenny    451-7339
     

    Using whereis and which to Find Utilities

    When you type the name of a utility on the command line, the shell searches a list of directories for the program and runs it. This list of directories is called a search path and you will learn how to change it later when you learn about the shell. If you do not change the list, the shell will search a standard set of directories. There may be other directories on your system that contain useful utilities.

    There are two utilities that you can use to find the full path name associated with a command, whereis and  which. To find a particular tool, you can use the  whereis utility. This utility is a helpful if you have heard of a command and think it may be available on your system but you do not know where it is stored. In the next example, user wants to ping croftj.net but the shell does not find it in any of the standard places. (The ping utility uses a network connection to communicate with a remote system).

    $ ping croftj.net
    ping:    not found

    $ whereis ping
    ping:   /usr/sbin/ping    /usr/man/manlm/ping.lm

    In addition to locating the command, /usr/sbin/ping, whereis reporst the location of the manual page (/usr/man/manlm/ping.lm). If the C source code for the command is available on your system, whereis will report where it is stored as well. Now that you know th elocation of the ping command, you can run it directly by using its full pathname.

    $ /usr/sbin/ping croftj.net
    Croftj.net is alive

    Another useful utility is the bwhich  command. There may be multiple commands on your system that have the same name (such as mail). When you type the name of a command, the shell examines your search path in the order you have specified and runs the first one it finds. You can find out which copy of the program the shell will run using the which command. In the following example, which reports the location of the cat command.

    $ which cat
    /usr/bin/cat

    The  which  utility can be very healpful when a command seems to be working in unexpected ways. By running which, you may discover that are running a non standard version of a tool or a different one than you expected. For example, if you find that you are running /usr/local/bin/cat  instead of /usr/bin/cat, you might suspect that the local version is broken.

    Using diff to Compare Two Files

    The diff command compares two files and displays a list of the differences between them. This utility does not change either file; it just displays a list of the actions you would need to take to convert one file into the other. This is useful if you want to compare two versions of a letter or report, or two versions of the source code for a program.

    The diff utility produces a series of lines  containing instructions to add (a), delete (d), or change (c) followed by lines that you need to add, delete, or change. If you have two files called colors.1 and colors.2 that contain names of colors, diff compares the two files and siaplys a list of their differences.

    The diff  utility assumes that you want to convert the first file (colors.1) into the second file (colors.2). The first line that diff displays (4d3) indicates that you do not need to delete the fourth line. You can ignore the number following the d since it is only important if you want to convert the second file into the first. The next line of the display shows the line to be deleted; the less than symbol indicates thet the line is from the first file. A greater than symbol is used to identify lines that are from the second file.

    $ cat colors.1
    red
    blue
    green
    yellow

    $ cat colors.2
    red
    blue
    green

    $ diff colors.1 colors.2
    4d3
    < yellow
     

    Using compress to Shrink Files

    Large files can use a lot of fisk space quickly and they are slow to transfer from system to another over a network. If you will not need to look at the contents of a large file very often, you may want to save it on a magnetic tape and remove it from the disk. If you have a continuing need for the file, however, retrieveing a copy from the tape is inconvenient. There is another way to reduce the amount of disk space you use without removing the file entirely. You can compress (shrink) the file without losing any of the information.

    The compress utility examines the contents of a file, searching for a repeated patterns. Once it has analyzed a file, compress applies a formula recodes the file more efficiently. The new version of the file looks completely different and is usually smaller. In fact, the new file will contain many non-printable characters, so you do not want to try to read it directly. The compress utility works well on large files that contain many repeated patters especially text files. On many systems, the electronic copy of the system manual is stored in compressed form.

    The following example uses a boring file: each os the 8,000 line of this file, letter_e, contains 72 letter e's. The file occupies more than half a megabyte of disk storage.

    $ ls -1
    -rw -r --r --    1 jam    pubs 576000 Oct 15 10:57 letter_e

    The -v (verbose) option below causes compress to report how much it was able to reduce the size if the file; In this case, by more than 99%!

    $ compress -v letter_e
    letter_e: compression: 99.27% -- replaced with the letter_e.Z

    $ ls -l
    -rw -r --r --    1 jam    pubs 10113 Oct 15 10:57 letter_e.Z

    The compress utililty also renamed the file --it appended .Z to the file's name.This naming convention helps to remind you that the file is compressed; you would not want to display or print it, for example, without first uncompressing it. The compress utuility did not change the modification date associated with it even though it has completely changed the file's contents.

    You can use the zcat utility to display the file. The zcat utility is the equivalent of  cat for the .Z file; unlike cat , zcat interprets the compressed data and displays the contents of the file as though it were not compressed.

    $ zcat letter_e.Z | head -2
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

    After running zcat, the contents of letter_e.Z are unchanged--the file is still stored on the disk in compressed form. If you need to restore the file to it's original size and form, use the uncompress utility as shown below:

    $ uncompress letter_e.Z
    $ ls -l
    -rw -r --r--    1 jam    pubs    576000 Oct 15 10:57 letter_e
     
     

    This Website is hosted by :

    Back to the main page