-
04-07-2019 - |
题
我猜这个网站上的大多数人都熟悉 tail,如果不熟悉的话 - 它提供了一种“跟随”模式,当文本附加到文件 tail 时,会将这些字符转储到终端。
我正在寻找(如果需要的话可能自己编写)是一个适用于二进制文件的 tail 版本。基本上,我有一个无线链接,我想在文件从另一个网络链接下来时通过该链接进行传输。查看尾部源代码,重写不会太难,但我宁愿不重新发明轮子!严格来说,这并不是“尾部”,因为我希望复制整个文件,但它会监视添加的新字节并传输这些字节。
有想法吗?
解决方案
还有 bintail 应用程序,它似乎比上述脚本更强大。
bintail 包中包含一个应用程序 bintail 。该程序从磁盘读取一个普通文件,并将输出管道输出到stdout,逐字节,没有转换,类似于 tail (1)对文本文件的作用。这对于“拖尾”是有用的。二进制文件,例如WAV文件,在实时编写时。这个应用程序正在进行中,但它已经完成了它为我设计的目标。
其他提示
将其传输到hexdump:
tail -f somefile | hexdump -C
这个用于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
来处理二进制文件。如果您想要接收新的“滴流”,也可能需要避免缓冲问题。数据尽快。
Linux coreutils tail(1)在二进制文件上运行得很好。对于大多数应用程序,您只需要避免其行方向,以便输出不会在数据结构中间的某个随机位置开始。你可以通过简单地从文件的开头开始,这也是你要求的:
tail -c +1 -f somefile
工作正常。
这不是尾巴 - 这是逐步复制文件。看看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