我猜这个网站上的大多数人都熟悉 tail,如果不熟悉的话 - 它提供了一种“跟随”模式,当文本附加到文件 tail 时,会将这些字符转储到终端。
我正在寻找的(如果有必要也可能自己写)是一个适用于二进制文件的 tail 版本。基本上,我有一个无线链接,当文件从另一个网络链接下来时,我想将它涓涓细流。查看尾部源代码,重写它不会太难,但我宁愿不重新发明轮子!这并不是严格意义上的“尾部”,因为我希望复制整个文件,但它会观察添加新字节并流式传输这些字节。
想法?
我猜这个网站上的大多数人都熟悉 tail,如果不熟悉的话 - 它提供了一种“跟随”模式,当文本附加到文件 tail 时,会将这些字符转储到终端。
我正在寻找的(如果有必要也可能自己写)是一个适用于二进制文件的 tail 版本。基本上,我有一个无线链接,当文件从另一个网络链接下来时,我想将它涓涓细流。查看尾部源代码,重写它不会太难,但我宁愿不重新发明轮子!这并不是严格意义上的“尾部”,因为我希望复制整个文件,但它会观察添加新字节并流式传输这些字节。
想法?
将其通过管道传输到 hexdump:
tail -f somefile | hexdump -C
还有一个bintail应用程序,它似乎比上述脚本更强大。
bintail包包含一个应用程序bintail。该程序从磁盘读取一个普通文件,并将输出逐字节传输到标准输出,不进行任何转换,类似于tail (1) 对文本文件所做的事情。这对于实时写入时“拖尾”二进制文件(例如 WAV 文件)很有用。这个应用程序正在进行中,但它已经完成了它为我设计的工作。
Linux coreutils tail(1) 在二进制文件上工作得很好。对于大多数应用程序,您只需要避免它的行方向,这样输出就不会从数据结构中间的某个随机点开始。您可以通过简单地从文件的开头开始来做到这一点,这也正是您所要求的:
tail -c +1 -f somefile
工作得很好。
这个用于 Windows 的仓促编码的 Python 脚本可能会有所帮助:
# 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.")
less somefile
然后按shift F
严格来说,您需要编写一个程序来执行此操作,因为tail
未指定用于处理二进制文件。如果您想尽快接收新的“涓涓”数据,您可能还想避免缓冲问题。
这不是尾巴——这是逐步复制文件。看看rsync。
我使用它,因为它也适用于实时流:
cat ./some_file_or_dev | hexdump -C
倾销我的按键(和释放)的示例:
[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
我使用这个命令(1 表示要解释的字节数):tail -f | od -x1