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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Directory and File Commands Objectives Upon completion of this module, you should be able to q Control screen output using control characters q Determine a file’s type with the file command q Display the contents of text files using the cat, more, head, and tail commands q Determine word, line, and character count using the wc command q Compare the contents of text files using diff and cmp q Create empty files or update access time of existing files using the touch command q Create and remove directories using mkdir and rmdir q Manage files and directories using the mv, cp, and rm commands q Save the output from a command into a file q Pass output from one command to another using a pipe q Use the tee command within a pipeline to create text within a file 6-1 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 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: q 6-2 Solaris User’s Guide, Part Number 802-6499 Fundamentals of Solaris 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 Directory and File Commands 6-3 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Control Characters The following control characters can be used: Table 6-1 Control Characters Control Characters Control-s Resumes screen output Control-c Interrupts current activity Control-d Indicates end-of-file or exit Control-u Erases the command line Control-w 6-4 Stops screen output Control-q  Purpose Erases the last word on the line The actual character in the shell appears as ^C, even though you press the Control key and the c key at the same time Fundamentals of Solaris 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: q  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 Directory and File Commands 6-5 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Determining File Type q 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.) q 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: 6-6 English text Fundamentals of Solaris 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 Directory and File Commands 6-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-8 Fundamentals of Solaris 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 Directory and File Commands 6-9 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-10 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Moving and Renaming Files Use the mv command to move or rename files and directories Command Format mv [-i] source target_file mv [-i] source target_directory You can use the mv -i option, which prompts for confirmation whenever the move would overwrite an existing target A y answer means that the move should proceed Any other answer prevents mv from overwriting the target Directory and File Commands 6-35 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Moving and Renaming Files Renaming Files in the Current Directory The following example demonstrates renaming a file in the current directory: $ cd ~/dir1/coffees $ ls beans nuts $ mv nuts brands $ ls beans brands $ Moving Files to Another Directory The following example demonstrates moving a file into another directory: $ pwd /home/user2/dir1/coffees $ ls beans brands $ mv brands ~ $ ls beans $ ls ~/b* /home/user2/brands $ 6-36 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Moving and Renaming Files Renaming Directories The following example demonstrates using the mv command to rename directories within the current directory: $ pwd /home/user2/dir1/coffees $ cd $ mkdir maildir $ ls Reports dir2 file1 brands dir3 file2 dante dir4 file3 dante_1 feathers file4 dir1 feathers_6 fruit $ mv maildir monthly $ ls Reports dir2 brands dir3 dante dir4 dante_1 feathers dir1 feathers_6 $ file1 file2 file3 file4 fruit fruit2 tutor.vi games maildir practice ski.places fruit2 tutor.vi games monthly practice ski.places Moving a Directory and Its Contents The following example demonstrates using the mv command to move a directory and its contents into another directory: $ pwd /home/user2 $ mv practice letters $ ls letters dir1 mailbox project $ Directory and File Commands research 6-37 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Moving and Renaming Files Renaming in a Non-Current Directory The following example demonstrates how to use the mv command to rename a file in a non-current directory: $ pwd /home/user2 $ mv letters/project letters/project2 $ ls letters dir1 mailbox project2 research $ 6-38 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Removing Files Use the rm command to remove files Command Format rm [-i] filename(s) Directory and File Commands 6-39 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Removing Files You can remove several files $ cd ~/letters $ pwd /home/user2/letters $ ls dir1 mailbox project2 research $ touch projection $ ls dir1 mailbox project2 projection $ rm research project2 $ ls dir1 mailbox projection research Use the rm -i command as a precaution The -i option prompts you for confirmation before removing any file $ rm -i projection rm: remove projection: (yes/no) ? y $ ls dir1 mailbox 6-40 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Removing Directories Use the rm and rmdir commands to remove directories The rmdir command only deletes empty directories If the directory to be removed is your current directory, you will not be able to remove it with either of these commands Command Format rmdir directory_name(s) rm -r [i] directory_name(s) Directory and File Commands 6-41 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Removing Directories Removing Directories Use the rmdir command to remove an empty directory $ cd $ pwd /home/user2 $ mkdir -p newdir/empty $ cd newdir $ ls -F empty/ $ rmdir empty $ ls $ Use rm -r to remove a directory that is not empty $ cd $ pwd /home/user2 $ rm -r letters $ ls Reports dir2 brands dir3 dante dir4 dante_1 feathers dir1 feathers_6 file1 file2 file3 file4 fruit fruit2 tutor.vi games monthly newdir ski.places Use rm -ir to interactively remove directories $ mkdir -p ~/practice/dir1 $ ls ~/practice dir1 $ rm -ir rm: examine files in directory practice (yes/no)? y rm: examine files in directory practice/dir1 (yes/no)? y rm: remove practice/dir1: (yes/no)? y rm: remove practice: (yes/no)? y 6-42 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Redirection Overview All central processing unit (CPU) operations have input and/or output (I/O) The keyboard, for example, provides standard input while the monitor displays standard output and standard error Figure 6-1 Input, CPU, and Output Directory and File Commands 6-43 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Redirection Overview These are defined as follows: q Input – Sending data to a command q Output – Receiving data from a command q Error - Errors generated by a command Standard input Command Standard output The Solaris computing environment enables command I/O to be controlled using redirection This is useful when attempting to save the output of a command to a file for later viewing, sending the contents of a file as an email message, or redirecting error messages to a file for use when debugging a program The format for the redirection of standard input, standard output, and standard error is: command > file command < file command 2> file 6-44 or command >> file Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Redirection Angle Brackets ( >, ) Use the right-angle bracket (>) to redirect the output of a command to a file rather than to the screen Use the left-angle bracket () to redirect error from a command to a file rather than to the screen Redirecting Output From a Command to a File $ ls /etc > etc.list ! Caution – If the name of the file already exists, you will overwrite it In the Korn shell, an option called noclobber can be set to prevent overwriting of files during redirection This can be done on the command line by using $ set -o noclobber Redirecting the Input of a File to a Command $ mailx user2@saturn < dante Note – This example illustrates redirection of input using the mailx command This is the standard command-line mail program and is addressed in more detail in Appendix B Redirecting Standard Error to a File $ Date 2> errorfile $ cat errorfile ksh: Date: not found Directory and File Commands 6-45 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Redirection Angle Brackets ( >> ) Appending Output Use the double right-angle bracket (>>) to append the output of a command to an existing file $ cal 10 2000> mon00 $ cat mon00 October 2000 S M Tu W Th F S 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ cal 2001 >> mon00 $ cat mon00 October 2000 S M Tu W Th F S 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 January S M Tu 14 15 16 21 22 23 28 29 30 6-46 2001 W Th 10 11 17 18 24 25 31 F 12 19 26 S 13 20 27 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Piping One of the most powerful metacharacters is the pipe (|) The pipe takes the standard output of one command and passes it as standard input into a following command In effect, pipes enable you to build a miniature program You must always have a command on each side of a pipe Directory and File Commands 6-47 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Piping Displaying Command Output One Screen at a Time One of the most common uses of the pipe metacharacter is to send the output of an ls command to the more command to enable you to see a long directory listing one screen at a time $ ls -l /etc | more The standard output from the command to the left of the pipe becomes the standard input for the command on the right side of the pipe Using the tee Command in a Pipeline Syntax for a pipe requires that there be a command on both sides of the pipe If output to a file is desired in the middle of a pipeline, the tee command can be used The following command creates a file containing a listing of the home directory from the output of the ls command, without disturbing the flow of the output to the lp command: $ ls ~ | tee homedir | lp Note – lp is the UNIX command-line print command used to print text files Printing is covered in Module 13 6-48 Fundamentals of Solaris Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D Exercise: Using Directory and File Commands Exercise objective – In this lab you will use the commands introduced in this module to display the contents of files; compare files; determine file type, and create, move, and remove files and directories Tasks Complete or answer the following: Determine the file type of /etc/passwd and display the contents of the file Display the contents of the file /usr/dict/words one screen at a time Exit after displaying two screens What command would you most likely use to read the contents of /usr/bin/cp ? Return to your home directory (if you need to) and list the contents Copy the dir1/coffees/beans file into the dir4 directory and call it roses Create a directory called vegetables in dir3 Move the dir1/coffees/beans file into the dir2/recipes directory Copy dir3/planets/mars to the practice directory and name the file addresses Create a directory called play in your practice directory and move the practice/addresses file to that play directory 10 Copy the play directory in the practice directory to a new directory in the practice directory called appointments 11 Recursively list the contents of the practice directory Directory and File Commands 6-49 Copyright 1999 Sun Microsystems, Inc All Rights Reserved Enterprise Services August 1999, Revision D ... phone numbers of your co-workers You can use the tee command as follows: $ tee Bill Bill Fred Fred ^D phone_list.txt O - 808 -55 5-9 876 O - 808 -55 5-9 876 P - 808 -55 5- 654 3 P - 808 -55 5- 654 3 When the... 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 January S M Tu 14 15 16 21 22 23 28 29 30 6-46 2001 W Th 10 11 17 18 24 25 31 F 12 19 26 S 13 20 27 Fundamentals of Solaris Copyright 1999 Sun. .. 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

Ngày đăng: 24/07/2014, 02:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan