How to view files in binary from bash?

BashShellBinary

Bash Problem Overview


I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this?

Bash Solutions


Solution 1 - Bash

xxd does both binary and hexadecimal.

bin:

xxd -b file

hex:

xxd file

Solution 2 - Bash

hexdump -C yourfile.bin

unless you want to edit it of course. Most linux distros have hexdump by default (but obviously not all).

Solution 3 - Bash

vi your_filename

hit esc

Type :%!xxd to view the hex strings, the n :%!xxd -r to return to normal editing.

Solution 4 - Bash

As a fallback there's always od -xc filename

Solution 5 - Bash

sudo apt-get install bless

Bless is GUI tool which can view, edit, seach and a lot more. Its very light weight.

Solution 6 - Bash

If you want to open binary files (in CentOS 7):

strings <binary_filename>

Solution 7 - Bash

$ echo -n 'Hello world!' | hd
00000000  48 65 6c 6c 6f 20 77 6f  72 6c 64 21              |Hello world!|
0000000c

Solution 8 - Bash

Solution 9 - Bash

You can open emacs (in terminal mode, using emacs -nw for instance), and then use Hexl mode: M-x hexl-mode.

https://www.gnu.org/software/emacs/manual/html_node/emacs/Editing-Binary-Files.html

Solution 10 - Bash

Hexyl formats nicely: sudo apt install hexyl

enter image description here

Solution 11 - Bash

To get the output all in a single line in Hexadecimal:

xxd -p yourfile.bin | tr -d '\n'

Solution 12 - Bash

to convert a file to its binary codes(hexadecimal representation) we say:

xxd filename                                         #

e.g:

xxd hello.c                                          #

to see all the contents and codes in a binary file , we could use commands like readelf and objdump, hexdump ,... .

for example if we want to see all the convert all the contents of a binary file(executable, shared libraries, object files) we say:

hexdump binaryfilename

e.g.

hexdump /bin/bash

but readelf is the best utility for analyzing elf(executable and linking format) files. so if we say:

readelf -a /bin/bash

all the contents in the binary file bash would be shown to us, also we could provide different flags for readelf to see all the sections and headers of an elf file separately, for example if we want to see only the elf header we say:

readelf -h /bin/bash

for reading all the segments of the file:

readelf -l /bin/bash

for reading all the sections of the file:

readelf -S /bin/sh

but again as summary , for reading a normal file like "hello.c" and a binary file like bash in path /bin/bash in linux we say:

xxd hello.c

readelf -a /bin/bash

Solution 13 - Bash

You can use hexdump binary file

sudo apt-get install hexdump

hexdump -C yourfile.bin

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionadam_0View Question on Stackoverflow
Solution 1 - BashEmilio BoolView Answer on Stackoverflow
Solution 2 - BashtyranidView Answer on Stackoverflow
Solution 3 - BashDuckView Answer on Stackoverflow
Solution 4 - BashJim GarrisonView Answer on Stackoverflow
Solution 5 - BashsiddiqView Answer on Stackoverflow
Solution 6 - BashRajuView Answer on Stackoverflow
Solution 7 - BashAalex GabiView Answer on Stackoverflow
Solution 8 - BashintgrView Answer on Stackoverflow
Solution 9 - BashdividebyzeroView Answer on Stackoverflow
Solution 10 - BashcaduceusView Answer on Stackoverflow
Solution 11 - BashMariano IvaldiView Answer on Stackoverflow
Solution 12 - BashMgh GhView Answer on Stackoverflow
Solution 13 - BashcrakenView Answer on Stackoverflow