Goyuix
Goyuix

Reputation: 24370

Binary "tail" a file

I would guess most people on this site are familiar with tail, if not - it provides a "follow" mode that as text is appended to the file tail will dump those characters out to the terminal.

What I am looking for (and possibly to write myself if necessary) is a version of tail that works on binary files. Basically I have a wireless link that I would like to trickle a file across as it comes down from another network link. Looking over the tail source code it wouldn't be too hard to rewrite, but I would rather not reinvent the wheel! This wouldn't strictly be "tail" as I would like the entire file to be copied, but it would watch as new bytes were added and stream those.

Ideas?

Upvotes: 18

Views: 19847

Answers (9)

Bin Tail
Bin Tail

Reputation: 31

This hastily coded Python script for Windows may be of assistance:

# bintail.py -- reads a binary file, writes initial contents to stdout,
# and writes new data to stdout as it is appended to the file.

import time
import sys
import os
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

# Time to sleep between file polling (seconds)
sleep_int = 1

def main():
    # File is the first argument given to the script (bintail.py file)
    binfile = sys.argv[1]

    # Get the initial size of file
    fsize = os.stat(binfile).st_size

    # Read entire binary file
    h_file = open(binfile, 'rb')
    h_bytes = h_file.read(128)
    while h_bytes:
        sys.stdout.write(h_bytes)
        h_bytes = h_file.read(128)
    h_file.close()


    # Loop forever, checking for new content and writing new content to stdout
    while 1:
        current_fsize = os.stat(binfile).st_size
        if current_fsize > fsize:
            h_file = open(binfile, 'rb')
            h_file.seek(fsize)
            h_bytes = h_file.read(128)
            while h_bytes:
                sys.stdout.write(h_bytes)
                h_bytes = h_file.read(128)
            h_file.close()
            fsize = current_fsize
        time.sleep(sleep_int)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        main()
    else:
        sys.stdout.write("No file specified.")

Upvotes: 3

Alex
Alex

Reputation: 36

I use this command (1 means the number of bytes to interpret) : tail -f | od -x1

Upvotes: 0

Anonymous
Anonymous

Reputation: 2172

I use this as it works on live streams too:

cat ./some_file_or_dev | hexdump -C

sample dumping my key presses (and releases):

[user@localhost input]$ sudo cat /dev/input/event2 | hexdump -C
00000000  81 32 b1 5a 00 00 00 00  e2 13 02 00 00 00 00 00  |.2.Z............|
00000010  04 00 04 00 36 00 00 00  81 32 b1 5a 00 00 00 00  |....6....2.Z....|
00000020  e2 13 02 00 00 00 00 00  01 00 36 00 01 00 00 00  |..........6.....|
00000030  81 32 b1 5a 00 00 00 00  e2 13 02 00 00 00 00 00  |.2.Z............|
00000040  00 00 00 00 00 00 00 00  81 32 b1 5a 00 00 00 00  |.........2.Z....|
00000050  a3 af 02 00 00 00 00 00  04 00 04 00 36 00 00 00  |............6...|
00000060  81 32 b1 5a 00 00 00 00  a3 af 02 00 00 00 00 00  |.2.Z............|
^C

Upvotes: 0

ruief
ruief

Reputation: 466

Linux coreutils tail(1) works just fine on binary files. For most applications, you just need to avoid its line-orientation, so that the output doesn't begin in some random spot in the middle of a data structure. You can do that by simply starting at the beginning of file, which is also exactly what you asked for:

tail -c +1 -f somefile

works just fine.

Upvotes: 5

Bin Tail
Bin Tail

Reputation: 76

There is also the bintail application which appears to be more robust than the aforementioned script.

The bintail package contains a single application, bintail. The program reads a normal file from disk, and pipes the output to stdout, byte-by-byte, with no translation, similar to what tail(1) does to text files. This is useful for "tailing" binary files, such as WAV files, while they are being written in realtime. This app is a work in progress, but it already does what it was designed to do for me.

Upvotes: 6

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215507

Strictly speaking, you need to write a program to do this, as tail is not specified to work on binary files. There are also buffering issues you probably want to avoid if you want to receive the new "trickled" data as soon as possible.

Upvotes: 1

wnoise
wnoise

Reputation: 9942

This isn't tail -- this is progressively copying a file. Look at rsync.

Upvotes: 0

mhawke
mhawke

Reputation: 87134

less somefile

Then press shift F

Upvotes: 1

Adam Pierce
Adam Pierce

Reputation: 34375

Pipe it to hexdump:

tail -f somefile | hexdump -C

Upvotes: 22

Related Questions