Directory and File Commands

27 248 0
Directory and File Commands

Đ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 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 Discussion – What tasks you need to complete to manage your files and directories? - Create , edit, delete, copy, a file - Identity size of file - View content of a file - Compare content of two files 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 The following control characters can be used: 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 Control-l Clear screen Shift-Page Up Scroll up one page Shift-Page Down Scroll down one page  The actual character in the shell appears as ^C , even though you press the Control key and the c key at the same time Determining File Type There are many types of files found on a Linux 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  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 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 /bin/cat /lib/ld-linux.so.2 libc.so.6 stpcpy stdout putc_unlocked memmove getopt_long fpending ctype_b puts mbrtowc malloc abort iswprint 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) 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 Using the cat Command to join two files into one $ cat filename1 filename2 > file3 This example joins filename1 and filename2 files into filename3 file Note – If filename3 is existed, this command will overwrite it You can use the same type to join more 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 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 Spacebar Return b f h q /string n Keys Purpose Scroll to the next screen Scroll one line at a time Move back one screen Move forward one screen Display a Help menu of more features Quit and return to the shell prompt Search forward for string Find next occurrence of string 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) 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 Displaying a Specific Number of Lines at the Beginning of a Files $ head -6 file1 file2 file3 ==> file1 file2 file2 file or command >> file command < file command 2> file 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 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 ... Command to join two files into one $ cat filename1 filename2 > file3 This example joins filename1 and filename2 files into filename3 file Note – If filename3 is existed, this command will overwrite... program The format for the redirection of standard input, standard output, and standard error is: command > file or command >> file command < file command 2> file Angle Brackets ( >, ) Use the... practice/dir1/admin: Copying Files Use the cp command to copy files Command Format cp [-i] source _file destination _file cp [-i] source _file( s) destination _directory Copying a File to Another Within a Directory

Ngày đăng: 02/10/2013, 09:20

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan