Redirect an output to a file from command piping grep

Solution 1:

Use the --line-buffered option for grep (and also get rid of the useless cat):

hexdump /dev/urandom | grep --line-buffered -i "ffff f" > random

This way the output is not buffered but every line put into random immediately. I would also recommend to use tee in your pipe to see how many lines have been produced:

hexdump /dev/urandom | grep --line-buffered -i "ffff f" | tee random

Solution 2:

Your file is empty because the process is interrupted before the file is written to disk. That is how redirection works. As a workaround, try this:

script -c 'cat /dev/urandom|hexdump|grep -i "ffff f"' -f random

This will basically write all screen output to the file.