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