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 thefilec
Trang 1Copyright 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 2Relevance
✓ 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 3Directory 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 4Control 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 5Directory 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 6Determining 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 7Directory 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
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 8Displaying Files
Using the cat Command
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 9Directory and File Commands 6-9
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the cat Command
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 10Displaying Files
Using the more Command
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 11Directory and File Commands 6-11
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the more Command
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 12Displaying Files
Using the head Command
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 13Directory and File Commands 6-13
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the head Command
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 14Displaying Files
Using the tail Command
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 15Directory and File Commands 6-15
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the tail Command
Displaying a Specified Number of Lines at the End of a File
$ tail -5 /usr/dict/words
zoundsz‘szucchiniZurichzygote
Trang 16Displaying Files
Using the wc Command
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 17Directory and File Commands 6-17
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Using the wc Command
Using wc Without Options
Trang 18Comparing 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 19Directory 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 20Comparing Files
Using the diff Command
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 21Directory and File Commands 6-21
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
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 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 22Comparing 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****
orangeapplebanana-pear-mangotomatopomegranate -2, 8 orangeapplebananatomato+guava+mangopomegranate
$ cat fruit
lemonorangeapplebananapearmangotomatopomegranate
$ cat fruit2
lemonorangeapplebananatomatoguavamangopomegranate
Trang 23Directory 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 24File 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 25Directory 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 26Suppose 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 27Directory 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 28Creating 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
Trang 29Directory and File Commands 6-29
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Trang 30Creating Directories
You must have the appropriate permissions to create a directory.(Permissions are covered later in the course.) If you do not have thecorrect permissions on a file or directory, you will receive an errormessage similar to this one:
You can create multiple levels of directories at one time by using the
Trang 31Directory and File Commands 6-31
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Copying Files
Use the cpcommand to copy files
Command Format
cp [-i] source_file destination_file
cp [-i] source_file(s) destination_directory
Trang 32Copying Files
Copying a File to Another Within a Directory
The following example demonstrates how to copy one file to a new file
in the same directory:
$ cp feathers feathers_6
Copying Multiple Files
The following example demonstrates how to copy multiple files into adirectory other than the current directory:
$ pwd
/home/user2
$ ls dir1
coffees fruit trees
$ cp feathers feathers_6 dir1
$ ls dir1
coffees feathers feathers_6 fruit trees
Preventing Overwriting an Existing File When Copying
Usecp -i as a security measure The-ioption prompts you if youare about to overwrite an existing file
$ cp -i feathers file3 cp: overwrite file3 (yes/no)? n
$
Trang 33Directory and File Commands 6-33
Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D
Copying Directories
Use the cp -r(recursive) command to copy a directory and itscontents to another directory If the directory does not exist, it iscreated by the cpcommand
Command Format
cp -r[i] source_directory(s) destination_directory
Without the-roption, the files and subdirectories contained within adirectory are not copied When used with the -ioption,cppromptsfor verification before overwriting an existing file