1. Trang chủ
  2. » Thể loại khác

Tài Liệu - Võ Tấn Dũng (votandung) Unit3 Linux ITC

51 101 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 5,93 MB

Nội dung

Information Technology College of HoChiMinh city Faculty of Information Technology Course: Fundamentals of Linux OS Unit File and Directory Commands Lecturer: Võ Tấn Dũng votandung@yahoo.com http://sites.google.com/site/votandungsg/ Objectives • 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 by the touch command Use the tee command to create text within a file • Create and remove directories using mkdir and rmdir • Manage files and directories using the mv,cp, and rm commands • Pass output from one command to another using a pipe VÕ TẤN DŨNG Displaying Files Using these commands: cat, more, head, tail, wc VÕ TẤN DŨNG 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) Example: $ 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 VÕ TẤN DŨNG Using the cat command (cont.) • Using the cat Command to join two files into one $ cat filename1 filename2 > file3 This example joins filename1 and filename2 files into filename3 file VÕ TẤN DŨNG 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 Command Format more filename(s) • At the More prompt, you can use the following keys to control the scrolling capabilities: VÕ TẤN DŨNG 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) In this example, the head -6 command displays the first six lines of the /usr/dict/words file $ head -6 /usr/dict/words 10th 1st 2nd 3rd 4th 5th $ VÕ TẤN DŨNG 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) In this example, the tail -5 command displays the last five lines of the /usr/dict/words file $ tail -5 /usr/dict/words In this example, the tail +23 command displays from line 23th through the end of the /usr/dict/words file $ tail +23 /usr/dict/words VÕ TẤN DŨNG Using the wc command Use the wc command to display a line, word, or character count of a file Command Format wc [ options] filename(s) $ wc dante 33 223 1320 dante $ wc -l dante 33 dante VÕ TẤN DŨNG Comparing Files Using these commands: cmp, diff VÕ TẤN DŨNG 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) VÕ TẤN DŨNG Use the rmdir command to remove an empty directory Example: $ cd $ pwd /home/user2 $ mkdir -p newdir/empty $ cd newdir $ ls -F empty/ $ rmdir empty $ ls $ VÕ TẤN DŨNG Use rm -r to remove a directory that is not empty Example: $ cd $ pwd /home/user2 $ rm -r letters $ ls Reports dir2 file1 fruit2 tutor.vi brands dir3 file2 games dante dir4 file3 monthly dante_1 feathers file4 newdir dir1 feathers_6 fruit ski.places VÕ TẤN DŨNG Use rm -ir to interactively remove directories Example: $ 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 VÕ TẤN DŨNG Redirection and Piping VÕ TẤN DŨNG Standard output, standard input • 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 VÕ TẤN DŨNG What does the redirection? • The Linux 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, 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 or command >> file command < file command 2> file VÕ TẤN DŨNG Angle brackets 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 VÕ TẤN DŨNG Redirecting output or error from a command to a file Example: $ ls /etc > etc.list $ ls baitap.txt 2> errorfile $ cat errorfile file not found VÕ TẤN DŨNG Redirecting the input of a file to a command Example: $ mailx user2@saturn < dante VÕ TẤN DŨNG Appending output using >> - Use the double right-angle bracket (>>) to append the output of a command to an existing file Example: VÕ TẤN DŨNG Piping - One of the most powerful meta characters is the pipe (|) - The pipe takes the standard output of one command and passes it as standard input into a following command VÕ TẤN DŨNG Displaying command output one screen at a time • One of the most common uses of the pipe meta character 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 Example: $ 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 VÕ TẤN DŨNG 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: Example: $ ls ~ | tee homedir | lp VÕ TẤN DŨNG END OF UNIT • remember to your homework (see http://sites.google.com/site/votandungsg/) VÕ TẤN DŨNG ... content, use Control-d to end the input of content Example: $ tee phone_list.txt Bill O - 80 8-5 5 5-9 876 Bill O - 80 8-5 5 5-9 876 Fred P - 80 8-5 5 5-6 543 Fred P - 80 8-5 5 5-6 543 ^D The -a option for the... dir3 $ cp -r planets /dir1/constellation $ cd $ cp -r dir1 ski.places /tmp $ ls -F /tmp dir1/ ski.places/ VÕ TẤN DŨNG Moving and rename files or directories Using this command: mv VÕ TẤN DŨNG Use... 223 1320 dante $ wc -l dante 33 dante VÕ TẤN DŨNG Comparing Files Using these commands: cmp, diff VÕ TẤN DŨNG Locating text differences with the cmp command - Comparing files to determine differences

Ngày đăng: 15/12/2017, 16:47

TỪ KHÓA LIÊN QUAN