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

Sun Fundamentals of Solaris 7 EU-118 Student Guide With Instructor Notes phần 5 doc

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 (305.85 KB, 49 trang )

6-1
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
DirectoryandFileCommands
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
6
Directory and File Commands 6-19

Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Comparing Files
Locating Text Differences With the cmp Command
Using the cmp Command to Compare Files That Appear to be the
Same
You would:
1. Do a long listing and compare the file sizes.
-rw-r r 1 user2 edu 3528 July 6 16:19 /home/user2/cshrc
-rw-r r- 1 user2 edu 3528 July 6 16:19 /home/user2/.cshrc
2. From the long listing, identify the date the files were last changed.
If this date signifies that the files are the same, use the cmp
command.
$ cmp ~/cshrc ~/.cshrc
cshrc /home/user2/cshrc differ: char 1638, line 42
This output identifies the first occurrence of a difference between
the two files. The difference occurred at the 1,638th character
position and was found on line 42.
You can now manually locate any further discrepancies and take
whatever action you deem necessary.
Note – The cmp command is quite useful when comparing binary files,
but less useful for text files. For more information on this command,
see the man pages.
6
6-20 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Comparing Files
Using the diff Command
Another command used for finding differences between files is the
diff command. The output of this command will display line-by-line
differences between two text files.

Command Format
$ diff [
option
]
file1 file2
6
Directory and File Commands 6-21
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Comparing Files
Using the diff Command
Options
-i Ignores the case of letters; for example, A is equal to
a.
-c Produces a listing of differences with three lines of
context. With this option, output format is modified
slightly: output begins with identification of the
files involved and their creation dates. Following a
line of a dozen *s, the line numbers of
file1
that
are to be displayed are listed. The actual lines from
file1
are then displayed, appended by a – for
those lines that are different from
file2
. The same
display follows as it applies to
file2
, with a +
appended for those lines that are different from

file1
.
6
6-22 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Comparing Files
Using the diff Command
Using diff to Compare Files
$ diff -c fruit fruit2
*** fruit Fri May 8 11:32:49 1998
fruit2 Fri May 8 14:55:21 1998
************
***2, 8****
orange
apple
banana
-pear
-mango
tomato
pomegranate
2, 8
orange
apple
banana
tomato
+guava
+mango
pomegranate
$ cat fruit
lemon

orange
apple
banana
pear
mango
tomato
pomegranate
$ cat fruit2
lemon
orange
apple
banana
tomato
guava
mango
pomegranate
6
Directory and File Commands 6-23
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
File and Directory Naming Conventions
These include:
● File names are made up of a maximum of 255 alphanumeric
characters.
● Some non-alphanumeric characters, such as underscores (_),
hyphens (-), and periods (.), are allowed.
● While the shell will allow asterisks (*), ampersands (&), pipes (|),
quotes (“ ”), and dollar signs ($) to be used in a file name, this is
not recommended, as these characters have special meaning to the
shell.
● File names may contain one or more extensions, usually appended

to file by an application. Extensions are usually one to three
characters that are appended to the end of a file name and are
preceded by a period (.). You may choose to use this convention
when naming files, but it is not a necessary part of a file name.
6
6-24 Fundamentals of Solaris 7
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
File and Directory Naming Conventions
● Directory names generally do not contain extensions, but there are
no rules against it.
Note – To help distinguish between files and directories, some site
administrators prefer their users to start filenames with lowercase
letters and directory names with capital letters.
6
Directory and File Commands 6-25
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1999, Revision D
Creating Files
You can create new, empty files using the touch command.
Command Format
touch
filename(s)
Absolute and relative pathnames can be specified when creating
files or directories.
Creating Empty Files
Note – The touch command creates an empty file if the file name
specified does not exist. Otherwise, the access/modification time of
the existing file is updated.
$ cd ~/practice
$ touch mailbox project research
$ ls

mailbox project research

×