-->

Previous | Table of Contents | Next

Page 351

CHAPTER 17

GNU Project Utilities

by Sriranga R. Veeraraghavan

IN THIS CHAPTER

Page 352

GNU (which stands for "GNU's not UNIX") is a UNIX-compatible software system that is being developed by Richard Stallman. The GNU project utilities are the GNU implementation of familiar UNIX programs like mv, cp, and ls.

The GNU versions of these programs generally run faster, provide more options, have fewer arbitrary limits, and are generally POSIX.2-compliant.

The GNU project utilities are distributed in several parts. The bin utilities, diff utilities, and shar (shell archive) utilities are primarily used in development work. The most frequently used utilities are the file utilities, find utilities, shell utilities, and text utilities; these are covered in this chapter.

The true power of the GNU project utilities is that they enable a user to break down complex tasks and solve them piece by piece, quickly and easily.

File Utilities

This section covers the major GNU file management utilities. The following is a complete list of the programs included in the GNU file utilities distribution:

chgrp ls
chown mkdir
chmod mvdir
cp mkfifo
dd mknod
df mv
du rm
install rmdir
ln sync
dir touch
vdir

Listing Directory Contents

The GNU file utilities include three programs for listing directory contents and information about files: ls, dir, and vdir. The biggest difference between these three programs is in their default behavior; dir is equivalent to ls -C, and vdir is equivalent to ls -l.

The default behavior of ls (invoked with no arguments) is to list the contents of the current directory. If a directory is given as an argument, then its contents are listed nonrecursively (files starting with a period (.) are omitted). For filename arguments, just the name of the file is printed. By default, the output is listed alphabetically.

Page 353

The GNU version of ls supports all the standard options and also introduces the major feature of color-coding files.

The variable $LS_COLOR (or $LS_COLOUR) is used to determine the color scheme. If $LS_COLOR is not set, the color scheme is determined from the system default stored in the file /etc/DIR_COLORS. This variable can be set by hand, but it is much easier to have the program dircolors set it by issuing the following command:


eval `dircolors`

To aid in customizing the color scheme, dircolors supports a -p option that prints out the default configuration. Redirecting the output to a file creates a valid dircolors init file. So,


dircolors -p > .dircolorsrc

will create a file .dircolorsrc, which can be customized. After the file .dircolorsrc is customized, $LS_COLORS can be set by issuing the following command:


eval `dircolors .dircolorsrc`

Putting this line in an init file (.profile or .cshrc) and then having the alias


alias ls="ls --colors" (sh,bash,ksh)

alias ls "ls --colors" (csh,tcsh)

will ensure that the custom color scheme is used for ls.

Listing 17.1 is an excerpt from a .dircolorsrc file that implements bold text for directories and normal text for all other types of files. If any of these file types are left out, default values are substituted for them. The comments describe the different color values that can be used.

Listing 17.1. Excerpt from a .dircolorsrc file.


# Below are the color init strings for the basic file types. A color init

# string consists of one or more of the following numeric codes:

# Attribute codes:

# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed

# Text color codes:

# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white

# Background color codes:

# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

NORMAL 00     # global default

FILE   00     # normal file

DIR    01     # directory

LINK   00     # symbolic link

FIFO   00     # pipe

SOCK   00     # socket

BLK    00     # block device driver

CHR    00     # character device driver

ORPHAN 00     # symlink to nonexistent file

EXEC   00     # executables

Page 354

To implement colors, simply specify the scheme as


FILE_TYPE attribute codes;text codes;background codes

This line indicates all links are red with a white background:


LINK 00;31;47

Another feature of the --color option is that files with extensions can also be colorized. For example, to make all .jpg files underlined, put the line


.jpg 04

into the .dircolors file. Any file extension can be used. Some people like to have archive files (.uu, .tar, .tar.gz, .gz, .Z, .z, .tgz) in one color and picture files (.jpg, .jpeg, .gif) in another.

File Operations

The next set of commands in the file utilities are the utilities that are used for basic file operations, such as copying and moving files.

The file operations commands like cp, mv, rm, and ln are familiar to all UNIX users. The GNU versions of these commands support all the standard options along with a few additional options for safety and convenience. These options are as follows:

-b or --backup Makes backups of files that are about to be overwritten or removed. Without this option, the original versions are destroyed. (Not available in rm.)
-s suffix or --suffix=suffix Appends suffix to each backup file made if a backup option is specified. (Not available in rm.)
-v or --verbose Prints out the filename before acting upon it.

In terms of safety, the backup options are like the -i option (interactive mode); they frequently prevent mishaps.

By default, the suffix for the backups is the tilde (~), but this can easily be changed by setting the variable $SIMPLE_BACKUP_SUFFIX. Setting this variable also avoids having to give the -s option each time.

Another command that is useful for copying files is the install command. It is frequently used to install compiled programs and is familiar to programmers who use make, but it also can be useful for the casual user because it can be used to make copies of files and set attributes for those files.

Previous | Table of Contents | Next