1

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

Josh
  • 295
  • 1
  • 9
  • 1
    possible duplicate of [How do I include a pipe | in my linux find -exec command?](http://stackoverflow.com/questions/307015/how-do-i-include-a-pipe-in-my-linux-find-exec-command) – Jonathon Reinhart Aug 03 '12 at 02:07
  • maybe, I'll change it in a sec if that's the case. I didn't know that's what I was looking at, if so. – Josh Aug 03 '12 at 02:09
  • John, you're right on this one. – Josh Aug 03 '12 at 02:15
  • 2
    Don't use `for i in $(find)` - use `find -exec` or `find -print0 | -xargs -0` (with a more complex `-exec` or `xargs` clause). By the way, the problem is that you're using `{}` instead of `$()`. The line continuation backslashes aren't necessary. Remove the asterisk - it's only getting in the way. – Dennis Williamson Aug 03 '12 at 02:16
  • Dennis, that is less than helpful. The context is 'my first bash script'. – Josh Aug 03 '12 at 02:24
  • @Josh - He gave you specific examples on what was wrong and what you should use instead. Why isn't that helpful? – jordanm Aug 03 '12 at 04:50
  • I don't *want* to discourage you, but is it wise to use your first script for something so critical? Is it even wise to compress everything indiscriminately? Modifying the core libraries like this is not for the lighthearted and may have all sort of unintended side-effects. Why do you want to do this? – thkala Aug 04 '12 at 09:58
  • @jordanm yeah, you're right – Josh Mar 20 '13 at 15:38
  • @thkala This did byte my system. Good practice, though. – Josh Mar 20 '13 at 15:38

2 Answers2

3

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
Stephane Rouberol
  • 4,286
  • 19
  • 18
1

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).

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144