Hot answers tagged

173

Often times when in a Unix/Linux terminal (Bash) for example you'll use the commands more or less or cat to view a file. When you do this and the file isn't meant to be viewed (such as /bin/ls) you'll get output like this:                  What's going on here is that you just ...


136

You can use grep -a 'pattern'. from man grep page: -a, --text Process a binary file as if it were text; this is equivalent to the ‘--binary-files=text’ option.


101

According to this answer by tyranid: hexdump -C yourfile.bin unless you want to edit it of course. Most Linux distros have hexdump by default (but obviously not all). Update According to this answer by Emilio Bool: xxd does both binary and hexadecimal For bin : xxd -b file For hex : xxd file


62

:help i_CTRL-V_digit In insert mode, type Ctrl+V followed by a decimal number (0-255) o then an octal number (o0-o377, i.e., 255 is the maximum value) x then a hex number (x00-xFF, i.e., 255 is the maximum value) u then a 4-hexchar Unicode sequence U then an 8-hexchar Unicode sequence Decimal and octal numbers are limited to three digits.  Decimal numbers ...


56

I used the -r and -p switch to xxd: $ echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181 Thanks to inspiration from @Gilles answer, here's a perl version: $ echo '0006303030304e43' | perl -e 'print pack "H*", <STDIN>' | nc -l localhost 8181


46

Various people have answered some aspects of the query, but not all. All files on computers are stored as 1's and 0's. Images, text files, music, executable applications, object files, etc. They are all 0's and 1's. The only difference is that they are interpreted differently depending upon what opens them. When you view a text file using cat, the ...


45

You can pass data into curl via STDIN like so: echo -e '...data...\n' | curl -X POST --data-binary @- http://foo.com The @- tells curl to pull in from STDIN. To pipe binary data to curl (for example): echo -e '\x03\xF1' | curl -X POST --data-binary @- http://foo.com


43

shc is what you're looking for. get it here: shc Extract, cd into dir, make and then ./shc -f SCRIPT. Done. Everything you need to do this, you find here: SHC Howto


42

No. Binaries must be (re)compiled for the target architecture, and Linux offers nothing like fat binaries out of the box. The reason is because the code is compiled to machine code for a specific architecture, and machine code is very different between most processor families (ARM and x86 for instance are very different). EDIT: it is worth noting that some ...


39

It's fairly straightforward to do the conversion from binary in pure bash (echo and printf are builtins): Binary to decimal $ echo "$((2#101010101))" 341 Binary to hexadecimal $ printf '%x\n' "$((2#101010101))" 155 Going back to binary using bash alone is somewhat more complex, so I suggest you see the other answers for solutions to that.


37

I've had an occasion where none of the usual tricks, reset or stty sane, worked (after accidentally calling print on a python bytearray). I had success with method 2 listed on this helpful blog. I've since created a most helpful alias: alias fix='echo -e "\033c"'


36

$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB -e expression evaluate the given expression as perl code -p: sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression. -l: even more like ...


34

It's your locale and tr problem. Currently, GNU tr fully supports only single-byte characters. So in locales using multibyte encodings, the output can be weird: $ </dev/urandom LC_ALL=vi_VN.tcvn tr -dc '[:print:]' | head -c 64 `�pv���Z����c�ox"�O���%�YR��F�>��췔��ovȪ������^,<H ���> The shell will print multi-byte characters correctly, but GNU ...


29

grep is a text processing tool. It expects their input to be text files. It seems that the same goes for tr on macOS (even though tr is supposed to support binary files). Computers store data as sequences of bytes. A text is a sequence of characters. There are several ways to encode characters as bytes, called character encodings. The de facto standard ...


26

Here a solution without xxd or perl: If the echo builtin of your shell supports it (bash and zsh do, but not dash), you just need to use the right backslash escapes: echo -ne '\x00\x06\x30\x30\x30\x30\x4e\x43' | nc -l localhost 8181 If you have /bin/echo from GNU coreutils (nearly standard on Linux systems) or from busybox you can use it, too. With sed ...


25

There are two ways of allowing you to run the binary without specifying its path (not including creating aliases or shell functions to execute it with an absolute path for you): Copy it to a directory that is in your $PATH. Add the directory where it is to your $PATH. To copy the file to a directory in your path, for example /usr/local/bin (where locally ...


23

You can use xxd to convert from ASCII and binary. $ echo -n "A" | xxd -b 0000000: 01000001 A $ echo -n "A" | xxd -b | awk '{print $2}' 01000001 Converting bases If you're looking to do just base conversions between Hex, Octal, & Dec I usually use the basic calculator command line tool (bc) to do such ...


22

Dealing with binary data at a low level in shell scripts is generally a bad idea. bash variables can't contain the byte 0. zsh is the only shell that can store that byte in its variables. In any case, command arguments and environment variables cannot contain those bytes as they are NUL delimited strings passed to the execve system call. Also note that: ...


20

You are right in that echo & company don't seem to handle binary that well. I suspect that the null characters break the stream all too early. You can convert picture information in some ASCII based format. For instance, this is with base64: $ pic=`base64 pic.jpeg` $ echo $pic | base64 --decode > pic2.jpeg $ diff pic* $ echo $? 0


20

That's just the binary representation of the ascii encoding of "Hello World", not an executable, there's no way to execute that.


18

One of the first things I had to memorise for computer science was Data + Interpretation = Useful Information. A corollary of this is that if you're missing Data or Interpretation, you have nothing. The data itself can't tell you how to interpret it. (you can have metadata which tells you this, but then you need to know how to interpret the metadata too) ...


18

This uses od to show one hex value per line, then sorts and counts: od -t x1 -w1 -v -An mybinaryfile | sort | uniq -c (-w1 is an extension, it’s not mandated by POSIX.)


17

When using vim -b, this displays all high characters as <xx>: set encoding=latin1 set isprint= set display+=uhex Any single-byte encoding will work, vim uses ASCII for all lower chars and has them hard-coded as printable. Setting isprint to empty will mark everything else as non-printable. Setting uhex will display them as hexadecimal. Here is how ...


17

In a strict sense a binary file is one which is not character encoded as human readable text. More colloquially, a "binary" refers to a file that is compiled, executable code, although the file itself may not be executable (referring not so much to permissions as to the capacity to be run alone; some binary code files such as libraries are compiled, but ...


17

I don't know if your version of sed will be binary-clean or if will choke on what it thinks are really long lines in its input, but barring those issues, editing the string in-place should work. To see whether it does, compare the old and new versions with cmp -l. It should tell you whether or not the only three differences between the two files are those 3 ...


17

I recently starting using Noah to run Linux binaries in macOS. You can install using homebrew (brew install linux-noah/noah/noah). Then you should be able to do this: noah linux_binary In my experience the behavior of the binary matches what I see on my Ubuntu machine.


16

This Super User question: Why don't you see binary code when you open a binary file with text editor? addresses your first point quite well. Binary and text data aren't separated: They are simply data. It depends on the interpretation that makes them one or the other. If you open binary data (such as an image file) in a text editor, much of it won't make ...


16

Assuming that by binary, you mean binary data as in data with any possible byte value including 0, and not base-2 numbers: To convert from binary, od (standard), xxd (comes with vim) or perl's unpack come to mind. od -An -vtu1 # for decimal od -An -vtx1 # for hexadecimal xxd -p # for hexa perl -pe 'BEGIN{$\="\n";$/=\30};$_=unpack("H*",$_)' # like xxd -p ...


16

Elizabeth Myers is correct, each architecture requires a compiled binary for the architecture in question. To build binaries for a different architecture than your system runs on you need a cross-compiler. In most cases you need to compile a cross compiler. I only have experience with gcc (but I believe that llvm, and other compilers, have similar ...


15

I assume that you use vim, because :helpoctal is a vim's command. On some systems vi is just a symlink to vim which runs it in vi-compatible mode. In vim: You can enter unicode characters from basic multilingual plane you can use: Press ctrl+v and then enter four digit hex unicode code. Another option is digraphs. You can read more about them in vim's ...


Only top voted, non community-wiki answers of a minimum length are eligible