Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [161]

By Root 983 0
be followed, but the option names vary: consult the manual pages for your system.

* * *

du


Usage

du [ options ] [ files-or-directories ]

Purpose

Show the space usage in one or more directory trees.

Major options

-k

Show space in kilobytes rather than (system-dependent) blocks.

-s

Show only a one-line summary for each argument.

Behavior

For each file or directory argument, or for the current directory if no such arguments are given, du normally produces one output line containing an integer representing the usage, followed by the name of the file or directory. Unless the -s option is given, each directory argument is searched recursively, with one report line for each nested directory.

Caveats

du's output is not sorted.

* * *

One common problem that du helps to solve is finding out who the big filesystem users are. Assuming that user home-directory trees reside in /home/users, root can do this:

# du -s -k /home/users/* | sort -k1nr | less

Find large home directory trees

This produces a list of the top space consumers, from largest to smallest. A find dirs -size +10000 command in a few of the largest directory trees can quickly locate files that might be candidates for compression or deletion, and the du output can identify user directory trees that might better be moved to larger quarters.

* * *

Tip


Some managers automate the regular processing of du reports, sending warning mail to users with unexpectedly large directory trees, such as with the script in Example 7-1 in Chapter 7. In our experience, this is much better than using the filesystem quota system (see the manual pages for quota(1)), since it avoids assigning magic numbers (filesystem-space limits) to users; those numbers are invariably wrong, and they inevitably prevent people from getting legitimate work done.

* * *

There is nothing magic about how du works: like any other program, it has to descend through the filesystem, and total up the space used by every file. Thus, it can be slow on large filesystems, and it can be locked out of directory trees by strict permissions; if its output contains Permission denied messages, its report undercounts the space usage. Generally, only root has sufficient privileges to use du everywhere in the local system.

Comparing Files

In this section, we look at four related topics that involve comparing files:

Checking whether two files are the same, and if not, finding how they differ

Applying the differences between two files to recover one from the other

Using checksums to find identical files

Using digital signatures for file verification

The cmp and diff Utilities

A problem that frequently arises in text processing is determining whether the contents of two or more files are the same, even if their names differ.

If you have just two candidates, then the file comparison utility, cmp, readily provides the answer:

$ cp /bin/ls /tmp

Make a private copy of /bin/ls

$ cmp /bin/ls /tmp/ls

Compare the original with the copy

No output means that the files are identical

$ cmp /bin/cp /bin/ls

Compare different files

/bin/cp /bin/ls differ: char 27, line 1 Output identifies the location of the first difference

cmp is silent when its two argument files are identical. If you are interested only in its exit status, you can suppress the warning message with the -s option:

$ cmp -s /bin/cp /bin/ls

Compare different files silently

$ echo $?

Display the exit code

1 Nonzero value means that the files differ

If you want to know the differences between two similar files, diff does the job:

$ echo Test 1 > test.1

Create first test file

$ echo Test 2 > test.2

Create second test file

$ diff test.[12]

Compare the two files

1c1

< Test 1

---

> Test 2

It is conventional in using diff to supply the older file as the first argument.

Difference lines prefixed by a left angle bracket correspond to the left (first) file, and those prefixed by a right angle bracket come from the right (second) file. The 1c1 preceding the differences

Return Main Page Previous Page Next Page

®Online Book Reader