1.6 Files
Although arguments to commands aren't always files, files are the most important types of "things" on any UNIX system. A file can contain any kind of information, and indeed there are different types of files. Three types are by far the most important:Regular files Also called text files; these contain readable characters. For example, this book was created from several regular files that contain the text of the book plus human-readable formatting instructions to the troff word processor.Executable files Also called programs; these are invoked as commands. Some can't be read by humans; othersthe shell scripts that we'll examine in this bookare just special text files. The shell itself is a (non-human-readable) executable file called bash.Directories These are like folders that contain other filespossibly other directories (called subdirectories).1.6.1 Directories
Let's review the most important concepts about directories. The fact that directories can contain other directories leads to a hierarchical structure, more popularly known as a tree, for all files on a UNIX system.Figure 1.2 shows part of a typical directory tree; rectangles are directories and ovals are regular files.Figure 1.2. A tree of directories and files

1.6.1.1 The working directory
Of course, it's annoying to have to use full pathnames whenever you need to specify a file. So there is also the concept of the working directory (sometimes called the current directory), which is the directory you are "in" at any given time. If you give a pathname with no leading slash, then the location of the file is worked out relative to the working directory. Such pathnames are called relative pathnames; you'll use them much more often than full pathnames.When you log in to the system, your working directory is initially set to a special directory called your home (or login) directory. System administrators often set up the system so that everyone's home directory name is the same as their login name, and all home directories are contained in a common directory under root.For example, /home/cam is a typical home directory. If this is your working directory and you give the command lp memo, then the system looks for the file memo in /home/cam. If you have a directory called hatter in your home directory, and it contains the file teatime, then you can print it with the command lp hatter/teatime.1.6.1.2 Tilde notation
As you can well imagine, home directories occur often in pathnames. Although many systems are organized so that all home directories have a common parent (such as /home or /users), you should not rely on that being the case, nor should you even have to know the absolute pathname of someone's home directory.Therefore, bash has a way of abbreviating home directories: just precede the name of the user with a tilde (~). For example, you could refer to the file story in user alice's home directory as ~alice/story. This is an absolute pathname, so it doesn't matter what your working directory is when you use it. If alice's home directory has a subdirectory called adventure and the file is in there instead, you can use ~alice/adventure/story as its name.Even more convenient, a tilde by itself refers to your own home directory. You can refer to a file called notes in your home directory as ~/notes (note the difference between that and ~notes, which the shell would try to interpret as user notes's home directory). If notes is in your adventure subdirectory, then you can call it ~/adventure/notes. This notation is handiest when your working directory is not in your home directory tree, e.g., when it's some system directory like /tmp.1.6.1.3 Changing working directories
If you want to change your working directory, use the command cd. If you don't remember your working directory, the command pwd tells the shell to print it.cd takes as an argument the name of the directory you want to become your working directory. It can be relative to your current directory, it can contain a tilde, or it can be absolute (starting with a slash). If you omit the argument, cd changes to your home directory (i.e., it's the same as cd ~ ).Table 1.1 gives some sample cd commands. Each command assumes that your working directory is /home/cam just before the command is executed, and that your directory structure looks like Figure 1.2.1.6.2 Filenames, Wildcards, and Pathname Expansion
Sometimes you need to run a command on more than one file at a time. The most common example of such a command is ls, which lists information about files. In its simplest form, without options or arguments, it lists the names of all files in the working directory except special hidden files, whose names begin with a dot (.).If you give ls filename arguments, it will list those fileswhich is sort of silly: if your current directory has the files duchess and queen in it and you type ls duchess queen, the system will simply print those filenames.Actually, ls is more often used with options that tell it to list information about the files, like the -l (long) option, which tells ls to list the file's owner, size, time of last modification, and other information, or -a (all), which also lists the hidden files described above. But sometimes you want to verify the existence of a certain group of files without having to know all of their names; for example, if you use a text editor, you might want to see which files in your current directory have names that end in .txt.Filenames are so important in UNIX that the shell provides a built-in way to specify the pattern of a set of filenames without having to know all of the names themselves. You can use special characters, called wildcards, in filenames to turn them into patterns. Table 1.2 lists the basic wildcards. Table 1.2. Basic Wildcards | |
Wildcard | Matches |
? | Any single character |
* | Any string of characters |
[set] | Any character in set |
[!set] | Any character not in set |
Table 1.3. Using the * Wildcard | |
Expression | Yields |
fr* | frank fred |
*ed | ed fred |
b* | bob |
*e* | darlene dave ed fred |
*r* | darlene frank fred |
* | bob darlene dave ed frank fred |
d*e | darlene dave |
g* | g* |