Tải bản đầy đủ (.pdf) (68 trang)

Sun Fundamentals of Solaris 7 Student Guide With Instructor Notes phần 8 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (360.98 KB, 68 trang )

14
14-30 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The sort Command
Using sort on Different Fields Within a File
The first example represents beginning a sort on some field other than
the first, and shows a numeric, reverse-order example. The sort
command line would read as, “Do a reverse order, numeric sort on the
fifth field of the data in the file list, and place the output into a file
called num.list.”
The second example represents a multilevel sort showing how the
M option would work. This command would read as “Do a Month
order sort beginning with the sixth field. Do a second-level sort in
numeric order and begin on the seventh field (this will sort the
numeric day of the month correctly), name the output file
update.list.”
$ ls -ld f* > list
$ cat list
-rw 1 melissao staff 218 Jul 15 16:47 feathers
-rw 1 melissao staff 218 Jul 15 16:48 feathers_6
-rw 1 melissao staff 1696 Oct 19 1998 file1
-rw 1 melissao staff 105 Oct 19 1998 file2
-rw 1 melissao staff 218 Oct 19 1998 file3
-rw 1 melissao staff 56 Mar 7 09:52 fruit
-rw 1 melissao staff 57 Mar 7 09:53 fruit2
$ sort -rn +4 list -o num.list
$ cat num.list
-rw 1 melissao staff 1696 Oct 19 1998 file1
-rw 1 melissao staff 218 Oct 19 1998 file3
-rw 1 melissao staff 218 Jul 15 16:48 feathers_6
-rw 1 melissao staff 218 Jul 15 16:47 feathers


-rw 1 melissao staff 105 Oct 19 1998 file2
-rw 1 melissao staff 57 Mar 7 09:53 fruit2
-rw 1 melissao staff 56 Mar 7 09:52 fruit
$ sort +5M +6n list -o update.list
$ cat update.list
-rw 1 melissao staff 56 Mar 7 09:52 fruit
-rw 1 melissao staff 57 Mar 7 09:53 fruit2
-rw 1 melissao staff 218 Jul 15 16:47 feathers
-rw 1 melissao staff 218 Jul 15 16:48 feathers_6
-rw 1 melissao staff 105 Oct 19 1998 file2
-rw 1 melissao staff 218 Oct 19 1998 file3
-rw 1 melissao staff 1696 Oct 19 1998 file1
14
File and User Information Utilities 14-31
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Exercise objective – In this exercise you will practice using
commands to find files and text.
Tasks
Complete the following steps and write the commands used to
perform each task in the space provided.
1. Use the find command to search the /usr directory and display
the file names of any length that end with ln.
2. Use the ls -li command to answer the following questions:
▼ How many links are there to the file /etc/init.d/lp?
_______________________________________________________
▼ Are these hard or symbolic links?
_______________________________________________________
▼ What is the inode number of the /etc/init.d/lp file?
_______________________________________________________

3. Create a file in /var/tmp that is linked to /etc/init.d/lp. Make
sure it works.

If
/var
is a separate file system, this will have to be a symbolic link.
4. Using the find command and starting at the /etc directory, find
the other files that are linked to /etc/init.d/lp. What are they?
___________________________________________________________
5. To what directory is /bin linked?
___________________________________________________________
6. Search for the text string other in the /etc/group file.
7. Using ls and grep, display all the files created today.
14
14-32 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Tasks
8. Using the grep command, look for all lines in the file4 file
located in your home directory that do not contain the letter "M".
9. Identify who is logged on to the system.
10. Switch to the guest account or another user’s account as specified
by your instructor in such a way that you are in that user’s
environment.
11. Type the who am i command. What happened?
___________________________________________________________
12. Type the id command. What happened?
___________________________________________________________
13. What command do you type to display the user ID and all groups
that you belong to?

__________________________________________________________
To create a file for use in the next step, type:
$ ls -la > ls.output
14. Sort the ls.output file. Produce a numerical listing by size of the
files, in reverse order. What command did you use?
___________________________________________________________
15. Perform a multilevel sort of the ls.output file that places the
data in chronological order, then alphabetically by name. What
command did you use?
___________________________________________________________
14
File and User Information Utilities 14-33
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Workshop Labs
Use what you have learned so far in this course to work through the
following:
1. On Monday you are told that all members of your department are
to be dismissed because they consistently arrive at work late. In
order to prove to your supervisor that this is not the case, you ask
to have a week to gather data to prove your group’s timeliness. At
the end of the week, you need to provide a printed copy of a file
that lists what time everyone in your group logged in each day for
a week. You have to create this file using commands that you have
learned. For easier readability, the printout should be sorted by
employee before being printed. You would also like to have a copy
in your home directory of the sorted file to refer to at a later date.
Using the least number of commands, how can you achieve this
goal and save the group’s job?
2. You would like to have a list of all of the names of the

subdirectories under /etc. Because you want to keep this list for
future reference in a file, you would like any error messages that
might appear while you are doing your search to be removed from
the final output. Create a file that contains the list of these
subdirectories and place it in your personal binary directory.
3. Using the caret (^) character with the regular expression in the
grep command indicates the first character in a line. (For example,
$ grep ^c
file
will look for lines that start with a “c” in the
named file.) Using the file created in the workshop lab for Module
6, generate a file that contains the shell commands that start with
the letters “a” though “f”, regardless of case (upper or lower). Sort
this file alphabetically using the second field of the output of the
grep command, and save the sorted text. Important – You will
need to have the sort command ignore blank spaces when
performing the sort.
4. To which commands is the cp command linked? What similarities
are there between these commands?
14
14-34 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Exercise Summary
Discussion – Take a few minutes to discuss what experiences, issues,
or discoveries you had during the lab exercises.

Manage the discussion here based on the time allowed for this module, which was given
in the “About This Course” module. If you find you do not have time to spend on
discussion, then just highlight the key concepts students should have learned from the

lab exercise.
● Experiences

Ask students what their overall experiences with this exercise have been. You might want
to go over any trouble spots or especially confusing areas at this time.
● Interpretations

Ask students to interpret what they observed during any aspects of this exercise.
● Conclusions

Have students articulate any conclusions they reached as a result of this exercise
experience.
● Applications

Explore with students how they might apply what they learned in this exercise to
situations at their workplace.
14
File and User Information Utilities 14-35
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Exercise Solutions
Complete the following steps and write the commands used to
perform each task in the space provided.
1. Use the find command to search the /usr directory and display
the file names of any length that end with ln.
$ find /usr -name ’*ln’
2. Use the ls -li command to answer the following questions:
▼ How many links are there to the file /etc/init.d/lp?
Four or five, depending upon the system.
▼ Are these hard or symbolic links?

Hard
▼ What is the inode number of the /etc/init.d/lp file?
It is different for every system. It is the number on the far left of the
permissions.
3. Create a file in /var/tmp that is linked to /etc/init.d/lp. Make
sure it works.
$ ln /etc/init.d/lp /var/tmp/printer
$ ls -li /var/tmp/printer
4. Using the find command and starting at the /etc directory, find
the other files that are linked to /etc/init.d/lp. What are they?
$ find /etc -inum inode number
5. To what directory is /bin linked?
$ ls -ld /bin
It is linked to /usr/bin.
6. Search for the text string other in the /etc/group file.
$ grep other /etc/group
14
14-36 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Exercise Solutions
7. Using ls and grep, display all the files created today.
$ ls -l | grep 'May 9'
(This date is for example only.)
8. Using the ln command, create a symbolic link in your home
directory to the command /usr/bin/clear. (You can name the
link in your home directory whatever you want.) Check to see if
the new link works to clear your screen.
$ ln -s /usr/bin/clear cl
9. Using the grep command, look for all lines in the file4 file

located in your home directory that do not contain the letter "M".
$ grep -v 'M' file4
10. Identify who is logged on to the system.
$ who
11. Switch to the guest account in such a way that you are in that
user’s environment.
$ su - guest
12. Type the who am i command. What happened?
Your real UID (RUID) is displayed.
13. Type the id command. What happened?
Your effective UID (EUID) is displayed.
14. What command do you type to display the user ID and all groups
that you belong to?
$ id -a
14
File and User Information Utilities 14-37
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Using File and User Information Utilities
Exercise Solutions
To create a file for use in the next step, type:
$ ls -la > ls.output
15. Sort the ls.output file. Produce a numerical listing by size of the
files, in reverse order. What command did you use?
$ sort -rn +4 ls.output
16. Perform a multilevel sort of the ls.output file that places the
data in chronological order, then alphabetically by name. What
command did you use?
$ sort +5M +6n +8d ls.output
If time of day created is an issue, the answer would be:
$ sort +5M +6n +7n +8d ls.output

14
14-38 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Check Your Progress
Before continuing on to the next module, check that you are able to
accomplish or answer the following:
❑ Describe some advantages of using file systems
❑ List the inode number of a file
❑ Link files using hard and symbolic links
❑ Employ the find command to locate files by specific criteria
❑ Search for text within a document using the grep command
❑ Identify which users are logged on to your system
❑ Switch to a new user ID and identify the user ID you are currently
using
❑ Identify your effective and real UID when switching users
❑ Use the sort command to sort ASCII files in alphabetical and
numerical order
❑ Use the sort command to perform multilevel sorts on ASCII data
14
File and User Information Utilities 14-39
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Think Beyond
For what kinds of situations might you use the utilities presented in
this module in your work environment?
15-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
BackingUpandRestoring
15
Objectives

Upon completion of this module, you should be able to:
● Use the tar and compress commands to back up files and
directories
● Use the jar command to save and compress files and directories
● Position a tape to a selected data set using the mt utility
15
15-2 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Relevance

Present the following question to stimulate the students and get them thinking about the
issues and topics presented in this module. While they are not expected to know the
answer to the question, the answer should be of interest to them and inspire them to learn
the content presented in this module.
Discussion – How frequently do you back up or restore files?
15
Backing Up and Restoring 15-3
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Contents Overview
In order to back up or easily transfer files via ftp or another method,
you need to be able to package and restore the files easily. This module
covers the following commands:
● tar
● compress
● jar
● mt
In addition, specific instructions for backing up and restoring your
home directory using tar and compress are included.
15
15-4 Fundamentals of Solaris 7

Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The tar Command
The tar (tape archive) command enables you to back up single or
multiple files in a directory hierarchy.
Command Format
tar
function_[letter/modifier]
[
output file
]
filename(s)/directory(s)
Function Letters
Function Modifiers
c Create a new tar file
t List the table of contents of the tar file
x Extract the specified files from the tar file
f Specify the tar file (
/tmp/file.tar
) or tape
drive (/dev/rmt/
x
) if other than default
v Execute in verbose mode
15
Backing Up and Restoring 15-5
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The tar Command
Saving a Directory to Tape
You can create the tar file using a relative path name.
This archives the user1 directory to the default device,/dev/rmt/0.

This command is also useful for creating archive files to share with
other users or attach to mail messages.
$ cd
$ tar cvf files.tar file1 file2 file3
$ cd /home
$ tar cv user1
15
15-6 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The compress Command
The compress command is used to compress files using a special
format to reduce the size of the file from 20 percent to 80 percent.
Command Format
compress
filename
Compressing a File
The following example uses the -v (verbose) option to compress a file
called bin.file:
The file is compressed and a .Z suffix is appended to the file name.
$ compress -v bin.file
bin.file: Compression: 70.84% replaced with
bin.file.Z
15
Backing Up and Restoring 15-7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The compress Command
Files are uncompressed using the uncompress command.
Command Format: Uncompressing
uncompress
filename

Uncompressing a File
$ uncompress bin.file.Z
15
15-8 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The jar Command
The jar command is similar to the tar command, but compresses the
resulting file in the same step. It is a Java™ application that combines
multiple files into a single JAR (Java archive) file. It is also a general-
purpose archiving and compression tool, based on ZIP and the ZLIB
compression format. The jar command was originally created for Java
programmers to download multiple files with one request, rather than
having to issue a download request for each separate file. jar is
standard with the Solaris 7 operating system, but is available on any
system that has Java virtual machine (JVM) installed.
Command Format
The syntax for the jar tool is almost identical to the syntax for the tar
command.
jar
options
[
output file
]
filename(s)/directory(s)
15
Backing Up and Restoring 15-9
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The jar Command
Options
Note – It is not necessary to use a hyphen (-) before options when

issuing the jar command.
Adding All the Files in a Directory to an Archive
$ ls
dante dir1 dir3 file1 file3 fruit
dante_1 dir2 dir4 file2 file4 fruit2
$ jar cvf bundle.jar *
adding: dante (in=1320) (out=744) (deflated 43%)
adding: dante_1 (in=368) (out=242) (deflated 34%)
adding: dir1/ (in=0) (out=0) (stored 0%)
adding: dir1/coffees/ (in=0) (out=0) (stored 0%)
adding: dir1/coffees/beans (in=0) (out=0) (stored 0%)
adding: dir1/coffees/nuts (in=0) (out=0) (stored 0%)
adding: dir1/fruit/ (in=0) (out=0) (stored 0%)
adding: dir1/trees/ (in=0) (out=0) (stored 0%)
adding: dir2/ (in=0) (out=0) (stored 0%)
<output omitted>
adding: file1 (in=1696) (out=874) (deflated 48%)
<output omitted>
Note – The jar tool is not intended for backing up symbolic links.
Error messages will display if this is attempted.
c Create a new jar file
t List the table of contents of the jar file
x Extract the specified files from the jar file
f Specify the jar file (
/tmp/file.jar
) or tape
drive (/dev/rmt/
x
) if other than default
v Execute in verbose mode

15
15-10 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The mt Command
The mt command enables direct tape manipulation.
Command Format
mt[-f
tape-device-name
]
command
[
count
]
The -f option is used to specify the tape device file name, typically a
no-rewind device file name. If the -f option is omitted, the value of
the TAPE environment variable is used to determine the tape device to
manipulate.
15
Backing Up and Restoring 15-11
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
The mt Command
Commands
Note – Only the unique prefix of a command is required. For example,
use ret in place of retension.
Use the following mt command to rewind a tape:
$ mt rew
To have the tape skip forward a tape file, use the following command:
$ mt -f /dev/rmt/0n fsf 1
The device name /dev/rmt/0n tells the tape drive not to rewind the
tape after it has performed a function. This device naming convention

can also be used when archiving files with tar and jar.
Note – The retension option does not work with 8-mm tapes.
status Displays status information about the tape
drive.
rewind Rewinds the tape.
retension Rewinds the cartridge tape completely, winds
the tape forward to the end of the tape, then
rewinds back to the beginning of the tape to
smooth out the tape tension.
erase Erases the entire tape.
fsf Forward skips
count
tape files.
bsf Backward skips
count
tape files.
eom Skips to the end of the recorded media.
off Ejects 4- or 8-mm tapes.
15
15-12 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Backing up and Restoring the Home Directory
Backing up the Home Directory
Since most work is done in users’ home directories, these directories
are often scheduled for backup on a nightly basis by system
administrators. If your home directory is not being backed up by the
system administrator, it is good practice to perform a regular nightly
backup of changing data.
1. Create the tar file using a relative path name.
2. Display the table of contents.

3. Compress the tar file.
4. Change directory to /tmp. Back up the compressed file on to a
tape using the tar command.
$ cd /export/home
$ tar cvf /tmp/home.tar user1
$ tar tvf /tmp/home.tar
$ compress -v /tmp/home.tar
/tmp/home.tar: Compression: 80.85% replaced with
/tmp/home.tar.Z
$ cd /tmp
$ tar cv home.tar.Z
15
Backing Up and Restoring 15-13
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Backing up and Restoring the Home Directory
Restoring the Home Directory
To restore files,
1. In the home directory, make a new directory and change to it to
prevent overwriting of files.
2. Extract the home directory from tape.
3. Uncompress the file.
4. Extract the tar file.
5. Move files as needed.
$ cd
$ mkdir newhome
$ cd newhome
$ tar xv
$ uncompress home.tar.Z
$ tar xvf home.tar
15

15-14 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Saving and Restoring Files
Exercise objective – In this exercise you will practice backing up files
to tape or disk.
Tasks
Note – If you get a “Permission Denied” error while performing the
following exercises, check the write protect switch on the tape.
Complete these steps:
1. Back up your home directory to a file using the tar command.
2. Compress the tar file and archive it to tape.
3. Use the jar utility to back up ~/practice.
4. Use the tar utitlity to back up ~/practice and compress the file.
5. Compare the tar and jar file backups of ~/practice for size.
6. Retension the tape.
Workshop Labs
Use what you have learned so far in this course to work through the
following:
1. Another user in your group needs a copy of the files in your
personal binary directory. Create a compressed tar file, place it in
that directory, and make it available to the other user.

×