Linux binary read file

Shell: How to read the bytes of a binary file and print as hexadecimal?

In shell, how can I read the bytes of a binary file I have, and print the output as hexadecimal numbers?

7 Answers 7

$ hexdump -x /usr/bin/hexdump 0000000 feca beba 0000 0300 0001 0700 0080 0300 0000010 0000 0010 0000 5080 0000 0c00 0000 0700 0000020 0000 0300 0000 00a0 0000 b06f 0000 0c00 0000030 0000 1200 0000 0a00 0100 0010 0000 107c 0000040 0000 0c00 0000 0000 0000 0000 0000 0000 0000050 0000 0000 0000 0000 0000 0000 0000 0000 

For information, the first column is the hexadecimal offset of the bytes, the rest of the line is 8 sets of two-byte displays, i.e. 16 bytes, which is why the second line starts with an offset of 10 , which is 16 in hexadecimal. The two-byte representation depends on the endianness of the system. Type man hexdump for the full details.

od has many options for finetuning.

If it can help somebody else, I wanted to have the last 4 bytes in hex of a binary file. Here is what I did that works. od -A n -t x1 -w1 -v myFile.bin | tail -n 4 | sed ‘s/ //g’ | paste -sd »

While we’re on od and hexdump , two more similar tools:

$ hd /usr/bin/od | head 00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF. | 00000010 02 00 03 00 01 00 00 00 20 8e 04 08 34 00 00 00 |. . 4. | 00000020 a4 a2 00 00 00 00 00 00 34 00 20 00 08 00 28 00 |. 4. . (.| 00000030 1b 00 1a 00 06 00 00 00 34 00 00 00 34 80 04 08 |. 4. 4. | 00000040 34 80 04 08 00 01 00 00 00 01 00 00 05 00 00 00 |4. | 00000050 04 00 00 00 03 00 00 00 34 01 00 00 34 81 04 08 |. 4. 4. | 00000060 34 81 04 08 13 00 00 00 13 00 00 00 04 00 00 00 |4. | 00000070 01 00 00 00 01 00 00 00 00 00 00 00 00 80 04 08 |. | 00000080 00 80 04 08 c4 9d 00 00 c4 9d 00 00 05 00 00 00 |. | 00000090 00 10 00 00 01 00 00 00 00 a0 00 00 00 20 05 08 |. ..| $ xxd /usr/bin/od | head 0000000: 7f45 4c46 0101 0100 0000 0000 0000 0000 .ELF. 0000010: 0200 0300 0100 0000 208e 0408 3400 0000 . . 4. 0000020: a4a2 0000 0000 0000 3400 2000 0800 2800 . 4. . (. 0000030: 1b00 1a00 0600 0000 3400 0000 3480 0408 . 4. 4. 0000040: 3480 0408 0001 0000 0001 0000 0500 0000 4. 0000050: 0400 0000 0300 0000 3401 0000 3481 0408 . 4. 4. 0000060: 3481 0408 1300 0000 1300 0000 0400 0000 4. 0000070: 0100 0000 0100 0000 0000 0000 0080 0408 . 0000080: 0080 0408 c49d 0000 c49d 0000 0500 0000 . 0000090: 0010 0000 0100 0000 00a0 0000 0020 0508 . .. 

Or, if you want to read the bytes one at a time and print them in your own format, try something like:

while read -n 1 byte; do ord=$(printf "%b" "$" | od -t x1 | < read offset hex; echo $hex; >) echo "$ord" done 
7f 45 4c 46 01 01 01 00 00 00 

Источник

Читайте также:  Virtualbox guest additions installation linux

Shell Commands for Reading Binary Files in Linux: A Comprehensive Guide

Learn how to read binary files in Linux using shell commands. Our comprehensive guide covers hexdump, endianness, file types, and more. Start reading now!

  • Hexdump and od Utilities
  • Endianness
  • How to read a binary file using unix / linux command line
  • Storing Binary Data in Shell Variables
  • Identifying Binary File Types
  • Executing Binary Files
  • Other helpful code examples for reading binary files in Linux using shell commands
  • Conclusion
  • How to read binary file in Linux?
  • How do I read a binary file?
  • How do I run a binary file in shell?
  • How to extract binary files in Linux?

Binary files are a fundamental component of modern computing systems. They are used to store data in an efficient and compact manner, and are essential for many applications, including operating systems, databases, and multimedia files. However, working with binary files can be challenging, especially when it comes to reading or interpreting their contents. In this blog post, we will explore some of the most useful shell commands and utilities for reading binary files in Linux, and provide you with a comprehensive guide on how to use them effectively.

Hexdump and od Utilities

Hexdump and od are two popular command-line utilities that allow you to view the contents of binary files in a human-readable format. Hexdump displays a file in hexadecimal and ASCII formats, while od displays a file in octal, decimal, hexadecimal, and ASCII formats.

To use hexdump, simply type the following command in your terminal:

This will display the contents of the file in hexadecimal and ASCII formats. You can also use the -n option to specify the number of bytes to display, and the -s option to specify the starting offset.

To use od, type the following command in your terminal:

This will display the contents of the file in hexadecimal format. You can also use the -c option to display the file in ASCII format, and the -A option to specify the output format.

While hexdump and od are useful for viewing binary files, they are not suitable for editing them. This is because they display the contents of a file in a read-only manner.

Endianness

Endianness refers to the order in which bytes are stored in a binary file. There are two types of endianness: big-endian and little-endian. Big-endian systems store the most significant byte first, while little-endian systems store the least significant byte first.

Understanding endianness is important when working with binary files, as it affects how two-byte representation of binary data is interpreted. For example, the hexadecimal value 0x1234 can be interpreted as either 4660 (in big-endian) or 13330 (in little-endian).

To convert endianness, you can use the dd utility. For example, to convert a file from big-endian to little-endian, you can type the following command in your terminal:

This will create a new file with the same contents as the original file, but with the byte order reversed.

How to read a binary file using unix / linux command line

unix / linux bash Command line using:od -cx to show formatted data inside binary filetr -d Duration: 4:01

Storing Binary Data in Shell Variables

While it is possible to store binary data in shell variables, it is not recommended. This is because shell variables are designed to store text data, and may corrupt binary data when used improperly.

Читайте также:  Разметка дисков astra linux при установке

To read binary files without using shell variables, you can use the cat utility. For example, to display the contents of a binary file, you can type the following command in your terminal:

You can also use the ReadAllBytes method in C# to read binary file s. This method reads the entire contents of a file into a byte array, which can then be manipulated as needed.

Identifying Binary File Types

The Linux file command can be used to identify the type of a binary file. This is useful when working with unfamiliar file formats, as it allows you to determine the appropriate tools and utilities to use.

To use the file command, type the following command in your terminal:

This will display the type of the file, as well as any additional information that may be relevant.

It is important to note that the file system handles binary files differently on DOS or Windows compared to Unix. This is because DOS and Windows use a different file format than Unix, which can cause compatibility issues when transferring files between systems.

Executing Binary Files

To execute a binary file in Linux, you can use the “./binary_name” command. This tells the shell to execute the file as a program, rather than simply displaying its contents.

Before you can execute a binary file, you must first mark it as executable using the chmod command. For example, to mark a file as executable, type the following command in your terminal:

This will give the owner of the file execute permission, allowing them to run the file as a program.

Other helpful code examples for reading binary files in Linux using shell commands

In shell, linux read binary file code example

hexdump file // man hexdump

Conclusion

In conclusion, reading binary files in Linux using shell commands and utilities is an essential skill for anyone working with computers. By understanding the commands and utilities outlined in this blog post, you will be able to view, manipulate, and execute binary files with ease. Remember to practice using these commands and utilities, and to always exercise caution when working with binary files.

Источник

How to use bash script to read binary file content?

I want to read a character and then a fixed length of string (the string is not null terminated in the file, and its length is given by the preceding character). How can I do this in a bash script? How to define the string variable so that I can do some post-processing on it?

5 Answers 5

If you want to stick with shell utilities, you can use head to extract a number of bytes, and od to convert a byte into a number.

export LC_ALL=C # make sure we aren't in a multibyte locale n=$(head -c 1 | od -An -t u1) string=$(head -c $n) 

However, this does not work for binary data. There are two problems:

    Command substitution $(…) strips final newlines in the command output. There's a fairly easy workaround: make sure the output ends in a character other than a newline, then strip that one character.

string=$(head -c $n; echo .); string=$

If you have binary data, you'll want to switch to a language like Perl or Python.

If you want to be able to deal with binary file in shell, the best option (only?) is to work with hexdump tool.

hexdump -v -e '/1 "%u\n"' binary.file | while read c; do echo $c done 
head -cX binary.file | hexdump -v -e '/1 "%u\n"' | while read c; do echo $c done 

Read length (and work with 0 as length) and then "string" as byte decimal value:

len=$(head -c1 binary.file | hexdump -v -e '/1 "%u\n"') if [ $len -gt 0 ]; then tail -c+2 binary.file | head -c$len | hexdump -v -e '/1 "%u\n"' | while read c; do echo $c done fi 

Rather than just present a bunch of commands, can you explain what they do and how they work? What do the options mean? What output can the user expect from your commands? Please do not respond in comments; edit your answer to make it clearer and more complete.

Читайте также:  Очистка файловой системы linux

Well, I can copy manpages here, but I don't see the point. There is only basic commands used here, the only trick is the usage of hexdump.

read -N stops at null bytes, so this is not a suitable way to work with binary data. In general, shells other than zsh can't cope with nulls.

UPDATE (with hindsight). This question/answer (my answer) makes me think of the dog which keeps chasing the car.. One day, finally, he catches up to the car.. Okay, he caught it, but he really can't do much with it. This anser 'catches' the strings, but then you can't do much with them, if they have embedded null-bytes. (so a big +1 to Gilles answer.. another language may be in order here.)

dd reads any and all data. It certainly won't baulk at zero as a "length". but if you have \x00 anywhere in your data, you will need to be creative how you handle it; dd has no propblems with it, but your shell script will have problems (but it depends on what you want to do with the data). The following basically outputs each "data string", to a file with a line divider between each strin.

btw: You say "character", and I assume you mean "byte".
but the word "character" has become ambiguous in these days of UNICODE, where only the 7-bit ASCII character-set uses a single byte per character. And even within the Unicode system, byte counts vary depending on the method of encoding characters, eg. UTF-8, UTF-16, etc.

Here is a simple script to highlight the difference between a Text "character" and bytes.

STRING="௵" echo "CHAR count is: $" echo "BYTE count is: $(echo -n $STRING|wc -c)" # CHAR count is: 1 # BYTE count is: 3 # UTF-8 ecnoded (on my system) 

If your length character is 1-byte long and indicates a byte-length, then this script should do the trick, even if the data contains Unicode characters. dd only sees bytes regardless of any locale setting.

This script uses dd to read the binary file and outputs the strings seperated by a "=== ================================="; echo $div ((skip=0)) # read bytes at this offset while ( true ) ; do # Get the "length" byte ((count=1)) # count of bytes to read dd if=binfile bs=1 skip=$skip count=$count of=datalen 2>/dev/null (( $( strlen=$((0x$(/dev/null ddgetct=$( echo -e "\n$div" >>dataline # add a newline for TEST PURPOSES ONLY. cat dataline # ((skip=skip+count)) # read bytes from and including this offset done # echo

This script builds test data which includes a 3-byte prefix per line.
The prefix is a single UTF-8 encoded Unicode character.

# build test data # =============== prefix="௵" # prefix all non-zero length strings will this obvious 3-byte marker. prelen=$(echo -n $prefix|wc -c) printf \\0 > binfile # force 1st string to be zero-length (to check zero-length logic) ( lmax=3 # line max . the last on is set to 255-length (to check max-length logic) for ((i=1;i<=$lmax;i++)) ; do # add prefixed random length lines suflen=$(numrandom /0..$((255-prelen))/) # random length string (min of 3 bytes) ((i==lmax)) && ((suflen=255-prelen)) # make last line full length (255) strlen=$((prelen+suflen)) printf \\$((($strlen/64)*100+$strlen%64/8*10+$strlen%8))"$prefix" for ((j=0;j>binfile # 

Источник

Оцените статью
Adblock
detector