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

Sun Fundamentals of Solaris 7 Student Guide With Instructor Notes phần 4 potx

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 (347.89 KB, 68 trang )

5
5-38 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Accessing Files and Directories
Exercise Solutions
Use Figure 5-3 on page 5-32 to identify the pathnames for the
following objects:
1. Specify the absolute pathnames for:
▼ user2
/home/user2
▼ coffees
/home/user2/dir1/coffees
▼ dir4
/home/user2/dir4
Assume /home/user2 is the current directory for the next two
questions.
2. Specify the relative pathnames for:
▼ dir3
dir3
▼ flowers
dir4/flowers
▼ recipes
dir2/recipes
3. Specify the relative pathnames for the dir1 subdirectories and
files.
dir1/coffees; dir1/coffees/nuts; dir1/coffees/beans
dir1/fruit; dir1/trees
5
Accessing Files and Directories 5-39
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Accessing Files and Directories


Exercise Solutions
Using Figure 5-3 on page 5-32 as reference, perform each of the
following tasks on your system. Use pathname abbreviations
whenever possible.
4. Change to your home directory (~).
cd or cd ~
5. Change to the dir1 directory.
cd dir1
6. Change to the fruit directory.
cd fruit or cd ~/dir1/fruit
7. Change to the planets directory.
cd ~/dir3/planets or cd / /dir3/planets
8. Go back to your home directory (~).
cd or cd ~
9. Change to the /etc directory.
cd /etc
10. Change to the recipes directory.
cd ~/dir2/recipes or cd /home/user2/dir2/recipes
11. Change to the flowers directory.
cd / /dir4/flowers or cd ~/dir4/flowers
5
5-40 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Accessing Files and Directories
Exercise Solutions
Use the ls and cd commands to complete the following steps. Refer to
Figure 5-3 on page 5-32 for the first six steps as needed.
12. Return to your home directory.
cd or cd ~
13. Change to the dir1 directory.

cd dir1
14. List the contents of the dir1 directory.
ls
15. Display a recursive listing of the contents of the dir2 directory.
ls -R /dir2 or ls -R ~/dir2
16. Use the ls command to display a detailed listing of your home
directory, including hidden files.
ls -la ~
17. Use the ls option that will recursively display all subdirectories in
your home directory.
ls -R ~
Is there a directory in the root directory (/) called kernel?
Yes ; ls -F / or ls -ld /kernel
Is there a directory in /var/spool called cron?
Yes ; ls -F /var/spool
18. Without changing directories, type the ls command that will
display any file names that end with the number 1 in your home
directory.
ls ~/*1
5
Accessing Files and Directories 5-41
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Exercise: Accessing Files and Directories
Exercise Solutions (Continued)
19. On one command line, change to your home directory and list the
contents of the directory.
cd;ls
20. Issue an ls command that will display file and directory names of
any length beginning with the letters “d” or ”f”.
ls [df]*

21. Issue an ls command that will display all files starting with file
followed by any one character.
ls file?
5
5-42 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:
❑ Contrast absolute and relative pathnames
❑ Access files and directories within the file structure using absolute
and relative pathnames
❑ Identify and describe the parts of a command line
❑ Access files and directories within the file structure using
pathname abbreviations
❑ List the contents of directories and their file types
❑ Identify various metacharacters
❑ Enter more than one command on a single command line
❑ Demonstrate the use of wildcard characters
5
Accessing Files and Directories 5-43
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Think Beyond
In your own work environment, what subdirectory structure would be
useful for your home directory? What kinds of files do you work with
most often?
6-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Directory andFile Commands

6
Objectives
Upon completion of this module, you should be able to
● Control screen output using control characters
● Determine a file’s type with the file command
● Display the contents of text files using the cat, more, head, and
tail commands
● Determine word, line, and character count using the wc command
● Compare the contents of text files using diff and cmp
● Create empty files or update access time of existing files using the
touch command
● Create and remove directories using mkdir and rmdir
● Manage files and directories using the mv, cp, and rm commands
● Save the output from a command into a file
● Pass output from one command to another using a pipe
● Use the tee command within a pipeline to create text within a file
6
6-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 – What tasks do you need to complete to manage your
files and directories?
Additional Resources
Additional resources – The following reference can provide additional
details on the topics discussed in this module:

● Solaris User’s Guide, Part Number 802-6499
6
Directory and File Commands 6-3
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Control Characters
Control characters are used to perform specific tasks such as stopping
and starting screen output. When displayed on the screen, the Control
key is represented by the caret symbol (^).
To enter a sequence of control characters, hold down the Control key
and press the appropriate character on the keyboard.

The Control-s and Control-q characters were originally needed by teletype operators and
are rarely used today.
6
6-4 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Control Characters
The following control characters can be used:

The actual character in the shell appears as
^C
, even though you press the Control key
and the c key at the same time.
Table 6-1 Control Characters
Control Characters Purpose
Control-s Stops screen output
Control-q Resumes screen output
Control-c Interrupts current activity
Control-d Indicates end-of-file or exit
Control-u Erases the command line

Control-w Erases the last word on the line
6
Directory and File Commands 6-5
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Determining File Type
There are many types of files found on a Solaris system. The type of
file can be determined by using the file command. This information
can be important when a user is attempting to open or read a file.
Determining the file type can help a user decide which program or
command to use to open the file.
The output from this command will most often be one of the
following:
● Text – Examples include ASCII text, English text, commands text,
and executable shell scripts. The text file type also includes
executable shell scripts. This type of file can be read using the cat
or more commands, which are discussed in this module, and
edited using vi or another editor.

The
file
command determines file type by referencing the first two bytes of the file. See
the contents of
/etc/magic
.
6
6-6 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Determining File Type
● Executable or Binary – Examples include 32-bit executable and
extensible linking format (ELF) code files and other dynamically

linked executables. This file type indicates that the file is a
command or program. The strings command, shown on the next
page, will print out readable characters in this type of file. (The
output produced by strings is easily interpreted by someone
with a programming background. The command is introduced
here solely as a method for demonstrating the printable characters
of an executable file.)
● Data – Data files are those which are created by an application
running on the system. In some cases the type of file is indicated;
for example, FrameMaker document. When the application in
which this file was created cannot be determined by the file
command, the output will simply indicate data file. The only way
to read a data file is to determine which application created it and
open the document with that application. If you have many
applications on your system, this can be a time consuming
process.
For information on other file types, see the man pages.
Command Format
file
filename
(
s
)
Example Text File
$ file dante
dante: English text
6
Directory and File Commands 6-7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Determining File Type

Example Data File
$ cd /home/user2/dir1/coffees
$ file beans
beans: Frame Maker Document
FrameMaker must be used to read the beans file.
Example Executable File
$ file /usr/bin/cat
/usr/bin/cat: ELF 32-bit MSB executable SPARC
Version 1, dynamically linked,stripped
The strings command must be used to read /usr/bin/cat. For this
example, use strings followed by the file name as the command
format.
$ strings /usr/bin/cat
SUNW_OST_OSCMD
usvtebn
usage: cat [ -usvtebn ] [-|file]
cat: Cannot stat stdout
cat: cannot open %s
cat: cannot stat %s
cat: input/output files ‘%s’ identical
cat: close error
<some output omitted>
6
6-8 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the cat Command
The cat (concatenate) command displays the contents of a text file on
the screen. It is often used to display short text files; because cat
flashes through the entire file rapidly without pausing, it is unsuitable

for files longer than one screen in length. The cat command is more
often used to join two or more files into one large file.
Command Format
cat
filename(s)
6
Directory and File Commands 6-9
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the cat Command
Using the cat Command to Display a Short Text File
$ cat dante
The Life and Times of Dante
by Dante Pocai
Mention “Alighieri” and few may know about whom you
are talking. Say “Dante,” instead, and the whole world
knows whom you mean. For Dante Alighieri, like Raphael
.
.
.
$
If the file fills more than one screen, the data scrolls off the screen—
unless you are using a scrolling window, such as a terminal window,
within the CDE environment.
6
6-10 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the more Command
Use the more command to display the contents of a text file to the

screen one screen at a time. If the information in a file is longer than
one screen, the following message appears at the bottom of the screen:
More (n%)
where n is the percentage of the file already displayed.
The on-line manual pages use the more utility for display purposes, so
the scrolling keys in the following table are the same ones you used to
display man pages.
Note – Using cat or more to read executable or binary files can cause
a terminal or window to hang.
6
Directory and File Commands 6-11
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the more Command
Command Format
more
filename(s)
At the More prompt, you can use the following keys to control the
scrolling capabilities:
Table 6-2 Scrolling Keys
Scrolling Keys Purpose
Spacebar Scroll to the next screen
Return Scroll one line at a time
b Move back one screen
f Move forward one screen
h Display a Help menu of more features
q Quit and return to the shell prompt
/string
Search forward for
string

n Find next occurrence of
string
6
6-12 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the head Command
Use the head command to display the first n lines of one or more files.
The first 10 lines are displayed by default if the -
n
option is omitted.
Command Format
head [ -
n
]
filename(s)
6
Directory and File Commands 6-13
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the head Command
Displaying a Specific Number of Lines at the Beginning of a File
$ head -6 /usr/dict/words
10th
1st
2nd
3rd
4th
5th
$

In this example, the head -6 command displays the first six lines of
the /usr/dict/words file.
6
6-14 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the tail Command
Use the tail command to display the last n lines of a file. The last 10
lines are displayed by default if the -
n
option is omitted.
Command Format
tail [
-n
]
filename(s)
tail [
+n
]
filename(s)
6
Directory and File Commands 6-15
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the tail Command
Displaying a Specified Number of Lines at the End of a File
$ tail -5 /usr/dict/words
zounds
z‘s
zucchini

Zurich
zygote
$
In this example, the tail -5 command displays the last five lines of
the /usr/dict/words file.
Displaying Lines From a Specific Point in the File
$ tail +23 /usr/dict/words
<Output not shown>
$
In this example, the tail +23 command displays lines 23 through the
end of the /usr/dict/words file.
6
6-16 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the wc Command
Use the wc command to display a line, word, or character count of a
file. This command is useful when trying to determine characteristics
of a file. Whereas the size can be determined in bytes by using the
ls -l command, much more information is obtained with wc.
Command Format
wc [
options
]
filename(s)
Options
-l Counts lines
-w Counts words
-c Counts bytes
-m Counts characters

6
Directory and File Commands 6-17
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Displaying Files
Using the wc Command
Using wc Without Options
$ wc dante
33 223 1320 dante
Using wc without options will give a line, word, and byte count of
the contents of the file.
Determining the Number of Lines in a File
$ wc -l dante
33 dante
6
6-18 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Comparing Files
Locating Text Differences With the cmp Command
Comparing files to determine differences between them can be done
using the cmp command. This command produces no output if the
files are exactly the same, or it provides the byte and line numbers at
which the first difference occurred if there are discrepancies between
the files.
Command Format
cmp
file1 file2

×