bash 如何只获取二进制文件的前十个字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4411014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:56:16  来源:igfitidea点击:

How to get only the first ten bytes of a binary file

bashbinary

提问by User1

I am writing a bash script that needs to get the header (first 10 bytes) of a file and then in another section get everything except the first 10 bytes. These are binary files and will likely have \0's and \n's throughout the first 10 bytes. It seems like most utilities work with ASCII files. What is a good way to achieve this task?

我正在编写一个 bash 脚本,它需要获取文件的标头(前 10 个字节),然后在另一个部分中获取除前 10 个字节之外的所有内容。这些是二进制文件,并且可能在前 10 个字节中包含\0's 和\n's。似乎大多数实用程序都使用 ASCII 文件。完成这项任务的好方法是什么?

回答by psmears

To get the first 10 bytes, as noted already:

要获取前 10 个字节,如前所述:

head -c 10

To get all but the first 10 bytes (at least with GNU tail):

要获取除前 10 个字节之外的所有字节(至少使用 GNU tail):

tail -c+11

回答by moonshadow

head -c 10does the right thing here.

head -c 10在这里做正确的事。

回答by Mark Ransom

You can use the ddcommand to copy an arbitrary number of bytes from a binary file.

您可以使用该dd命令从二进制文件中复制任意数量的字节。

dd if=infile of=outfile1 bs=10 count=1
dd if=infile of=outfile2 bs=10 skip=1