Directory and Ordinary Files

 < Day Day Up > 

Like a family tree, the tree representing the filesystem is usually pictured upside down, with its root at the top. Figures 4-2 and 4-3 show that the tree "grows" downward from the root, with paths connecting the root to each of the other files. At the end of each path is either an ordinary file or a directory file. Ordinary files, or simply files, appear at the ends of paths that cannot support other paths. Directory files, usually referred to as directories or folders, are points that other paths can branch off from. (Figures 4-2 and 4-3 show some empty directories.) When you refer to the tree, up is toward the root and down is away from the root. Directories directly connected by a path are called parents (closer to the root) or children (farther from the root). A pathname is a series of names that traces a path along branches from one file to another.

Figure 4-3. Directories and ordinary files


Every file has a filename. The maximum length of a filename varies with the type of filesystem; Linux includes support for several types of filesystems. Most of today's filesystems allow you to create files with names up to 255 characters in length, but some older filesystems may restrict you to 14-character names. Although you can use almost any character in a filename, you will avoid confusion if you choose characters from the following list:

  • Uppercase letters (A Z)

  • Lowercase letters (a z)

  • Numbers (0 9)

  • Underscore ( _ )

  • Period (.)

  • Comma (, )

/ or root

The root directory is always named / (slash) and referred to by this single character. No other file can use this name or have a / in its name. However, in a pathname, which is a string of filenames including directory names, the slash separates filenames (page 83).

Like the children of one parent, no two files in the same directory can have the same name. (Parents give their children different names because it makes good sense, but Linux requires it.) Files in different directories, like children of different parents, can have the same name.

The filenames you choose should mean something. Too often a directory is filled with important files with such unhelpful names as hold1, wombat, and junk, not to mention foo and foobar. Such names are poor choices because they do not help you recall what you stored in a file. The following filenames conform to the suggested syntax and convey information about the contents of the file:

  • correspond

  • january

  • davis

  • reports

  • 2001

  • acct_payable

Filename length

When you share files with users on other systems, you may need to make long filenames differ within the first 14 characters. If you keep the filenames short, they are easy to type; later you can add extensions to them without exceeding the 14-character limit imposed by some filesystems. The disadvantage of short filenames is that they are typically less descriptive than long filenames. When you share files with systems running DOS or older versions of Windows, you must respect the 8-character filename body length and 3-character filename extension length imposed by those systems.

Long filenames enable you to assign descriptive names to files. To help you select among files without typing entire filenames, shells support filename completion. For more information about this feature, see the "Filename completion" tip on page 45.

You can use uppercase and/or lowercase letters within filenames. Linux is case sensitive, so files named JANUARY, January, and january represent three distinct files.

caution: Do not use SPACEs within filenames

Although you can use SPACEs within filenames, it is a poor idea. Because a SPACE is a special character, you must quote it on a command line. Quoting a character on a command line can be difficult for a novice user and cumbersome for an experienced user. Use periods or underscores instead of SPACEs: joe.05.04.26, new_stuff.

Filename Extensions

In the filenames listed in Table 4-1, filename extensions help describe the contents of the file. A filename extension is the part of the filename following an embedded period. Some programs, such as the C programming language compiler, depend on specific filename extensions. In most cases, however, filename extensions are optional. Use extensions freely to make filenames easy to understand. You can use several periods within the same filename for example, notes.4.10.01 or files.tar.gz.

Table 4-1. Filename extensions


A C programming language source file


The object code for the program


The same program as an executable file


A text file


A PDF file; view with xpdf under a GUI

A PostScript file; view with gs under a GUI


A file compressed with compress (page 58); use uncompress or gunzip (page 58) to decompress

memo.tgz or memo.tar.gz

A tar (page 58) archive of files compressed with gzip (page 58)


A file compressed with gzip (page 58); view with zcat or decompress with gunzip (both on page 58)


A file compressed with bzip2 (page 56); view with bzcat or decompress with bunzip2 (both on page 57)


A file meant to be viewed using a Web browser, such as Mozilla

photo.jpg or photo.gif

A file containing graphical information, such as a picture (also .jpeg)

Invisible Filenames

A filename that begins with a period is called an invisible filename (or an invisible file or sometimes a hidden file) because ls does not normally display it. The command ls a displays all filenames, even invisible ones. Names of startup files (page 83) usually begin with a period so that they are invisible and do not clutter a directory listing. The .plan file (page 65) is also invisible. Two special invisible entries a single and double period (. and . .) appear in every directory (page 85).

mkdir:Creates a Directory

The mkdir utility creates a directory. The argument (861) to mkdir becomes the pathname of the new directory. The following examples develop the directory structure shown in Figure 4-4. In the figure the directories that are added are a lighter shade than the others and are connected by dashes.

Figure 4-4. The file structure developed in the examples

In Figure 4-5, ls shows the names of the files Alex has been working with in his home directory: demo, names, and temp. Using mkdir, Alex then creates a directory named literature as a child of the /home/alex directory. When you use mkdir, enter the pathname of your home directory (page 82) in place of /home/alex. The second ls verifies the presence of the new directory.

Figure 4-5. The mkdir utility
 $ ls demo  names  temp $ mkdir /home/alex/literature $ ls demo  literature  names  temp $ ls -F demo  literature/  names  temp $ ls literature $ 

You can use the F option with ls to display a slash after the name of each directory and an asterisk after each executable file (shell script, utility, or program). When you call it with an argument that is the name of a directory, ls lists the contents of the directory. If the directory is empty, ls does not display anything.

The working directory


While you are logged in on a character-based interface to a Linux system, you are always associated with a directory. The directory you are associated with, or are working in, is called the working directory or current directory. Sometimes this association is referred to in a physical sense: "You are in (or working in) the jenny directory." The pwd command displays the pathname of the working directory.

To access any file in the working directory, you need only a simple filename. To access a file in another directory, you must use a pathname.

Significance of the Working Directory

Typing a long pathname is tedious and increases the chance of making a mistake. This possibility is less likely under a GUI, where you click filenames or icons. You can choose a working directory for any particular task to reduce the need for long pathnames. Your choice of a working directory does not allow you to do anything you could not do otherwise. Instead, it simply makes some operations easier.

Refer to Figure 4-6 as you read this paragraph. Files that are children of the working directory can be referenced by simple filenames. Grandchildren of the working directory can be referenced by short relative pathnames: two filenames separated by a slash. When you manipulate files in a large directory structure, short relative pathnames can save time and aggravation. If you choose a working directory that contains the files used most often for a particular task, you need to use fewer long, cumbersome pathnames.

Figure 4-6. Relative pathnames

Home directory

When you first log in on a Linux system, your working directory is your home directory. To display the pathname of your home directory, use pwd just after you log in (see Figure 4-7).

When used without any arguments, the ls utility displays a list of the files in the working directory. Because your home directory has been the only working directory you have used so far, ls has always displayed a list of files in your home directory. (All the files you have created up to this point were created in your home directory.)

cd: Changes to Another Working Directory

The cd (change directory) utility makes another directory the working directory but does not change the contents of the working directory. The first cd command in Figure 4-8 makes the /home/alex/literature directory the working directory, as verified by pwd.

When used without an argument, cd makes your home directory the working directory, as it was when you first logged in. The second cd command in Figure 4-8 does not have an argument and makes Alex's home directory the working directory.

Figure 4-7. Logging in and displaying the pathname of your home directory
 login: alex Password: Last login: Wed Oct 20 11:14:21 from bravo $ pwd /home/alex 

Figure 4-8. cd changes your working directory
 $ cd /home/alex/literature $ pwd /home/alex/literature $ cd $ pwd /home/alex 

tip: The working directory versus your home directory

The working directory is not the same as your home directory. Your home directory remains the same for the duration of your session and usually from session to session. Each time immediately after you log in, you are working in the same directory: your home directory.

Unlike your home directory, the working directory can change as often as you like. You have no set working directory, which is why some people refer to it as the current directory. When you log in and until you change directories by using cd, your home directory is your working directory. If you were to change directories to Scott's home directory, then Scott's home directory would be your working directory.

Startup files

Startup files, which appear in your home directory, give the shell and other programs information about you and your preferences. Frequently, one of these files tells the shell what kind of terminal you are using and executes the stty (set terminal) utility to establish your line kill and erase keys.

Either you or the system administrator can put a shell startup file containing shell commands in your home directory. The shell executes the commands in this file each time you log in. Because the startup files have invisible filenames, you must use the ls a command to see whether one is in your home directory. See pages 257 and 342 for more information about startup files.

Absolute Pathnames

Every file has a pathname. Figure 4-9 shows the pathnames of directories and ordinary files in part of a filesystem hierarchy. An absolute pathname always starts with a slash (/), the name of the root directory. You can build the absolute pathname of a file by tracing a path from the root directory through all the intermediate directories to the file. String all the filenames in the path together, separating each from the next with a slash ( / ) and preceding the entire group of filenames with a slash ( / ). This path of filenames is called an absolute pathname because it locates a file absolutely by tracing a path from the root directory to the file. The part of a pathname following the final slash is called a simple filename or just filename.

Figure 4-9. Absolute pathnames

Another form of absolute pathname begins with a tilde (~), which represents a home directory. For more information refer to "~ (Tilde) in Pathnames" on page 89.

Relative Pathnames

A relative pathname traces a path from the working directory to a file. The pathname is relative to the working directory. Any pathname that does not begin with the root directory (/) or a tilde (~) is a relative pathname. Like absolute pathnames, relative pathnames can describe a path through many directories.

Alex could have created the literature directory in Figure 4-5 more easily by using a relative pathname:

 $ pwd /home/alex $ mkdir literature 

The pwd command shows that Alex's home directory (/home/alex) is the working directory. The mkdir utility displays an error message if a directory or file named literature exists: You cannot have two files or directories with the same name in the same directory. The pathname used in this example is a simple filename a kind of relative pathname that specifies a file in the working directory.

The following commands show two ways to create the promo directory as a child of the newly created literature directory. The first way works when /home/alex is the working directory and uses a relative pathname.

 $ pwd /home/alex $ mkdir literature/promo 

caution: When using a relative pathname, know which directory is the working directory

The location of the file that you are accessing with a relative pathname depends on (is relative to) the working directory. Always make sure you know which directory is the working directory before you use a relative pathname. Use pwd to verify the directory. If you are using mkdir and you are not where you think you are in the file hierarchy, the new directory will end up in an unexpected location.

It does not matter which directory is the working directory when you use an absolute pathname.

The second way uses an absolute pathname:

 $ mkdir /home/alex/literature/promo 

Use the p (parents) option to mkdir to create both the literature and promo directories with a single command.

 $ pwd /home/alex $ ls demo  names  temp $ mkdir -p literature/promo 


 $ mkdir -p /home/alex/literature/promo 

The . and .. Directory entries

The mkdir utility automatically puts two entries in each directory it creates: a single period (.) and a double period (. .), representing the directory itself and the parent directory, respectively. These entries are invisible because each of their filenames begins with a period.

Because mkdir automatically places these entries in every directory, you can rely on their presence. The . is synonymous with the pathname of the working directory and can be used in its place; . . is synonymous with the pathname of the parent of the working directory.

With the literature directory as the working directory, the following example uses . . three times: first to list the contents of the parent directory (/home/alex), second to copy the memoA file to the parent directory, and third to list the contents of the parent directory again.

 $ pwd /home/alex/literature $ ls .. demo  literature  names  temp $ cp memoA .. $ ls .. demo  literature  memoA  names  temp 

While working in the promo directory, Alex can use a relative pathname to edit a file in his home directory. Before calling the editor, Alex checks which directory he is in:

 $ pwd /home/alex/literature/promo $ vim ../../names 

Virtually anywhere that a utility or program requires a filename or pathname, you can use an absolute or relative pathname or a simple filename. This usage holds true for ls, vim, mkdir, rm, and other Linux utilities.

Important Standard Directories and Files

Originally files on a Linux system were not located in standard places. The scattered files made it difficult to document and maintain a Linux system and just about impossible for someone to release a software package that would compile and run on all Linux systems. The first standard for the Linux filesystem, the FSSTND (Linux Filesystem Standard), was released on February 14, 1994. In early 1995 work was started on a broader standard covering many UNIX-like systems: FHS (Linux Filesystem Hierarchy Standard More recently, FHS has been incorporated in LSB (Linux Standard Base, a workgroup of FSG (Free Standards Group Figure 4-10 shows the locations of some important directories and files as specified by FHS. The significance of many of these directories will become clear as you continue reading.

Figure 4-10. A typical FHS-based Linux system file structure

The following list describes the directories shown in Figure 4-10, some of the directories specified by FHS, and some other directories. Most Linux distributions do not use all the directories specified by FHS. You cannot always determine the function of a directory by its name. For example, although /opt stores add-on software, /etc/opt stores configuration files for the software in /opt.


Root The root directory, present in all Linux system file structures, is the ancestor of all files in the filesystem.


Essential command binaries Holds the files needed to bring the system up and run it when it first comes up in single-user mode.


Static files of the boot loader Contains most of the files needed to boot the system.


Device files Contains all files that represent peripheral devices, such as disk drives, terminals, and printers.


Machine local system configuration Holds administrative, configuration, and other system files. One of the most important is /etc/passwd, which contains a list of all users who have permission to use the system.


Machine local configuration for the X Window System


Configuration files for add-on software packages kept in /opt


User home directories Each user's home directory is typically one of many subdirectories of the /home directory. As an example, assuming that users' directories are under /home, the absolute pathname of Jenny's home directory is /home/jenny. On some systems the users' directories may not be under /home but instead might be spread among /inhouse and /clients.


Shared libraries and kernel modules


Loadable kernel modules


Mount point for temporary mounting of filesystems


Add-on software packages (optional packages)


Kernel and process information virtual filesystem


Home directory for root


Essential system binaries Utilities used for system administration are stored in /sbin and /usr/sbin. The /sbin directory includes utilities needed during the booting process, and /usr/sbin holds utilities used after the system is up and running. In older versions of Linux, many system administration utilities were scattered through several directories that often included other system files (/etc, /usr/bin, /usr/adm, /usr/include).


Temporary files Used to hold temporary files.


Second major hierarchy Traditionally includes subdirectories that contain information used by the system. Files in /usr subdirectories do not change often and may be shared by multiple systems.


Most user commands Contains the standard Linux utility programs that is, binaries that are not needed in single-user mode.


Symbolic link to /usr/X11R6/bin


Games and educational programs


Header files included by C programs


Symbolic link to /usr/X11R6/include/X11




Symbolic link to /usr/X11R6/lib/X11


Local hierarchy Holds locally important files and directories that are added to the system. Subdirectories can include bin, games, include, lib, sbin, share, and src.


Online manuals


Nonvital system administration binaries See /sbin.


Architecture-independent data Subdirectories can include dict, doc, games, info, locale, man, misc, terminfo, and zoneinfo.


Miscellaneous documentation


GNU info system's primary directory


Source code


X Window System, version 11 release 6


Variable data Files with contents that vary as the system runs are found in subdirectories under /var. The most common examples are temporary files, system log files, spooled files, and user mailbox files. Subdirectories can include cache, lib, lock, log, opt, run, spool, tmp, and yp. Older versions of Linux scattered such files through several subdirectories of /usr (/usr/adm, /usr/mail, /usr/spool, /usr/tmp).


Log files Contains lastlog (a record of the last login by each user), messages (system messages from syslogd), and wtmp (a record of all logins/logouts).


Spooled application data Contains anacron, at, cron, lpd, mail, mqueue, news, samba, and uucp. The file /var/spool/mail typically has a symbolic link in /var.

     < Day Day Up > 

    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213 © 2008-2017.
    If you may any questions please contact us: