380

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?

adam_0
  • 6,920
  • 6
  • 40
  • 52

13 Answers13

658

xxd does both binary and hexadecimal.

bin:

xxd -b file

hex:

xxd file
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Emilio Bool
  • 6,724
  • 1
  • 15
  • 6
194
hexdump -C yourfile.bin

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

John_West
  • 2,239
  • 4
  • 24
  • 44
tyranid
  • 13,028
  • 1
  • 32
  • 34
  • 1
    I like this idea, but like the other suggestions it only outputs hex. Obviously this is much more compact than binary, but I am dealing with very small files so binary is preferred. Is hex the only way I will be able to view the file? – adam_0 Nov 19 '09 at 18:21
  • 1
    Well how small is the file? Anything over a couple of bytes and you will start to lose your mind using binary anyway. Hex makes much more sense for most things. If you are uncomfortable with hex just locate the bytes in which you are interested and convert them using a hex calculator. – Duck Nov 19 '09 at 18:38
  • 3
    I need to make sure that my file is compressing correctly and I don't know what it should look like in hex (the size of each unit is 7 bits), so I would have to crunch the numbers by hand. – adam_0 Nov 19 '09 at 18:38
  • do you have any methods to see text from binary file? I can get HEX code, but how should i decode it to normal human text? – Lukas Liesis Aug 22 '14 at 13:54
  • What about output of hexdump -C data.bin | hexdump -C – cyb0k May 12 '16 at 18:33
  • 2
    `hexdump -C` does not show binary output. This doesn't answer the question. – Daniel Sep 28 '18 at 18:22
76
vi your_filename

hit esc

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

Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
Duck
  • 26,924
  • 5
  • 64
  • 92
40

As a fallback there's always od -xc filename

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
13

sudo apt-get install bless

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

siddiq
  • 203
  • 3
  • 6
13

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

strings <binary_filename>
Pang
  • 9,564
  • 146
  • 81
  • 122
Raju
  • 165
  • 1
  • 2
  • IMO this is the simplest most elegant of all the answers. I wish I could upvote it more than once. – Olumide Mar 20 '19 at 12:22
  • The best answer hands down. This converts the Binary file into a JSON file. Not all heros wear capes,that is true – Shubham Pawar Apr 14 '20 at 14:59
  • 4
    It doesn't convert it into JSON file. It only finds the printable strings in an object and show you. It doesn't convert the binary file into text or any format at all. – Shai Alon Sep 30 '20 at 09:44
7

Hexyl formats nicely: sudo apt install hexyl

enter image description here

caduceus
  • 1,542
  • 2
  • 16
  • 21
6
$ echo -n 'Hello world!' | hd
00000000  48 65 6c 6c 6f 20 77 6f  72 6c 64 21              |Hello world!|
0000000c
Aalex Gabi
  • 1,525
  • 1
  • 18
  • 32
3

See Improved Hex editing in the Vim Tips Wiki.

intgr
  • 19,834
  • 5
  • 59
  • 69
2

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

dividebyzero
  • 2,190
  • 1
  • 21
  • 33
2

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

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

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
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Mgh Gh
  • 81
  • 4
-1

You can use hexdump binary file

sudo apt-get install hexdump

hexdump -C yourfile.bin
craken
  • 1,411
  • 11
  • 16