File and User Information Utilities

18 385 0
File and User Information Utilities

Đ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

File and User Information Utilities Objectives Upon completion of this module, you should be able to: • Describe some advantages of using file systems • List the inode number of a file • Link files using hard and symbolic links • Employ the find command to locate files by specific criteria • Search for text within a document using the grep command • Identify which users are logged on to your system • Switch to a new user ID and identify the user ID you are currently using • Identify your effective and real UID when switching users • Use the sort command to sort ASCII files in alphabetical and numerical order • Use the sort command to perform multilevel sorts on ASCII data Discussion – What utilities do you need for daily computer use? Do you need to search for items, such as files? What other searches do you need to do? File Systems When the UNIX operating system was first developed, hard disks could store what is now considered a very small amount of data. When disks larger than 300 Mbytes arrived on the market the operating system could not address such a large amount of space. This necessitated the development of logical partitions of the hard drive to allow the kernel to access smaller, addressable parts of the drive. Today, with hard drives being sold with up to a Tbyte or more of space on them, the kernel can access all available space. Despite this, the convention of partitioning a hard drive into logical partitions has persisted for various reasons. Partitioning allows an administrator to functionally organize data so that user files are on a different logical partition than executables or applications. One of the main advantages to this is that daily backups can be done only to those partitions on which the data changes frequently, without having to back up all information on the disk. Logical partitioning also cuts down on seek time: if the disk is partitioned, when a user specifies a file to search for or a directory to change to, the name of the directory gives the kernel information about where the information is stored. This prevents the system from having to seek the information over the entire disk. These logical partitions are referred to as file systems. They are transparent to a user and therefore appear to be just part of the directory hierarchy. The most common file systems on a UNIX system are /usr, where binary and executables are stored, /opt, where third party applications are usually located, and root (/), where the files that pertain to the operation of the system are kept. Inode Numbers Inode numbers are identifiers of a file on a file system. Similar to the way in which a passport number is unique for each person in a country, an inode number is unique for each file in a file system. Each file and directory has an inode number assigned to it by the system. These numbers can be seen by using the following ls command: $ ls -i ~ 12110 dante 68349 dir3 12118 file3 12115 dante_1 68451 dir4 12119 file4 67773 dir1 12169 file1 68552 practice The numbers to the left of the file name are the inode numbers. These are sometimes referred to as index numbers, as the kernel keeps an index of the files and directories by the inode number and can therefore refer to them faster. Note – Inode numbers are unique on each file system, even when file names are identical. Linking Files and Directories Links Links are used to create alternate names or aliases for files and directories on a system. In this way, different users can refer to the same file or directory by names they are more comfortable with or names that are shorter. Many system sadministrators set up links to commands, giving them names more familiar to users of different operating systems. There are two kinds of links: hard and symbolic (or soft). Hard Links Hard links are used to link files on the same file system. Files that are hard linked share the same inode number and, therefore, refer to the same data on the hard disk. Hard links are not used to link directories and cannot cross file systems, as the inode number is only unique for that file in its current file system. A completely different file may have the same inode number on a different file system. The output of the ls -l command shows a link count following the permissions. This is a count of how many files are hard linked to the same inode number as the file listed. $ ls -l ~ -rw------- 1 torey staff 1320 Oct 19 dante -rw------- 1 torey staff 368 Oct 19 dante_1 drwx--x--x 5 torey staff 512 Oct 19 dir1 drwx--x--x 4 torey staff 512 Oct 19 dir2 <output omitted> The link count on directories includes a link to the current directory (.) and from the parent ( ) directory, and a number for each file or subdirectory included in the directory. The structure of a hard link is as follows: File1 Inode number Data Display File2 File3 All hard-linked files share the same inode number and therefore the same data. This data can be displayed using an appropriate command; for example, cat or more. In the case of hard links, as long as one file that refers to the inode number remains, the data remains available on the system. For this reason, File1 and File3 could be deleted, leaving the information referred to intact, as File2 would still exist. Note – Hard links cannot be used to link directories; only symbolic links can be used to do so. Symbolic Links Symbolic links are used for two main reasons: to link a file or directory across file systems, or for backwards compatibility. Since symbolically linked files do not share a single inode, these links can cross file system boundaries. There are also many cases where commands, files, or directories that have existed as part of the UNIX operating system for years are given different names. In order to avoid having to retrain the multitude of UNIX users in the world, symbolic links are employed to refer to these files by both their old and new names. This is referred to as backwards compatibility. The following commands indicate that the file symlink is a symbolically linked file: $ ls -l Test -rw-r--r-- 1 torey staff 35 May 8 linktest lrwxrwxrwx 1 torey staff 8 May 12 symlink--> linktest $ ls -F linktest symlink@ The structure of a symbolic link is as follows: File1 inode number Data Display File2 inode number Absolute pathname to File1 The data contained in File2 is the absolute pathname to File1, but displaying either File1 or File2 will produce the same output. If File2 is deleted, there is no effect on File1. If File1 is deleted File2 will still exist, but it will point to an invalid file name and therefore be of no practical use. The ln Command Use the ln command to create hard or symbolic links. Command Format ln [-s] source_file target_file By default, the ln command will create a hard link. The -s option is used to create a symbolic link. The source_file is the existing file and the target_file is the new file to be linked to the source_file. Creating Links You can link two files with a : • Hard link $ ln /export/home/user2/dante essay $ ls -i /export/home/user2/dante 89532 dante $ ls -i essay 89532 essay • Symbolic link $ ln -s tutor.vi symlink $ ls -l symlink lrwxrwxrwx 1 torey staff 8 May 9 symlink--->tutor.vi Finding Files The find Command The find command is one of the most powerful and useful of the commands available to a UNIX environment user. This command can be used to find files based on specific criteria. Once a file or group of files that matches a search criterion is found, another command can be executed on the matching files. The find command can be used for many purposes including deleting, backing up, or printing files. Command Format find path expression [ action] path Names the directory where the search begins. Expression The search criteria is specified by one or more values. If more than one expression is specified, find treats the statement as an “and” request and all listed expressions must be verified as true or present. Many search expressions will require a value to match and in some cases metacharacters, or wildcards, may be used for the arguments. The expressions used with the find command evaluate as true or false. Actions -exec command {} \; The exec option must be terminated by { } \; which allows find to apply the specified command to each file that it identifies from the search criteria. -ok command {} \; Interactive form of -exec. This option is used with commands that require input from the user; for example, rm -i. -ls Prints the current path name using the long listing format. This expression is most commonly used in conjunction with a redirection of output to a file in order for the listing to be examined at a later time. The find command has several additional features that can be used to further define search criteria. Consult man pages for further details. Some additional features: -o Allows for an “or” type of criteria definition -a Allows for an “and” type of criteria definition Using the find Command You can: • Search for openwin starting at the /usr directory $ find /usr -name openwin /usr/openwin /usr/openwin/bin/openwin • Search for files ending in tif starting at the /usr directory $ find /usr -name ’*tif’ /usr/openwin/demo/kcms/images/tiff/ireland.tif /usr/openwin/demo/kcms/images/tiff/new_zealand.tif • Search for core files starting at the user’s home directory and delete them $ find ~ -name core -exec rm {} \; • Look for all files, starting at the current directory, that have not been modified in the last 90 days $ find . -mtime +90 <find output omitted> • Find files larger than 400 blocks (512-byte blocks) starting at /etc $ find /etc -size +400 <find output omitted> Additional Features of the find Command You can: • Find files with open permissions starting at the user’s home directory $ find ~ -perm 777 -depth > holes • Find files owned by a specific user or group in the current directory hierarchy, list them, and put the listing in a file for later viewing $ find . -user billw -o -group staff -ls > review • Find files, starting at /etc, which share the same inode number $ find /etc -inum 769 <find output omitted> The grep Command Use the grep command to search a file for a specified text string. A string is one or more characters; it can be a character, a word, or a sentence. A string can include white space or punctuation if these are enclosed in quotations. The grep command searches a file for a character string and prints all lines that contain that pattern to the screen. The grep command can be used as a filter with other commands. The grep command is case sensitive. You must match the pattern with respect to uppercase and lowercase letters, unless you use the –i option. Command Format grep [option(s)] string filename Options -i Ignore case of string when searching -v Search for all lines that do not match string The following examples show how to search lines in a file or standard output $ grep root /etc/passwd root:x:0:1:Super-User:/:/sbin/sh $ ls -la | grep -i ’jun 11’ drwxr-xr-x 3 user1 staff 512 Jun 11 13:13 dir4 If the date is a single numeral date, the grep string needs to have two spaces between the month and the numeral; for example, Jun 3. Switching to Another User Account Using the su Command You can temporarily switch to another user account to have access and privileges to files and directories that belong to that user by using the su command. To switch user IDs, you must supply the password of the user ID you are switching to. While using the user ID you switched to, you will not have access to your previous privileges, only those of the new user ID. To change to your previous user ID, type exit. Command Format su [-] username Using the su Command to Become Another User $ su guest Password: $ pwd /home/user2 $ cd $ pwd /home/guest $ exit When su command is used without username , you want to become root user. When su is used without options, you will remain in the directory you were in when you switched user IDs. Environmental settings customized for your user ID will also remain in effect. To switch to another UID and have the system read the new user’s initialization files, you must use a dash (-) between the command and the new user ID. $ su - guest Password: $ pwd /home/guest $ Using the id Command Use the id command to display the user name corresponding to the effective user ID. Command Format id [option(s)] Displaying Your Current UID $ id uid=102(user2) gid=10(staff) groups=10(staff) $ su - guest Password: $ pwd /home/guest $ id uid=115(guest) gid=10(staff) groups=10(staff) 14(sysadmin) $ The listing for gid identifies the user’s primary group and the groups listing identifies all groups to which the user belongs. Using the who Command The who command displays information about all users currently logged on the local system. This command lists the user’s name, terminal line, login time, and elapsed time since the last activity on the terminal line. Command Format who [option(s)] Displaying Users on the System $ who user2 console May 24 10:17 (machine name) user3 pts/4 May 24 17:36 (machine name) $ Use the who -H option to print column headings above the regular output. $ who -H USER LINE LOGIN_TIME FROM user2 console May 24 10:17 (machine name) user3 pts/4 May 24 17:36 (machine name) $ Use the who -q option to display only the names and the number of users currently logged on. $ who -q user2 user3 # users=2 $ Using the who am I and whoami Commands who am i The who am i command displays information about your real user ID (RUID). If you use the su command to switch from one UID to another, the who am i command will display your original login UID. The who am i command lists the user name, the terminal line, and the date and time logged on. Command Format who am i Displaying Your EUID and RUID Information Display your effective UID (EUID) $ id uid=0(root)gid=0(root)groups=0(root),1(bin),2(daemon) Display your real UID $ who am i user2 console May 24 10:17 (hostname) $ whoami The whoami command displays only the login name of the effective user. Command Format whoami $ whoami guest [...]... find command to locate files by specific criteria Search for text within a document using the grep command      Identify which users are logged on to your system Switch to a new user ID and identify the user ID you are currently using Identify your effective and real UID when switching users Use the sort command to sort ASCII files in alphabetical and numerical order Use the sort command to perform... work This command would read as “Do a Month order sort beginning with the sixth field Do a second-level sort in numeric order and begin on the seventh field (this will sort the numeric day of the month correctly), name the output file update.list.” Exercise: Using File and User Information Utilities Exercise objective – In this exercise you will practice using commands to find files and text Tasks... displayed 14 What command do you type to display the user ID and all groups that you belong to? $ id –a It ‘s the same id command To create a file for use in the next step, type: $ ls -la > ls.output 15 Sort the ls.output file Produce a numerical listing by size of the files, in reverse order What command did you use? $ sort -rn +4 ls.output 16 Perform a multilevel sort of the ls.output file that places... who am i command What happened? _ 12 Type the id command What happened? _ 13 What command do you type to display the user ID and all groups that you belong to? To create a file for use in the next step, type: $ ls -la > ls.output 14 Sort the ls.output file Produce a numerical listing by size of the files, in reverse... 105 Oct 19 1998 file2 -rw - 1 melissao staff 218 Oct 19 1998 file3 -rw - 1 melissao staff 1696 Oct 19 1998 file1 The first example represents beginning a sort on some field other than the first, and shows a numeric, reverse-order example The sort command line would read as, “Do a reverse order, numeric sort on the fifth field of the data in the file list, and place the output into a file called num.list.”... uucp in the /etc/group file 7 Using ls and grep, display all the files created today 8 Using the grep command, look for all lines in the file4 file located in your home directory that do not contain the letter "M" 9 Identify who is logged on to the system 10 Switch to the guest account or another user s account as specified by your instructor in such a way that you are in that user s environment 11... regular expression in the grep command indicates the first character in a line (For example, $ grep ^c file will look for lines that start with a “c” in the named file. ) Using the file created in the workshop lab for Module 6, generate a file that contains the shell commands that start with the letters “a” though “f”, regardless of case (upper or lower) Sort this file alphabetically using the second... workplace Exercise Solutions Complete the following steps and write the commands used to perform each task in the space provided 1 Use the find command to search the /usr directory and display the file names of any length that end with ln $ find /usr -name ’*ln’ 2 Use the ls -li command to answer the following questions:  How many links are there to the file /usr/bin/zgrep ? Three or four, depending upon... Using the grep command, look for all lines in the file4 file located in your home directory that do not contain the letter "M" $ grep -v 'M' file4 10 Identify who is logged on to the system $ who 11 Switch to the guest account in such a way that you are in that user s environment $ su - guest 12 Type the who am i command What happened? Your real UID (RUID) is displayed 13 Type the id command What happened?... commands to find files and text Tasks Complete the following steps and write the commands used to perform each task in the space provided 1 Use the find command to search the /usr directory and display the file names of any length that end with ln 2 Use the ls -li command to answer the following questions:  How many links are there to the file /usr/bin/zgrep? _  Are these hard . names and the number of users currently logged on. $ who -q user2 user3 # users=2 $ Using the who am I and whoami Commands who am i The who am i command. file update.list.” Exercise: Using File and User Information Utilities Exercise objective – In this exercise you will practice using commands to find files

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