如何在 Linux 中将 8 个字节的数据添加到二进制文件中?

mar*_*ark 13 linux command-line

我想在二进制文件的开头添加 8 个字节的数据。

是否有针对此的 Linux 命令?

gar*_*ohn 21

这是一种方法。

printf "\x68\x65\x6c\x6c\x6f\x20\x77\x6f" | cat - oldfile > newfile
Run Code Online (Sandbox Code Playgroud)

to 的参数printf是 8 个字节的十六进制序列。只需将我使用的值(即 ASCII 字符“hello wo”)替换为您的值。


aki*_*ira 4

它不是“那个”命令,而是“一堆命令”(按照古老的 UNIX 传统):

  • 将 8 个字节放入文件中
  • 将原始文件附加到该文件
  • 将新文件重命名为原始文件的名称。

或者:

% echo -n "12345689" > new_file
% cat original >> new_file
% mv new_file original
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要从其他地方读取 8 个字节:

% dd if=inputstream of=new_file bs=1 count=8
Run Code Online (Sandbox Code Playgroud)

然后继续如上所述。