system wide elf compression script using find, file, and upx

839 views Asked by At

I'm working on my first script, and it is to compress all elf executables on a system.

  find / * -executable -type f -exec file '{}' \; | grep ELF | sed -e "s/[:].*//" |     
  upx --best --ultra-brute 

upx is not responding to sent files

2

There are 2 answers

0
Stephane Rouberol On BEST ANSWER

Not sure if you're going a safe way for your system (don't you want to filter at least shared libs?), but I would suggest:

find / -executable -type f | xargs file |\
grep ELF | cut -d: -f1 | xargs upx --best --ultra-brute

If you have blanks in file names

find / -executable -type f -print0 | xargs -0 file |\
grep ELF | cut -d: -f1 | xargs -0 upx --best --ultra-brute

but is is likely you will bump into a shell limit (xargs: argument line too long)

One workaround would be to use a loop:

find / -executable -type f -print0 | xargs -0 file |\
grep ELF | cut -d: -f1 |\
while read f 
do
   upx --best --ultra-brute "$f"
done
0
Igor Chubin On

You need to include pipe in the exec argument. I do that with sh -c:

find / * -executable -type f -exec sh -c 'file '{}' \
| grep -q ELF' \; -print \
| upx --best --ultra-brute 

Also I use here -print instead of your construction with sed. It's better because in case you have : in a filename, sed-based solution will not work (even better is -print0 with xargs -0).