Search code examples
linuxbashtailhexdump

How to I write this output command to a file


I'm trying to get every new line with tail from a file and transform it to hexdump, but I'm unable to write it to a file, I've tried with >> and | tee -a destfile but it doesn't give any error but stops working.

So, I have a binary file (data.bin) that is always growing new lines with a script.I'm trying to read and transform it to hexadecimal, that works well, and it outputs well:

tail -f data.bin | hexdump -e '16/1 "%02x " "\n"'

this outputs this:

01 55 1d fa 14 ae b5 41 ec 51 3c 42 64 55 00 00
74 5e f7 5d 00 00 00 00 02 55 1d fa 33 33 b3 41
7b 14 3f 42 63 55 00 00 74 5e f7 5d 00 00 00 00

When I try to do this

tail -f data.bin | hexdump -e '16/1 "%02x " "\n"' >> destfile.txt 

It creates an empty file and doesn't write anything.


Solution

  • When you redirect the output of a command, the output stream becomes fully buffered, so it will not be visible until flushed. You could read for exaple this link for more information.

    To make the output line buffered you can use stdbuf utility from GNU coreutils:

    stdbuf -oL tail -f input | stdbuf -oL hexdump -e '16/1 "%02x " "\n"' >> destfile.txt 
    

    That way the output will be flushed each line.