Directory Commands


This section discusses how to create directories (mkdir), switch between directories (cd), remove directories (rmdir), use pathnames to make your work easier, and move and copy files and directories between directories.

mkdir: Creates a Directory

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

Figure 6-7. The file structure developed in the examples


In Figure 6-8 (next page), pwd shows that Alex is working in his home directory (/home/alex) and ls shows the names of the files in his home directory: demo, names, and temp. Using mkdir, Alex creates a directory named literature as a child of his home directory. He uses a relative pathname (a simple filename) because he wants the literature directory to be a child of the working directory. Of course, Alex could have used an absolute pathname to create the same directory: mkdir/home/alex/literature.

Figure 6-8. The mkdir utility

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

The second ls in Figure 6-8 verifies the presence of the new directory. The F option to ls displays a slash after the name of each directory and an asterisk after each executable file (shell script, utility, or application). When you call it with an argument that is the name of a directory, ls lists the contents of that directory. The final ls does not display anything because there are no files in the literature directory.

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

$ pwd /home/alex $ mkdir literature/promo


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 one command:

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


or

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


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. Figure 6-9 shows two ways to make the /home/alex/literature directory the working directory, as verified by pwd. First Alex uses cd with an absolute pathname to make literature his working directoryit does not matter which is your working directory when you give a command with an absolute pathname. A pwd command confirms the change made by Alex. When used without an argument, cd makes your home directory the working directory, as it was when you logged in. The second cd command in Figure 6-9 does not have an argument so it makes Alex's home directory the working directory. Finally, knowing that he is working in his home directory, Alex uses a simple filename to make the literature directory his working directory (cd literature) and confirms the change with pwd.

Figure 6-9. cd changes your working directory

$ cd /home/alex/literature $ pwd /home/alex/literature $ cd $ pwd /home/alex $ cd literature $ pwd /home/alex/literature

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. Immediately after you log in, you are always 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 explains 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.


The . and .. Directory Entries

The mkdir utility automatically puts two entries in each directory you create: a single period (.) and a double period (..). The . is synonymous with the pathname of the working directory and can be used in its place; the .. is synonymous with the pathname of the parent of the working directory. These entries are hidden because their filenames begin with a period.

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


After using cd to make promo (a subdirectory of literature) his working directory, Alex can use a relative pathname to call vim to edit a file in his home directory.

$ cd promo $ vim ../../names


You can use an absolute or relative pathname or a simple filename virtually anywhere that a utility or program requires a filename or pathname. This usage holds true for ls, vim, mkdir, rm, and most 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, www.pathname.com/fhs). More recently FHS has been incorporated in LSB (Linux Standard Base, www.linuxbase.org), a workgroup of FSG (Free Standards Group, www.freestandards.org). Figure 6-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 6-10. A typical FHS-based Linux filesystem structure


The following list describes the directories shown in Figure 6-10, some of the directories specified by FHS, and some other directories. Red Hat Linux, however, does not use all the directories specified by FHS. Be aware that 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. See also "Important Files and Directories" on page 448.

/


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

/bin


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

/boot


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

/dev


Device files Contains all files that represent peripheral devices, such as disk drives, terminals, and printers. Previously this directory was filled with all possible devices. As of Fedora Core 3 and RHEL v.4, udev (page 461) provides a dynamic device directory that enables /dev to contain only devices that are present on the system.

/etc


Machinelocal system configuration files 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.

/etc/opt


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

/etc/X11


Machinelocal configuration files for the X Window System

/home


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 found under /home but instead might be spread among other directories such as /inhouse and /clients.

/lib


Shared libraries

/lib/modules


Loadable kernel modules

/mnt


Mount point for temporarily mounting filesystems

/opt


Add-on software packages (optional packages)

/proc


Kernel and process information virtual filesystem

/root


Home directory for root

/sbin


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).

/sys


Device pseudofilesystem See udev on page 461 for more information.

/tmp


Temporary files

/usr


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 several systems.

/usr/bin


Most user commands Contains the standard Linux utility programsthat is, binaries that are not needed in single-user mode (page 409).

/usr/games


Games and educational programs

/usr/include


Header files included by C programs

/usr/lib


Libraries

/usr/local


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.

/usr/man


Online manuals

/usr/sbin


Nonvital system administration binaries See /sbin.

/usr/share


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

/usr/share/doc


Documentation

/usr/share/info


GNU info system's primary directory

/usr/src


Source code

/var


Variable data Files with contents that vary as the system runs are kept 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).

/var/log


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).

/var/spool


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




A Practical Guide to Red Hat Linux
A Practical Guide to Red HatВ® LinuxВ®: Fedoraв„ў Core and Red Hat Enterprise Linux (3rd Edition)
ISBN: 0132280272
EAN: 2147483647
Year: 2006
Pages: 383

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net