I have a 100+ rar files which I want to extract using find's exec command. I'd like to see the usual rar output so I can monitor its progress, and also to pipe the output to grep and then on to wc to count the 'All OK' lines (which rar prints if an archive is extracted successfully).

I tested with the following command (without the final wc -l component), which was designed to find 2 rar archives, but that simply printed 4 'All OK' lines, 2 for each of the 2 rar archives that were extracted.

$ find -iname 'TestNum*.rar' -execdir rar e '{}' \; | tee - | grep -i 'All OK'
All OK
All OK
All OK
All OK
Note: 2 archives extracted, but 4 'All OK' lines, and none of rar's output.

What I wanted was something like:

$ find -iname 'TestNum*.rar' -execdir rar e '{}' \; | ... ???
Extracting from TestNum1.rar
All OK
Extracting from TestNum2.rar
All OK

The final line with just 2 being the wc -l output, showing the actual number of matches of 'All OK'.

Is this possible? Thanks.

EDIT @ 2018-04-25 19:21

I've just realized that I could just do the following which works fine:

find . -iname 'TestNum*.rar' -execdir rar e '{}' \; | tee rar_out
grep -i 'All OK' rar_out | wc -l

Still out of interest is what I originally asked possible?

  • 1
    That's some non ideal output ; whenever I do this sort of thing I write a script that checks the exit code and Echoes just the file name if it failed so that it can be used for some other script to collect the failed files and delete them or something. – user1133275 Apr 26 '18 at 0:35
  • How would you go about getting the rar exit code if it gets called by the -exec option of find? – mattst Apr 26 '18 at 18:57
  • 1
    I use "find | xargs -P $(nproc) function" (pseudocode) instead of -exec because it's more flexible. – user1133275 Apr 27 '18 at 2:42

tee can send to stdout and to a file. In your example you send both outputs to stdout (which in this case is the pipe). One way around this is to use a named pipe to capture the output:

 mkfifo p
 cat p &   # this blocks until something is written to p
 find -iname 'TestNum*.rar' -execdir rar e '{}' \; | tee p | grep -c 'All OK'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.