Check Your Progress Before continuing on to the next module, check that you are able toaccomplish or answer the following: ❑ Contrast absolute and relative pathnames ❑ Access files and d
Trang 2Accessing Files and Directories 5-39
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Exercise Solutions
Using Figure 5-3 on page 5-32 as reference, perform each of thefollowing tasks on your system Use pathname abbreviationswhenever possible
4 Change to your home directory (~)
Trang 4Accessing Files and Directories 5-41
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Exercise Solutions (Continued)
19 On one command line, change to your home directory and list thecontents of the directory
cd;ls
20 Issue anlscommand that will display file and directory names ofany length beginning with the letters “d” or ”f”
ls [df]*
21 Issue anlscommand that will display all files starting with file
followed by any one character
ls file?
Trang 5Check Your Progress
Before continuing on to the next module, check that you are able toaccomplish or answer the following:
❑ Contrast absolute and relative pathnames
❑ Access files and directories within the file structure using absoluteand relative pathnames
❑ Identify and describe the parts of a command line
❑ Access files and directories within the file structure usingpathname 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
Trang 6Accessing Files and Directories 5-43
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
In your own work environment, what subdirectory structure would beuseful for your home directory? What kinds of files do you work withmost often?
Trang 8Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Upon completion of this module, you should be able to
● Control screen output using control characters
● Determine a file’s type with thefilecommand
● Display the contents of text files using the cat,more,head, and
tailcommands
● Determine word, line, and character count using the wccommand
● Compare the contents of text files using diffand cmp
● Create empty files or update access time of existing files using the
touchcommand
● Create and remove directories using mkdirand rmdir
● Manage files and directories using the mv,cp, and rmcommands
● Save the output from a command into a file
● Pass output from one command to another using a pipe
● Use theteecommand within a pipeline to create text within a file
Trang 9Relevance
✓ 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
Trang 10Directory 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 stoppingand starting screen output When displayed on the screen, the Controlkey is represented by the caret symbol (^)
To enter a sequence of control characters, hold down the Control keyand 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.
Trang 11Control 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 outputControl-q Resumes screen outputControl-c Interrupts current activityControl-d Indicates end-of-file or exitControl-u Erases the command lineControl-w Erases the last word on the line
Trang 12Directory 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 offile can be determined by using the filecommand This informationcan be important when a user is attempting to open or read a file.Determining the file type can help a user decide which program orcommand to use to open the file
The output from this command will most often be one of thefollowing:
● Text – Examples include ASCII text, English text, commands text,
and executable shell scripts The text file type also includesexecutable shell scripts This type of file can be read using thecat
ormorecommands, which are discussed in this module, andedited usingvior another editor
✓ The filecommand determines file type by referencing the first two bytes of the file See the contents of /etc/magic.
Trang 13Determining File Type
● Executable or Binary – Examples include 32-bit executable and
extensible linking format (ELF) code files and other dynamicallylinked executables This file type indicates that the file is acommand or program Thestringscommand, shown on the nextpage, will print out readable characters in this type of file (Theoutput produced bystringsis easily interpreted by someonewith a programming background The command is introducedhere 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 inwhich this file was created cannot be determined by thefile
command, the output will simply indicate data file The only way
to read a data file is to determine which application created it andopen the document with that application If you have manyapplications on your system, this can be a time consumingprocess
For information on other file types, see the man pages
Trang 14Directory and File Commands 6-7
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Example Data File
$ cd /home/user2/dir1/coffees
$ file beans
beans: Frame Maker Document
FrameMaker must be used to read the beansfile
Example Executable File
$ strings /usr/bin/cat
SUNW_OST_OSCMDusvtebn
usage: cat [ -usvtebn ] [-|file]
cat: Cannot stat stdoutcat: cannot open %scat: cannot stat %scat: input/output files ‘%s’ identicalcat: close error
<some output omitted>
Trang 15Displaying Files
Thecat(concatenate) command displays the contents of a text file onthe screen It is often used to display short text files; becausecat
flashes through the entire file rapidly without pausing, it is unsuitablefor files longer than one screen in length Thecatcommand is moreoften used to join two or more files into one large file
Command Format
cat filename(s)
Trang 16Directory and File Commands 6-9
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the cat Command to Display a Short Text File
$
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
Trang 17Displaying Files
Use themorecommand to display the contents of a text file to thescreen one screen at a time If the information in a file is longer thanone 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 themoreutility for display purposes, sothe scrolling keys in the following table are the same ones you used todisplay man pages
Note – Usingcatormoreto read executable or binary files can cause
a terminal or window to hang
Trang 18Directory and File Commands 6-11
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Command Format
more filename(s)
At the More prompt, you can use the following keys to control thescrolling capabilities:
Table 6-2 Scrolling Keys
Scrolling Keys Purpose
Spacebar Scroll to the next screenReturn Scroll one line at a time
b Move back one screen
f Move forward one screen
h Display a Help menu ofmore features
q Quit and return to the shell prompt
/string Search forward forstring
n Find next occurrence ofstring
Trang 19Displaying Files
Use theheadcommand to display the first n lines of one or more files.
The first 10 lines are displayed by default if the-noption is omitted
Command Format
head [ -n ] filename(s)
Trang 20Directory and File Commands 6-13
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Displaying a Specific Number of Lines at the Beginning of a File
$ head -6 /usr/dict/words
10th1st2nd3rd4th5th
$
In this example, the head -6command displays the first six lines ofthe /usr/dict/wordsfile
Trang 21Displaying Files
Use thetail command to display the last n lines of a file The last 10
lines are displayed by default if the-noption is omitted
Command Format
tail [ -n ] filename(s)tail [ +n ] filename(s)
Trang 22Directory and File Commands 6-15
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Displaying a Specified Number of Lines at the End of a File
$ tail -5 /usr/dict/words
zoundsz‘szucchiniZurichzygote
Trang 23Displaying Files
Use thewccommand to display a line, word, or character count of afile This command is useful when trying to determine characteristics
of a file Whereas the size can be determined in bytes by using the
ls -lcommand, much more information is obtained with wc
Trang 24Directory and File Commands 6-17
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using wc Without Options
Trang 25Comparing Files
Locating Text Differences With the cmp Command
Comparing files to determine differences between them can be doneusing thecmpcommand This command produces no output if thefiles are exactly the same, or it provides the byte and line numbers atwhich the first difference occurred if there are discrepancies betweenthe files
Command Format
cmp file1 file2
Trang 26Directory and File Commands 6-19
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
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 thecmp
command
$ cmp ~/cshrc ~/.cshrc
cshrc /home/user2/cshrc differ: char 1638, line 42
This output identifies the first occurrence of a difference betweenthe two files The difference occurred at the 1,638th characterposition and was found on line 42
You can now manually locate any further discrepancies and takewhatever action you deem necessary
Note – The cmpcommand is quite useful when comparing binary files,but less useful for text files For more information on this command,see the man pages
Trang 27Comparing Files
Another command used for finding differences between files is the
diffcommand The output of this command will display line-by-linedifferences between two text files
Command Format
$ diff [option] file1 file2
Trang 28Directory and File Commands 6-21
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
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 modifiedslightly: output begins with identification of thefiles involved and their creation dates Following aline of a dozen *s, the line numbers offile1 thatare to be displayed are listed The actual lines from
file1 are then displayed, appended by a – forthose lines that are different fromfile2 The samedisplay follows as it applies tofile2, with a +appended for those lines that are different from
file1
Trang 29Comparing Files
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****
orangeapplebanana-pear-mangotomatopomegranate -2, 8 orangeapplebananatomato+guava+mangopomegranate
$ cat fruit
lemonorangeapplebananapearmangotomatopomegranate
$ cat fruit2
lemonorangeapplebananatomatoguavamangopomegranate
Trang 30Directory and File Commands 6-23
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
File and Directory Naming Conventions
● 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 arepreceded by a period (.) You may choose to use this conventionwhen naming files, but it is not a necessary part of a file name
Trang 31File 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 lowercaseletters and directory names with capital letters
Trang 32Directory and File Commands 6-25
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Creating Empty Files
Note – The touchcommand creates an empty file if the file namespecified does not exist Otherwise, the access/modification time ofthe existing file is updated
Trang 33Suppose you want to create a file which contains the names and phonenumbers of your co-workers You can use theteecommand as
follows:
$tee phone_list.txt Bill O - 808-555-9876
Bill O - 808-555-9876
Fred P - 808-555-6543
Fred P - 808-555-6543
^D
When the first line is entered, the system searches for a file named
phone_list.txt.If the file exists, the system overwrites its contentswith the new information If the file does not exist, it is created and thecontent is written into it
Trang 34Directory and File Commands 6-27
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Each line is entered by pressing the Return key The line of text isduplicated in the shell, showing you it has been written to the outputfile Use Control-d to end the input of content
The -a option for theteecommand allows you to append to a file’scurrent contents without overwriting the current contents
Trang 35Creating Directories
Use themkdircommand to create directories Directories can becreated using either an absolute or a relative pathname You canspecify more than one directory name on the same line to create morethan one new directory
Command Format
mkdir [-p] directory_name