UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 24.10 zloop: Run a Command on Compressed Files Chapter 24
Other Ways to Get Disk Space
Next: 24.12 Compressing a Directory Tree: Fine-Tuning
 

24.11 Edit Compressed Files with zvi, zex, and zed

Compressed files (24.7) save disk space. But compressed files aren't as convenient to work with: you have to uncompress them before you can read or edit them. This script makes editing easier. It uncompresses files that were compressed with the GNU gzip utility. Then it starts a text editor: vi, ex, or ed. (It's easy to modify this to use other editors.) The vi and ex editors can edit several files (30.4); this script handles that. After you edit all the files, the script recompresses them in the background so that you don't have to wait. There's one more bit of trickery here: instead of uncompressing all files you specify before it starts the editor, the script uncompresses just the first file - it does the rest in the background while you're editing the first file. (It figures out what all the uncompressed files will be named. By the time the editor gets to them, they should have been uncompressed.)

This makes it easy to save a lot of disk space by keeping your files gzipped most of the time. The response is almost as fast as editing an uncompressed file, especially if the first file on the command line is a small one. Here's an example. I'll edit the files qlog.gz and /usr/central/data.gz with vi. Next, I'll run zed editing script (28.9) on bigfile.gz:

% zvi qlog.gz /usr/central/data   The .gz isn't required

    ...Edit the two files like a normal vi multiple-file session...

zvi: re-gzipping qlog /usr/central/data in the background...
% zed bigfile < edscr
173571   ed's file size counts
183079
zed: re-gzipping bigfile in the background...

zvi
If there are any errors, the program prompts you when it can - otherwise you'll get email (1.33) with a copy of the error messages. Please test this script carefully on your system before you use it. All this trickery could need a little tweaking to work right.

Most of the script is pretty straightforward. Unfortunately, the script won't work with editors like Emacs (32.1) that try to open all files from the command line immediately. You could change that by making the script uncompress all files before starting Emacs.

The section that follows is interesting. It's the part that uncompresses background files. If there's an error in the background, how does the script catch it?

test -n "$bgfiles" && $uncompress $bgfiles >$t 2>&1 &
$prog $files

if [ -s $t ]
then
    echo "$myname: 'gunzip $bgfiles' bombed:" 1>&2
    cat $t 1>&2
    $echo "Should I try to gzip all files [ny](n)? $nnl"
    read ans

The standard output and standard error of the background job goes to a temporary file, $t. The editor ($prog) runs. After you quit the editor, the test ([) (44.20) -s option checks the temporary file. If the file isn't empty, the script shows you the errors from it (the file) and asks you whether the files should be regzipped.

The script is written to have two other links (18.3). You may want to make more or fewer links though, depending on the editors your system has. If you install the script from the CD-ROM, the links will be made for you. If you type in the script, put it in an executable file named zvi. Then make the links:

% chmod 755 zvi
% ln zvi zex
% ln zvi zed

The script tests the name it was called with, from $0, to decide which editor to use. This trick saves disk space. You can change the editors it uses by modifying the script and adding or removing links.

The absolute pathnames at the start of the script may need to be changed for your system.

- JP


Previous: 24.10 zloop: Run a Command on Compressed Files UNIX Power ToolsNext: 24.12 Compressing a Directory Tree: Fine-Tuning
24.10 zloop: Run a Command on Compressed Files Book Index24.12 Compressing a Directory Tree: Fine-Tuning

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System