How to view files in binary from bash?
Asked Answered
B

13

394

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?

Busload answered 19/11, 2009 at 18:4 Comment(0)
A
681

xxd does both binary and hexadecimal.

bin:

xxd -b file

hex:

xxd file
Agnesagnese answered 30/11, 2013 at 21:20 Comment(6)
sudo xxd /dev/diskn | less is now my new favorite thing.Rebate
...and it's preinstalled on Mac OS X and on Linux.Conscionable
This has the advantage over "hexdump" that it also shows the ASCII form on the side, making it easier to identify the location I want to look at.Keyte
And to dump the output to an ASCII text file for perusing & searching: xxd file > hex_dump_of_file.txtEmpathic
a supplment: xxd is not only for linux shell. I think it comes with vim. I had vim installed on windows, and I just found I can use xxd in windows too.Anodic
You could even play with binary files in bash: Read a file by bytes in BASHCristoforo
M
200
hexdump -C yourfile.bin

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

Magnetochemistry answered 19/11, 2009 at 18:7 Comment(6)
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?Busload
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.Revealment
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.Busload
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?Reck
What about output of hexdump -C data.bin | hexdump -CElectioneer
hexdump -C does not show binary output. This doesn't answer the question.Humphries
R
79
vi your_filename

hit esc

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

Revealment answered 19/11, 2009 at 18:11 Comment(1)
I've found :%!xxd adding unwanted characters i.e. new line to my file?Meany
F
41

As a fallback there's always od -xc filename

Fishbowl answered 19/11, 2009 at 18:7 Comment(0)
K
13

sudo apt-get install bless

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

Kinlaw answered 3/11, 2015 at 9:6 Comment(3)
Yes. I found this to be easier than Vim.Badly
beware, it comes with monoMackenziemackerel
Working in centos using snap: sudo snap install bless-unofficialEsch
C
13

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

strings <binary_filename>
Cardiomegaly answered 3/10, 2018 at 6:0 Comment(3)
IMO this is the simplest most elegant of all the answers. I wish I could upvote it more than once.Tijuanatike
The best answer hands down. This converts the Binary file into a JSON file. Not all heros wear capes,that is trueLob
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.Splinter
A
10

Hexyl formats nicely: sudo apt install hexyl

enter image description here

Androclinium answered 21/10, 2020 at 8:39 Comment(1)
very nice!/////Slouch
R
6
$ echo -n 'Hello world!' | hd
00000000  48 65 6c 6c 6f 20 77 6f  72 6c 64 21              |Hello world!|
0000000c
Relict answered 30/7, 2016 at 15:40 Comment(3)
hd is an alias to "hexdump -C" ... ?Artima
@Artima Yes indeed. I use it because it is easier to remember.Relict
And because it is present by default on some systems (Debian).Relict
O
3

See Improved Hex editing in the Vim Tips Wiki.

Oberheim answered 19/11, 2009 at 18:8 Comment(0)
F
3

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

xxd -p yourfile.bin | tr -d '\n'
Fame answered 10/5, 2021 at 23:34 Comment(0)
A
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

Accusal answered 11/4, 2017 at 15:11 Comment(0)
W
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
Warford answered 8/9, 2020 at 21:21 Comment(0)
P
-1

You can use hexdump binary file

sudo apt-get install hexdump

hexdump -C yourfile.bin
Psychosomatic answered 4/3, 2016 at 8:40 Comment(1)
This is just a duplicate of https://mcmap.net/q/86643/-how-to-view-files-in-binary-from-bashUruguay

© 2022 - 2024 — McMap. All rights reserved.