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

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 24.12 Compressing a Directory Tree: Fine-Tuning Chapter 24
Other Ways to Get Disk Space
Next: 24.14 Don't Use strip Carelessly

24.13 Save Space in Executable Files with strip

Warning! After you compile (52.8 ) and debug a program, there's a part of the executable binary that you can delete to save disk space. The strip command does the job. Note that once you strip a file, you can't use a symbolic debugger like dbx on it!

Here's an example. I'll compile a C program and list it. Then I'll strip it and list it again. How much space you save depends on several factors, but you'll almost always save something.


% cc -o echoerr echoerr.c

% ls -ls echoerr

  52 -rwxr-xr-x   1 jerry    24706 Nov 18 15:49 echoerr
% strip echoerr

% ls -ls echoerr

  36 -rwxr-xr-x   1 jerry    16656 Nov 18 15:49 echoerr

If you know that you want a file stripped when you compile it, use cc with its -s option. If you use ld -say, in a makefile (28.13 ) - use the -s option there.

Here's a shell script named stripper that finds all the unstripped executable files in your bin directory (4.2 ) and strips them. It's a quick way to save space on your account. (The same script, searching the whole filesystem, will save even more space for system administrators - but watch out for unusual filenames (9.22 ) ):


#! /bin/sh
skipug="! -perm -4000 ! -perm -2000"  # SKIP SETUID, SETGID FILES
find $HOME/bin -type f \( -perm -0100 $skipug \) -print |
xargs file |
sed -n '/executable .*not stripped/s/:[TAB]
.*//p' | 
xargs -t strip

The find (17.2 ) finds all executable files that aren't setuid or setgid (24.14 ) and runs file (25.8 ) to get a description of each. The sed command skips shell scripts and other files that can't be stripped. sed searches for lines from file like:


... executable xxx

... not stripped

with the word "executable" followed by "not stripped"-sed removes the colon, tab, and description, then passes the filename to strip .

- JP

Previous: 24.12 Compressing a Directory Tree: Fine-Tuning UNIX Power Tools Next: 24.14 Don't Use strip Carelessly
24.12 Compressing a Directory Tree: Fine-Tuning Book Index 24.14 Don't Use strip Carelessly

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