Linuxincludes many text-based commands for doing real work. Many of thesetools are extremely specialized and complex, so I can't present morethan a bare introduction to these programs in this chapter, whichcovers some of the most important general-purpose text-mode tools. Ifyou have specific needs, they might be described in other chapters ofthis book, or you might be able to find helpful information in aspecific program's documentation or web page.
Accessing Media: Filesystem Manipulation Tools
One common requirement when using a Linuxworkstation is the need to access removable media—floppy disks, Zipdisks, CD-ROMs, and so on. If you're familiar with DOS or Windows,you're probably used to accessing these media using drive letters, suchas A: for the floppy disk. This approach doesn't normally work in Linux, although the mtools package enables limited access in this way, "Improving Disk Performance." Instead, you must mount a removable disk to a mount point—that is, make it available at a specific directory set aside for this purpose. . If you include the user option, ordinary users can mount and unmount the filesystem, but only the user who mounted it can unmount it. The users option works in the same way, but anybody can unmount the filesystem once it's mounted.
To mount a filesystem, you use the mount command, followed by the mount point or device filename, as shown here:
$ mount /mnt/floppy
This command mounts the device with a defined mount point of /mnt/floppy in /etc/fstab—presumablythe floppy drive. Assuming permissions allow, you can then read, write,and otherwise manipulate files on the floppy disk. When you're done,you should unmount the disk using the umount command:
$ umount /mnt/floppy
These commands work as shown only if you've defined a mount point in /etc/fstab; if you haven't, you must also specify the device file used to access the partition or disk, as in mount /dev/fd0 /mnt/floppy. Only root can mount a filesystem for which no /etc/fstab entry exists.
Sometimes you need to prepare a disk to hold files. You use one or both of two commands to do this job:
fdformat This command performs a low-level format of the floppy disk. This format defines the low-level data structures, such as sectors and tracks. This tool can only be used on floppy disks; hard disks are low-level formatted at the factory.
mkfs This tool writes a filesystem to a disk or partition, an operation that's sometimes called high-level formatting. In reality, mkfs calls other programs, such as mkfs.ext2 and mkdosfs, to do the real work.
Suppose you have a new unformatted floppy disk you wantto use to exchange data with a Windows user. You could use thefollowing commands to prepare the disk:
$ fdformat /dev/fd0
$ mkfs -t msdos /dev/fd0
After creating a filesystem, you can mount the floppyand copy files to it. If you prefer to use a filesystem other than FAT,you can do so—substitute the other filesystem's name, such as ext2 or minix, for msdos. Because fdformatis used only for floppy disks, you won't type that command when usingZip disks or other non-floppy removable media. You also don't need touse fdformat on floppies that have already been used.
.
Accessing Files: File Manipulation Tools
Much of the work you do with any computer and inany environment relates directly to manipulating files—copying them,moving them, changing their ownership and permissions, and so on. Linuxprovides a wide variety of tools for performing these tasks. They mayseem peculiar and even intimidating to the uninitiated, but Linux'stext-mode file-manipulation commands are very powerful.
Wildcard Specifications
One aspect of file manipulation in Linux is the use of wildcards,which are special characters or character groups that stand in forother characters. Using wildcards, you can tell the system to operateon a large number of files that meet certain name criteria.
Wildcard
Meaning
You can combine wildcards in various ways. For instance, *.[co] matches any filename that ends in .c or .o, such as inet.c or load.o; but not names that end in any other string, such as sound.h.Wildcards are expanded by the shell; therefore, programs called from ashell and passed wildcards as arguments actually receive the completelist of matching files as arguments, not the wildcard.
Copying Files
One of the workhorse Linux file-manipulation commands is cp, which copies files. The syntax for this command is deceptively simple:
cp [options] source destination
In this case, source is one or more source files, possibly expressed using wildcard operators. The destinationcan be either a file, in which case the original is copied to the newfilename; or a directory, in which case the original is copied to thenew directory using its original filename. If you specify multiple source files, either explicitly or via a wildcard, the destination must be a directory.
The complexity of cp emerges in the use of its many options, some of which are summarized in Table 5.2. This table isn't comprehensive; consult the cp man page for more options.
Option
Effect
Linking Files
Linux supports two types of links, which are ways to reference a single file by multiple names:
Hard Links This type of link creates two or moredirectory entries for each file. No directory entry is more "real" or"official" than the others, although one will necessarily be createdfirst. If you delete any link, the others continue to work, until youdelete the last link, at which point the file is deleted. All hardlinks to a file must reside on the same filesystem; you can't create,say, a hard link from your home directory to a file on a CD-ROM,floppy, or even another hard disk partition. Hard links betweendirectories aren't permitted.
Symbolic Links This type of link, also known as a
soft link,creates a special file whose contents point to another file by name.The soft link stops working if the original file is deleted, but theoriginal file is unaffected if you delete any soft links pointing toit. You can create cross-filesystem soft links, as well as soft linksto directories. Soft links impose an extra filesystem lookup and,therefore, are very slightly slower than hard links, although you won'tnotice this tiny difference in common operations.
You can create hard or soft links using the -l and -s options to cp, as noted in Table 5.2. You can also create links using the ln command, which uses a syntax that's identical to that of cp. The two commands also share many options, including -b, -f, and -i. Ordinarily, ln creates a hard link; but passing it the -soption causes it to create a symbolic link. In either case, you cancreate links on any Linux-native filesystem, such as the Minixfilesystem, ext2fs, ext3fs, ReiserFS, JFS, or XFS. The Rock Ridgeextensions to ISO-9660 also support soft links, although some mkisofsoptions cause them to be ignored or converted into duplicate files. Afew non-Linux filesystems support soft links, such as OS/2'sHigh-Performance File System (HPFS). The soft links are encoded as HPFSExtended Attributes and aren't useable from OS/2. You can't createlinks on most other filesystems, including FAT, although you can createsoft links that reside on Linux filesystems that point to files onfilesystems that don't support links.
Linux makes extensive use of links—especially symboliclinks—in its standard system files. Many commands are available undermultiple names via symbolic links, and symbolic links are also criticalin most distributions' startup script systems. News server softwaretypically makes heavy use of hard links.
Renaming and Moving Files
The mv command does double duty: It renames and moves files. Its syntax is much like that of cp:
mv [options] source destination
When you specify a complete filename for the destination, mv renames the file. When the destination is a directory, mv moves the file (keeping the old filename). When the destination is a complete filename in another directory, mv moves and renames the file.
You can use many of the same options with mv that you use with cp or ln. In particular, from Table 5.2, -b, -f, -i, and -u all apply. The -p option isn't available because mv doesn't alter permissions, ownership, or time stamps.
You can apply mv to adirectory, but only when the target location is on the same filesystemas the original. If you want to move an entire directory tree from onepartition to another, you'll have to use cp with its recursive option (or better, -a, to preserve permissions, symbolic links, and so on), tar, or some other tool to copy the files, and then delete the original files with rm and its recursive option.
Deleting Files
The rm command deletes (removes) files. Its syntax is shown here:
rm [options] files
This command accepts many of the same options as cp, ln, and mv. Specifically, from Table 5.2, -f, -i, and -r work with rm. Unlike some operating systems' file-deletion tools, rm is permanent; Linux doesn't store deleted files in any sort of "trash can" folder.
The rm command doesn't normally delete directories, but if you pass it the -r or -R option, it will delete an entire directory tree, whether or not there are files in the target directory.
Changing Ownership
In Linux, all files have owners. This informationis encoded in the form of a user ID (UID) number, but most utilitieswork with the associated username. For instance, a long file listingmight look like this:
$ ls -l report.*
-rw-r--r-- 1 homer users 5271 Dec 12 12:07 report.tex
The username in this case is homer. The file is also associated with a specific group—usersin this example. It's sometimes necessary to change the ownership of afile—for instance, a system administrator may want to move files into aspecific person's account for that person's exclusive use. Ordinaryusers can't change the ownership of a file, but root can, by using the chown command, which has the following syntax:
chown [options] owner[:group] files
You can specify the owner as a username or as a UID number. The optional groupspecification can also be a name or a group ID (GID) number. Thiscommand accepts several options, the most important of which is -R, which initiates a recursive ownership change. You can use this option to change ownership of an entire directory tree.
Although only root has thepower to change a file's ownership, ordinary users can change a file'sgroup, within certain limits. Specifically, the user must belong to thetarget group and must own the file in question. For instance, if homer is a member of the users, horse, and bow groups, homer can assign a file to any of these groups, but not to the library group. The tool to change a file's group is chgrp, and it works much like chown:
chgrp [options] group files
As with chown, you can use the -R option to perform a recursive change.
Changing Permissions
Linux file security is based upon both ownership and permissions. Three permissions are paramount: read, write, and execute.The first grants the ability to read the contents of a file; the secondenables the ability to modify a file's contents; and the third grantsthe right to run a file as a program (of course, it must be a programfile for this access to be meaningful). These three permissions can beset differently for three increasingly broad classes of users: thefile's owner, the file's group, and all others (that is, world permissions).
The combination of three permission types and threescopes to which they apply means that there are nine primary permissionbits. These permissions are frequently expressed as a nine-characterstring, such as rwxr-x---. The first threecharacters represent read, write, and execute permissions for theowner. If these characters are letters matching the type of permission (r for read, w for write, and x for execute), then the owner has the specified permission. If the character is a dash (-),then the owner lacks the specified permission. The next block of threecharacters represents the access granted to the file's group, and thefinal block of three characters represents world access. Thus, in thecase of rwxr-x---, the owner has full read,write, and execute permission; the group has read and execute but notwrite access; and everybody else has no access. These nine charactersare sometimes preceded by another that represents the file's type—adash (-) for an ordinary file, d for a directory, s for a symbolic link, and so on.
These permissions can also be expressed by using octal(base
numbers. An octal 0 represents no access; a 1 means executepermission; a 2 means write permission; and a 4 means read permission.These numbers can be added together when more than one permission ispresent. The result is a single octal digit for each permission scope,and these numbers are displayed one after another. For instance, rwxr-x--- is equivalent to 750.
You can change permissions using the chmod command, which takes the following syntax:
chmod [options] mode files
The mode specification is potentially complex. You can specify the mode as an octal number, such as 750.You can also use a symbolic format in which you specify whosepermissions are to be affected; whether you're adding, deleting, orsetting permissions; and what permissions you're changing. Table 5.3summarizes the options for symbolic modes. Pick one or more elementsfrom the Affects Symbol column, one from the Operation Symbol column,and one or more from the Permission Symbol column.
Affects
Affects Symbol
Operation
Operation Symbol
Permission
Permission Symbol
You can combine multiple symbolic mode options by separating them with commas (,). As an example of chmod's symbolic modes, Table 5.4presents some before-and-after scenarios. As a general rule, you canachieve the same goals using either symbolic or octal modes; however,there are exceptions. For instance, you can use the u, g, and opermission symbols to set permissions on a group of files uniquely foreach file, depending on their existing permissions for a specific userset. The uppercase X permission symbol canalso be useful in setting permissions on directories, which normallyhave execute permissions set whenever their read permissions are set.(Execute permission for a directory enables searching the directory'scontents, not executing code in the directory.)
Permissions Before
Symbolic Mode
Permissions After
A couple of special permission settings deserve attention:
SUID and SGID Bits The set user ID (SUID) and set group ID (SGID) bits can be set on executable files by applying the spermission symbol to owner or group permission, respectively.Ordinarily, when you run a program, that program runs with thepermissions of the user who launched the program. With the SUID or SGIDbit set, though, the program runs with the permissions associated withthe program file's owner or group, respectively. This feature is usedby a handful of key system programs to enable users to do things thatthey otherwise wouldn't be able to do, such as access a CD-R drive'sdevice files. You should use this feature sparingly, though; a bug in aprogram that's run with its SUID bit set (particularly if its owner is root) can be a security risk if the program has a bug or enables users to write arbitrary files. For instance, an SUID rooteditor would enable any user who can run the editor to edit keyconfiguration files. You can spot SUID or SGID programs by the presenceof an s rather than an x as the execute permission symbol in their permission strings, as in rwsr-sr-x.
Sticky Bit Ordinarily, write permission on adirectory enables any user to create and delete any file within adirectory. Sometimes, though, this isn't desirable; for instance, youprobably don't want to let users delete each others' temporary files in/tmp or similar shared directories. You can set the sticky bit on such directories by using the tsymbolic permission symbol. This bit keeps users from deleting filesthey don't own. You can tell when a directory has its sticky bit set bythe presence of a t rather than an x in the world permission string, such as rwxrwxrwt.
Working with Directories
Many Linux commands for working with files also apply to directories. For instance, chown, chmod, cp, mv, rm, and ln(with some restrictions on some of these commands) all apply todirectories. Directories present their own unique needs, though. Forinstance, you need a special command to create a directory. Somedirectory-centric commands include:
mkdir This command creates a new directory, as in mkdir reports to create a directory called reports.
rmdir This command deletes an empty directory. If you want to delete a directory tree that contains files, you're better off using rm -r.
cd This command changes the current working directory. For instance, typing cd /etc moves you into the /etc directory, so that you can more easily work on files in this directory.
pwd This command displays the current working directory, so that you know where you're working.
Using System Information Tools
Sometimes you need to know things about yourcomputer—where partitions are mounted, how much memory is in use, andso on. Linux provides a number of commands that provide thisinformation. Examples include:
df This commanddisplays information about all of the mounted filesystems. If you passa mount point or partition identifier as a parameter, dfdisplays information about the specified filesystem only. Informationincludes the device filename, total filesystem size, used space, freespace, percentage of disk space used, and mount point. This tool isextremely useful in tracking disk use and in planning disk expansions.
du The dfcommand is very useful for tracking disk use on apartition-by-partition basis, but it's not good for trackingfiner-grained disk use. That's where ducomes in; it tells you how much disk space each subdirectory in adirectory tree uses. This command accepts many options, one of the mostuseful of which is --max-depth=n; this option trims the report so you don't see details of subdirectories below a specific depth. For instance, du --max-depth=1 /homewill tell you how much disk space your users are consuming in theirhome directories. (Depending on the permissions users have set, thisparticular example might work only if it is typed by root.)
stat This commanddisplays information on a file, including the filename, file size,ownership, permissions, three dates (last access, last modification,and last file status change), and some low-level data structures suchas the inode number. This information can be useful in determining whenfiles have been used or modified and in performing some low-level filemaintenance.
lsof This command listsall the open files on a computer, and it lists information on the userand process that is accessing the files. As such, lsof produces copious output. You'll probably have to pipe the output through less or grep to make sense of it, or you might use various lsofoptions to trim its output. This command is very useful in findingprocesses that are using files on removable media you want to unmountbut can't because of open files.
uptime This command reports how long the computer has been running. It also displays three load averages,which give you an idea of how much demand there is for CPU time. A loadaverage of 0.0 means no programs are requesting CPU time; 1.0 means theCPU is being used to its fullest; and values above 1.0 mean that thekernel has been rationing CPU time because programs want more CPU timethan the system can deliver. Some systems, such as busy servers, runwith load averages of well over 1.0, but on others, load averagesshould be below 1.0 most of the time.
free This command summarizes the system's memory use. The total column displays available memory; used reports memory that's in use; and free summarizes free memory. The most important line is the one labeled -/+ buffers/cache; this line reports memory use by system processes, and so is a good measure of the demand for memory on the system. The Memline is likely to show very little free memory because this lineincludes memory that is used by buffers and caches, which are allocateddynamically by Linux to improve disk performance, consuming most memorynot being used by programs. The Swap line reports the demand for swap space. If the used entry for this line approaches the total entry, you may need to add memory or swap space to improve performance.
hostname Typed byitself, this command returns the computer's TCP/IP networking hostname.The system administrator may change the hostname by typing a new nameafter the command.
who This command returns a list of the users who are logged onto the computer. The users command is similar to who, and finger provides additional information.
dmesg This commanddisplays the Linux kernel message buffer. Soon after starting Linux,this buffer contains startup messages, which can be useful in systemdebugging. As the system runs, the message buffer will accumulatemessages on normal operations and the startup messages will be lost.