home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

15.9. Compressing a Directory Tree: Fine-Tuning

Here's a quick little command that will compress (Section 15.6) files in the current directory and below. It uses find (Section 9.2) to find the files recursively and pick the files it should compress:

-sizeSection 9.14, xargs Section 28.17

% find . ! -perm -0100 -size +1 -type f -print | xargs gzip -v

This command finds all files that are the following:

  • Not executable (! -perm -0100), so we don't compress shell scripts and other program files.

  • Bigger than one block, since it won't save any disk space to compress a file that takes one disk block or less. But, depending on your filesystem, the -size +1 may not really match files that are one block long. You may need to use -size +2, -size +1024c, or something else.

  • Regular files (-type f) and not directories, named pipes, etc.

The -v switch to gzip tells you the names of the files and how much they're being compressed. If your system doesn't have xargs, use the following:

% find . ! -perm -0100 -size +1 -type f -exec gzip -v {} \;

Tune the find expressions to do what you want. Here are some ideas -- for more, read your system's find manual page:

! -name \*.gz
Skip any file that's already gzipped (filename ends with .gz ).

-links 1
Only compress files that have no other (hard) links.

-user yourname
Only compress files that belong to you.

-atime +60
Only compress files that haven't been accessed (read, edited, etc.) for more than 60 days.

You might want to put this in a job that's run every month or so by at (Section 25.5) or cron (Section 25.2).

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.