start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

15.10. Save Space in Executable Files with strip

After you compile 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 or gdb 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.

-s Section 9.14

% 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

The GNU strip has a number of options to control what symbols and sections are stripped from the binary file. Check the strip manpage for specific details of the version you have.

If you know that you want a file stripped when you compile it, your compiler probably has a -s option (which is passed to ld after compilation is complete). If you use ld directly -- say, in a makefile (Section 11.10) -- use the -s option there.

Here's a shell script named stripper that finds all the unstripped executable files in your bin directory (Section 7.4) 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):

xargs Section 28.17

#! /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 -rpl strip

The find (Section 9.2) finds all executable files that aren't setuid or setgid and runs file (Section 12.6) 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 the following:

/usr/local/bin/xemacs: TAB xxx... 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.

The final xargs command uses the options -r (to not run strip if sed outputs no names to strip), -p (to be interactive, asking before each strip), and -l (to process one filename at a time). None of those options are required; if you don't want them, you might at least use -t so the script will list the files it's stripping.

-- JP



Library Navigation Links

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