Previous | Table of Contents | Next |
In addition to working with files and directories, shell scripts are often called on to manipulate the attributes of a file. In this chapter, you learn how to manipulate the following file attributes:
You will examine the different types of files available on UNIX systems and how to identify them.
UNIX supports several different types of files. Files can contain your important data, such as files from a word processor or graphics package, or they can represent devices, directories, or symbolic links. In this section, you will look at the different types of files available under UNIX.
To determine a files type, specify the -1 option to the 1s. When this option is specified, ls lists the file type for the specified files. For example, the command
$ ls -l /home/ranga/.profile
produces the following output:
-rwxr-xr-x 1 ranga users 2368 Jul 11 15:57 .profile*
Here, you see that the very first character is a hyphen (-). This indicates that the file is a regular file. For special files, the first character will be one of the letters given in Table 5.1.
Character | File Type |
---|---|
- | Regular file |
l | Symbolic link |
c | Character special |
b | Block special |
p | Named pipe |
s | Socket |
d | Directory file |
To obtain file type information about a directory, you must specify the -d option along with the -l option:
$ ls -ld /home/ranga
This produces the following output:
drwxr-xr-x 27 ranga users 2048 Jul 23 23:49 /home/ranga/
Ill provide the actual descriptions of each of these file types in the following sections.
Regular files are the most common type of files you will encounter. These files store any kind of data. This data can be stored as plain text, an application-specific format, or a special binary format that the system can execute.
UNIX does not have to understand the data contained in a regular file. A regular file can store any form of raw data because UNIX does not interpret the data that is in the file.
Note:
Often simply determining that a file is a regular file tells you very little about the file itself. Usually you need to know whether a particular file is a binary program, a shell script, or a library. In these instances, the file program is very useful.It is invoked as follows:
file filenameHere, filename is the name of the file you want to examine. As an example, on my system, the command
$ file /sbin/shproduces the following output:
/sbin/sh: ELF 32-bit MSB executable SPARC Version 1, ⇒statically linked, strippedHere you see that the file, /sbin/sh, is an executable program. Try it out on a few files to get an idea of the kind of information that it can give you.
A symbolic link is a special file that points to another file on the system. When you access one of these files, it has a pathname stored inside it. Use this pathname to advance to the file or directory on the system represented by the pathname stored in the symbolic link.
For readers who are familiar with Windows or Mac OS, a symbolic link is similar to a shortcut or an alias.
You can use symbolic links to make a file appear as though it is located in many different places or has many different names in the file system. Symbolic links can point to any type of file or directory.
The ls -l output for a symbolic link looks like this:
lrwxrwxrwx 1 root root 9 Oct 23 13:58 /bin/ -> ./usr/bin/
The output indicates that the directory /bin is really a link to the directory ./usr/bin.
The relative path in the output is not relative to your current working directory: it is relative to the directory where the link resides. In this case, the link /bin resides in the / directory, thus ./usr/bin indicates that /bin is a link to the directory /usr/bin.
Previous | Table of Contents | Next |