UNIX Unleashed, System Administrator''''s Edition phần 2 ppt

95 239 0
UNIX Unleashed, System Administrator''''s Edition phần 2 ppt

Đ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

input to ed, the output is written back to the file on which it is applied. If we execute more on the file diffout, you can see the extra line at the end: more diffout 6,8c This is the seventh line This is the eighth line This is the ninth line . 4d 2a This is the third line . w To update file1 and see the result, you now use the following command: ed - file1 < diffout more file11 This is the first line This is the second line This is the third line This is the fourth line This is the sixth line This is the seventh line This is the eighth line This is the ninth line If you do not want to update the original file, use the command 1,$p instead of w. This generates the output to the standard output instead. You can also redirect the output to a file as follows: ed - file1 < diffout > file1.new To generate output in reverse order to that specified by -e, execute the following command with the -f flag: a2 This is the third line . d4 c6 8 This is the seventh line This is the eighth line This is the ninth line . If you do not care about whether the files have lowercase or uppercase letters, use the -i flag to execute case-insensitive diff as in the following command: diff -i file1 file2 2a3 > This is the third line 4d4 <Ti s the fifth line 6,7c6,7 < This is the seventh line < This is the eighth line > This is the seventh line > This is the eighth Notice that the lines This is the ninth line in file1 and This is the ninth line in file2 are evaluated to be equal due to use of the -i flag. If you want to know the number of lines affected by each insertion or deletion, use the -n flag as in the following command: diff -n file1 file2 a2 1 This is the third line d4 1 d6 3 a8 3 This is the seventh line This is the eighth line This is the ninth line The information in the above lines is with respect to file1. It tells you that one line is to be inserted after line 2, followed by the lines to be inserted, one line is to be deleted at line 4, three lines are deleted at line 6, and 3 lines are to be inserted after line 8, followed by lines to be inserted. To ignore all tab and space characters, use the -w flag. The difference between the -b and -w flags is that -b ignores all space and tab characters except leading ones, while -w ignores all. Following is an example of the -w flag: diff -w file1 file2 2a3 > This is the third line 4d4 < This is the fifth line 8c8 < This is the NINTH line > This is the ninth line So far, we have seen the actions of the diff command for comparing two files. Now let us see some examples of comparing two directories. Let us assume that the two following sub-directories exist under the current directory: testdir1 and testdir2 Further, let us see what files exist under these directories: ls -R test* testdir1: file1 file2 file3 file4 file5 file6 testdir3 testdir1/testdir3: filea fileb filec filed filee testdir2: file2 file4 file5 file7 file8 testdir3 testdir2/testdir3: fileb filed filee filef fileg The simplest form of the diff command without any flags to compare two directories will result in the following output: diff testdir1 testdir2 Only in testdir1: file1 Only in testdir1: file3 Only in testdir1: file6 Only in testdir2: file7 Only in testdir2: file8 Common subdirectories: testdir1/testdir3 and testdir2/testdir3 In the above example, the diff command does not go through the sub-directory testdir3 under the directory testdir1 and testdir2. If you want the diff command to traverse the sub-directory under the directories, use the -r flag as in the following command: diff -r testdir1 testdir2 Only in testdir1: file1 Only in testdir1: file3 Only in testdir1: file6 Only in testdir2: file7 Only in testdir2: file8 Only in testdir1/testdir3: filea Only in testdir1/testdir3: filec Only in testdir2/testdir3: filef Only in testdir2/testdir3: fileg If you want to know a list of all files in the directories that are identical, use the -s command as in the following command: diff -rs testdir1 testdir2 Only in testdir1: file1 Files testdir1/file2 and testdir2/file2 are identical Only in testdir1: file3 Files testdir1/file4 and testdir2/file4 are identical Files testdir1/file5 and testdir2/file5 are identical Only in testdir1: file6 Only in testdir2: file7 Only in testdir2: file8 Only in testdir1/testdir3: filea Files testdir1/testdir3/fileb and testdir2/testdir3/fileb are identical Only in testdir1/testdir3: filec Files testdir1/testdir3/filed and testdir2/testdir3/filed are identical Files testdir1/testdir3/filee and testdir2/testdir3/filee are identical Only in testdir2/testdir3: filef Only in testdir2/testdir3: fileg If you do not want to process all files whose names collate before the specified filename (in this case file2), use the -S flag as in the following command: diff -r -S file2 testdir1 testdir2 Only in testdir1: file3 Only in testdir1: file6 Only in testdir2: file7 Only in testdir2: file8 Only in testdir1/testdir3: filea Only in testdir1/testdir3: filec Only in testdir2/testdir3: filef Only in testdir2/testdir3: fileg diff3 The diff command compares two files. If you want to compare three files at the same time, use the diff3 command. The diff3 command writes output to the standard output that contains the following notations to identify the differences: ==== means all three files differ.● ====1 means the first file differs.● ====2 means the second file differs.● ====3 means the third file differs.● The following is a list of flags that can be used with the diff3 command: -3 to produce an edit script that contains only lines containing the differences from the third file.● -E, -X to produce an edit script where the overlapping lines from both files are inserted by the edit script, bracketed by <<<<<< and >>>>>> lines. ● -e to create an edit script that can be input to the ed command to update the first file with differences that exist between the second and third (that is, the changes that normally would be flagged ==== and ====3). ● -x to produce an edit script to incorporate only changes flagged ====.● The format of the generated output is as follows: File Id:Number1 a means that lines are to be added after line Number1 in the file File Id. The File Id can be 1, 2, or 3, depending on the file it is referring to. This is followed by the lines to be added. ● File Id:Number1[,Number2]c means that lines in the range Number1 through Number2 are to be modified. This is followed by the lines to be modified. ● Examples Let us assume that we have three files: file1, file2 and file3. The contents of these three files are shown below using the more command: more file1 This is the first line in first file This is the second line This is the third line This is the fourth line This is the fifth line This is the sixth line This is the seventh line This is the eighth line This is the ninth line more file2 This is the first line This is the second line This is the third line This is the 3.5th line This is the fourth line This is the sixth line in second file This is the seventh line This is the eighth line This is the ninth line more file3 This is the first line This is the second line This is the third line This is the fourth line This is the sixth line in third file This is the seventh line This is the eighth line This is the ninth line This is the tenth line This is the eleventh line Now execute diff3 on these three files without using any flag, as in the command below: diff3 file1 file2 file3 ====1 1:1c This is the first line in first file 2:1c 3:1c This is the first line ====2 1:3a 2:4c This is the 3.5th line 3:3a ==== 1:5,6c This is the fifth line This is the sixth line 2:6c This is the sixth line in second file 3:5c This is the sixth line in third file ====3 1:9a 2:9a 3:9,10c This is the tenth line This is the eleventh line The first group of lines starting with ====1 show that line 1 of file1 is different from the line 1 of file2 and file3. The lines starting with ====2 show that line 4 in file2 should be inserted after line 3 of file1 and file3 to make them identical. The lines starting with ==== show that line 5, 6 of file1, line 6 of file2 and line 5 of file3 are all different. The lines starting with ====3 show that line 9, 10 of file3 should be inserted after line 9 of file1 and file2 to make them identical. If you are interested in only finding out the differences in file3, use the -3 flag as in the following command: diff3 -3 file1 file2 file3 9a This is the tenth line This is the eleventh line . w q This tells that there are two lines lines 9 and 10 that are present in file3 but not in file1 or file2. If you want to apply changes between file2 and file3 to file1, use the -e flag to create an edit script as in the following command: diff3 -e file1 file2 file3 9a This is the tenth line This is the eleventh line . 5,6c This is the sixth line in third file . w q This output means that file3 has two extra lines at line 9 and line 6 of file2 has been replaced by line 5 of file3. If, however, you are interested in changes, use the -x flag as in the following command: diff3 -x file1 file3 file2 5,6c This is the sixth line in second file . w q dircmp If you want to compare the contents of two directories, use the dircmp command. This command compares the names of the files in each directory and generates a list of filenames that exist only in one of the directories followed by filenames that exist in both and whether they are identical or not. The following is a list of flags that can be used with dircmp command: -d to generate a list of files that exist in either of the directories followed by a list of files that exist in both and whether they are identical or different. This is further followed by output of diff command on pairs of files that are different ● -s to generate a list of files that exist in either of the directories followed by list of files that are different● Examples Let us assume that the there are two directories testdir1 and testdir2 in the current directory. The list of files in these directories are as follows: ls testdir1 file1 file2 file3 file4 file5 file6 ls testdir2 file2 file3 file5 file6 file7 file8 If you want to do a plain vanilla dircmp between these two directories, execute the following command: dircmp testdir1 testdir2 Fri Nov 29 22:51:34 1996 testdir1 only and testdir2 only Page 1 ./file1 ./file7 ./file4 ./file8 Fri Nov 29 22:51:34 1996 Comparison of testdir1 and testdir2 Page 1 directory . different ./file2 same ./file3 same ./file5 same ./file6 The first part of the above report shows the files present only in testdir1 on the left and only in testdir2 on the right. The second part shows a comparison of the directories and also shows which files are identical and which are different. If you want further information on what are the differences between the files file2 in these directories, use the -d flag as in the following command: testdir -d testdir1 testdir2 Fri Nov 29 22:56:01 1996 testdir1 only and testdir2 only Page 1 ./file1 ./file7 ./file4 ./file8 Fri Nov 29 22:56:01 1996 Comparison of testdir1 and testdir2 Page 1 directory . different ./file2 same ./file3 same ./file5 same ./file6 Fri Nov 29 22:56:01 1996 diff of ./file2 in testdir1 and testdir2 Page 1 1c1 < This file is in testdir1 > This file is in testdir2 If you want only a list of files that are unique to each directory and files that are different, use the -s flag as in the following command: dircmp -s testdir1 testdir2 Fri Nov 29 23:39:59 1996 testdir1 only and testdir2 only Page 1 ./file1 ./file7 ./file4 ./file8 Fri Nov 29 23:39:59 1996 Comparison of testdir1 and testdir2 Page 1 different ./file2 If you want to suppress the display of identical files, but you want list of the files that are different and the difference between these files, execute the dircmp command with both -s and -d flags. sdiff The command sdiff compares two files and displays output on the standard output in a side-by-side format. Following is the detail of the display format: if the two lines are identical then displays each line of the two files with a series of spaces between them● if the line only exists in the first file, then a < (less than sign) is displayed at the end of the line● if the line only exists in the second file, then a > (greater than sign) is displayed at the beginning of the line● if the lines from the two files are different, then a | (vertical bar) is displayed between the lines● The flags that can be used with sdiff command are as follows: -s if you do not want to display the identical lines● -w number to set the width of the display to number● -l to display only the line from the first file if the lines from the two● files are identical● -o file to create a merged file from the first and second file depending on a number of sub-commands you can specify ● Examples Let us assume that we have two files file1 and file2 whose contents are displayed below using the more command: more file1 This is the first line in first file This is the second line This is the third line This is the fourth line This is the fifth line This is the sixth line This is the seventh line This is the eighth line This is the ninth line more file2 This is the first line This is the second line This is the third line This is the 3.5th line This is the fourth line This is the sixth line in second file This is the seventh line This is the eighth line This is the ninth line If you execute sdiff command on the two files file1 and file2, we get the following result: sdiff file1 file2 This is the first line in first file | This is the f irst line This is the second line This is the s econd line This is the third line This is the t hird line > This is the 3 .5th line This is the fourth line This is the f ourth line This is the fifth line | This is the s ixth line in second file This is the sixth line < This is the seventh line This is the s eventh line This is the eighth line This is the e ighth line This is the ninth line This is the n inth line If, however, you do not want to display the identical lines, use the -s flag as in the following command: sdiff -s file1 file2 This is the first line in first file | This is the f irst line > This is the 3 .5th line This is the fifth line | This is the s ixth line in second file This is the sixth line < You can use the -l to display only the line from the first file if the lines are identical so that the other lines stand out as in the following command: sdiff -l file1 file2 This is the first line in first file | This is the f irst line This is the second line This is the third line > This is the 3 .5th line This is the fourth line This is the fifth line | This is the s ixth line in second file This is the sixth line < This is the seventh line This is the eighth line This is the ninth line File Manipulation Commands Here we will discuss several commands that can be used to manipulate various attributes of one or more files, as well as to copy and move files from one location to another. The various attributes that can be manipulated include modification time, permission, and more. touch The touch command can be used for a number of purposes depending on whether a file already exists. If a file does not exist, the touch command will create it if you have write access to the directory. If a file is already present, the touch command modifies the last modification time of the file. Examples To create a file called testfile in the current directory, execute the following command: touch testfile To create testfile in the /u/testuser/testdir, execute the following command: touch /u/testuser/testdir/testfile chmod You may need to modify the permission of a directory or files either to secure them or to make them accessible to others. You can use the chmod command to modify the permission or files and directories. The permission in UNIX is specified in octal number (0 thorough 7). Permission for a file or directory can be specified for the following: Owner The user who created the file● Group The group to which the owner belongs● World or others Users other than the owner and users in the group to which the owner belongs● For each of these, one octal number is specified to designate the permission. The permission for the owner, group, and world is derived on the basis of three bits associated with read, write, and execute authority for the file. That is, the bit for read will have a value of one if read permission is to be granted, the bit for write will have a value of one if write permission is to be granted, and the bit for execute will have a value of one if execute permission is to be granted. You should be aware that the execute bit functions differently for directories. The execute permission for a directory is used to designate whether you are able to access that directory. The combination of these three bits is expressed as an octal number and is used to designate the permission. The weight associated with the read bit is 4, the weight associated with write is 2, and the weight associated with execute is 1. The value of the permission is derived as follows: (4 * value of read bit) + (2 * value of write bit) + (1 * value of execute bit) The value of the permission can vary from 0 (no read, write, or execute permission) to 7 (read, write, and execute permission). For example, if you want to provide read and write permission but no execute permission, then the value to be used will be: (4 * 1) + (2 * 1) + (1 * 0) = 6 You should be aware that execute permission on a directory means that the directory can be accessed. That is, operations can be performed on files residing in that directory. If you provide write permissions to a directory, the user will be able to read, write, delete, and execute all files in that directory, irrespective of the permissions of the individual files. With the chmod command, you specify the new permissions you want on the file or directory. The new permission can be specified in one the following two ways: As a three-digit numeric octal code● As symbolic mode● Examples If you want testfile to have the permission: Owner with read, write, execute; group with read only; and others with execute only, you must execute the following command: chmod 741 testfile While using the symbolic mode, the following will need to be specified: Whose (owner, group, or others) permission you want to change.● What operation (+ (add), - (subtract), or = (equals)) you want to perform on the permission.● The permission (r, w, x, and so on).● If you want to setup the permission for testfile owned by you in the current directory so that only you and users in your group can read and write the file, execute the following command using absolute permissions: chmod 660 testfile If you want to add write permission for the group for testfile in the current directory (assuming that currently testfile has 741 permission), execute the following command: chmod g+w testfile Similarly, if you want to revoke the read permission for others for testfile in the current directory, execute the following command: chmod o-r testfile If you want to grant the same permissions to the world (other) as the group for testfile in the current directory, execute the following command: chmod o=g testfile NOTE: Modifying the permissions will not have any effect on the root user. The root user has access to all files and directories irrespective of the permissions you may have granted. chgrp If you want to change the group to which the file belongs, use the chgrp command. The group must be one of the groups to which the owner belongs. That is, the group must be either the primary group or one of the secondary groups of the owner. Examples Assume user testuser owns the file testfile, and the group of the file is staff. Also assume that testuser belongs to the groups staff and devt. To change the owner of testfile from staff to devt, execute the following command: chgrp devt testfile chown In case you want to change the owner of a file or directory, use the chown command. CAUTION:: On UNIX systems with disk quotas, only the root user can change the owner of a file or directory. Examples If the file testfile is owned by the user called testuser, to change ownership of the file to user friend, you must execute the following command: chown friend testfile rm Once you are done using a file and you do not want to use it any more, then you would like to remove the file so as to regain the space utilized by the file. The rm command lets you do this by removing files permanently from the disk. If an entry is the last link to a file, the file is deleted. To remove a file from a directory, you do not need either read or write permission to the file, but you do need write permission to the directory containing the file. The rm command is usually used to remove files, but it provides a special flag -r to remove files in a directory recursively, including the directory and its sub-directories. Following is a list of some of the flags that can be used with the rm command: -i to interactively remove the files.● -f to remove the files without any messages. This will not generate any messages for cases where a file does not exist or you do not have permission to remove one or more files. ● -r to remove files within a directory and directories themselves recursively.● The native version of the rm command does not ask for confirmation while removing files. So you should be careful when [...]... 007 0 62 147 21 1 007 000 000 000 000 000 000 000 000 001 337 \0 007 2 g 21 1 007 \0 \0 \0 \0 \0 \0 \0 \0 01df 0007 326 7 8907 0000 0000 0000 0000 0000 020 000 110 020 007 001 013 000 001 000 0 02 050 25 0 000 001 135 27 4 \0 H 020 007 001 013 \0 001 \0 0 02 ( 25 0 \0 001 ] 27 4 0048 1007 010b 0001 00 02 28a8 0001 5dbc 0000040 000 0 12 061 104 000 001 124 320 000 000 0 02 000 000 000 000 000 \0 \n 1 D \0 001 T 320 ... testuser author 647 Nov 24 18: 32 /test5 18 4 -rw - 1 testuser author 10 Nov 24 17:36 /test1 19 0 -r-x 1 testuser author 0 Nov 24 14:58 /test3 20 0 -rw-r r 1 testuser author 0 Nov 24 17:33 /test4 67584 4 drwxr-xr-x 2 testuser author 5 12 Nov 24 17: 32 /test2 67585 0 -rw-r r 1 testuser author 0 Nov 24 17:45 /test2/test21 67586 0 -rw-r r 1 testuser author 0 Nov 24 14:58 /test2/test 22 22 0 -rw-r r 1 testuser... testuser author 10 Nov 24 17:36 test1 -rw-r r-1 testuser author 0 Nov 24 14:58 test3 -rw-r r-1 testuser author 0 Nov 24 17:33 test4 -rw-r r-1 testuser author 11885 Nov 24 11:50 test5 test2: total 16 drwxr-xr-x drwxrwx -rw-r r rw-r r 2 3 1 1 testuser testuser testuser testuser author author author author 5 12 30 72 0 0 Nov Nov Nov Nov 24 24 24 24 17: 32 17:41 17:45 14:58 test21 test 22 Notice that the entries... 64 drwxrwx 3 testuser author 30 72 Nov 25 00:41 drwxr-xr-x 36 root system 20 48 Nov 23 19:51 -rw-r r-1 testuser author 0 Nov 24 14:54 dot1 -rw 1 testuser author 10 Nov 24 17:36 test1 drwxr-xr-x 2 testuser author 5 12 Nov 24 17: 32 test2 -r-x -1 testuser author 0 Nov 24 14:58 test3 -rw-r r-1 testuser author 0 Nov 24 17:33 test4 -rw-r r-1 testuser author 15647 Nov 24 18: 32 test5 In its simplest form, you... total 40 -rw-r r-1 testuser author 10 Nov 24 17:36 test1 drwxr-xr-x 2 testuser author 5 12 Nov 24 17: 32 test2 -rw-r r-1 testuser author 0 Nov 24 14:58 test3 -rw-r r-1 testuser author 0 Nov 24 17:33 test4 -rw-r r-1 testuser author 11885 Nov 24 11:50 test5 /test2: total 0 -rw-r r rw-r r 1 testuser 1 testuser author author 0 Nov 24 17:45 test21 0 Nov 24 14:58 test 22 Following are examples of the ls command... line 23 This is the line 24 This is the line 25 This is the line 26 This is the line 27 This is the line 28 This is the line 29 This is the line 30 If you want to see the last 10 lines of the file, execute the tail command without any flags as follows: tail file1 This is the line 21 This is the line 22 This is the line 23 This is the line 24 This is the line 25 This is the line 26 This is the line 27 ... -rw-r r-1 testuser author 11885 Nov 24 11:50 test5 -rw-r r-1 testuser author 0 Nov 24 17:33 test4 -rw-r r-1 testuser author 0 Nov 24 14:58 test3 drwxr-xr-x 2 testuser author 5 12 Nov 24 17: 32 test2 -rw-r r-1 testuser author 10 Nov 24 17:36 test1 -rw-r r-1 testuser author 0 Nov 24 14:54 dot1 drwxr-xr-x 36 root system 20 48 Nov 23 19:51 drwxrwx 3 testuser author 30 72 Nov 24 18:00 To obtain a list of all... /test4 /test2/test21 /test2/test 22 /.dot1 If you want a list of all files that have zero size, execute the find command with the -exec parameter as follows: find -size 0c -exec ls -l {} \; -r-x 1 testuser author 0 Nov 24 14:58 /test3 -rw-r r 1 testuser author 0 Nov 24 17:33 /test4 -rw-r r 1 testuser author 0 Nov 24 17:45 /test2/test21 -rw-r r 1 testuser author 0 Nov 24 14:58 /test2/test 22 -rw-r... its sub-directories find -print /test5 /test1 /test3 ./test4 /test2 /test2/test21 /test2/test 22 /.dot1 If you want to search for all the files in the current directory that have been modified in the last 24 hours, use the -mtime operator as follows: find -mtime 0 -print /test5 /test1 /test3 /test4 /test2 /test2/test21 /test2/test 22 /.dot1 To search for a file whose permission is 600 (only owner has... -rw-r r-1 testuser author 10 Nov 24 17:36 test1 -rw-r r-1 testuser author 0 Nov 24 17:33 test4 drwxr-xr-x 2 testuser author 5 12 Nov 24 17: 32 test2 -rw-r r-1 testuser author 0 Nov 24 14:58 test3 -rw-r r-1 testuser author 0 Nov 24 14:54 dot1 -rw-r r-1 testuser author 11885 Nov 24 11:50 test5 drwxr-xr-x 36 root system 20 48 Nov 23 19:51 Until now, we have not specified any file or directory name in the ls . command: dircmp testdir1 testdir2 Fri Nov 29 22 :51:34 1996 testdir1 only and testdir2 only Page 1 ./file1 ./file7 ./file4 ./file8 Fri Nov 29 22 :51:34 1996 Comparison of testdir1 and testdir2 Page 1 directory. ./file8 Fri Nov 29 22 :56:01 1996 Comparison of testdir1 and testdir2 Page 1 directory . different ./file2 same ./file3 same ./file5 same ./file6 Fri Nov 29 22 :56:01 1996 diff of ./file2 in testdir1. 30 72 Nov 24 17:35 . drwxr-xr-x 36 root system 20 48 Nov 23 19:51 -rw-r r 1 testuser author 0 Nov 24 14:54 .dot1 -rw-r r 1 testuser author 10 Nov 24 17:36 test1 drwxr-xr-x 2 testuser author 512

Ngày đăng: 14/08/2014, 02:22