29

Can I tell xxd to not print any line breaks in its output and have my dump as one continuous line?

[user@localhost] : ~ $ echo -n "this is a long line and xxd is going to take multiple lines to print it" | xxd -p
746869732069732061206c6f6e67206c696e6520616e6420787864206973
20676f696e6720746f2074616b65206d756c7469706c65206c696e657320
746f207072696e74206974
5
  • 11
    You could simply use tr to delete the newlines, e.g. ... | xxd -p | tr -d \\n Commented Jul 26, 2015 at 23:12
  • 1
    It depends on what you need it for, but one handy option of xxd is that it ignores whitespace for the reverse -r of its postcript/plain -p dump (or any plain hexdump for that matter). eg. The following line wraps with \n, but the reversed output is exactly what was input: echo {1..14} | xxd -p | xxd -p -r produces output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14\n – the \n is from the echo Commented Jul 27, 2015 at 0:20
  • 3
    You can also use hexdump -v -e '/1 "%02X"' instead of xxd. Commented Oct 10, 2016 at 16:29
  • I'd stick with piping to tr -d '[[:blank:][:space:]]'. Commented Nov 17, 2022 at 12:20
  • 1
    Short answer: Turn xxd -p into xxd -p -c 0 to make it single-line. (I posted it as an answer, but got removed because included in more complex answers…) Commented Sep 16, 2024 at 15:53

2 Answers 2

32

What you need is the -c option.

# echo -n "this is a long line and xxd will print it as one line" | xxd -p -c 1000000

746869732069732061206c6f6e67206c696e6520616e64207878642077696c6c207072696e74206974206173206f6e65206c696e65

Here is some info from the documentation:

-c cols | -cols cols format octets per line. Default 16 (-i: 12, -ps: 30, -b: 6). Max 256.

Documentation says that the max value for "c" parameter is 256, but I tried greater values and it worked. Check it out:

# xxd -c 1000000 -p -l 1000000 /dev/urandom | wc -c
2000001

Here I dump one million bytes from /dev/random and I get a string of 2 million + 1 characters. Each byte from /dev/random is represented by 2 characters and additional byte is the final newline.

3
  • With xxd version V1.10, it does not work: xxd -c 10000 xxd: invalid number of columns (max. 256). Commented Aug 27, 2021 at 8:58
  • @cdupont, you forgot the -p / -ps Commented Jun 16, 2022 at 9:21
  • 1
    You can just give 0 to the -c parameter to have it on a single line. Commented Jul 27, 2024 at 10:41
10

With xxd version 2022-01-14 (coming with vim 8.2.4088) or newer, you can now use -p and -c with value 0 as said in the man page:

-c cols | -cols cols

Format cols octets per line. Default 16 (-i: 12, -ps: 30, -b: 6). Max 256. No maximum for -ps. With -ps, 0 results in one long line of output.

For example:

$ echo "Lorem Ipsum is simply dummy text of the printing and typesetting industry." | xxd -p -c0 
4c6f72656d20497073756d2069732073696d706c792064756d6d792074657874206f6620746865207072696e74696e6720616e64207479706573657474696e6720696e6475737472792e0a
1
  • 1
    Just be careful that for previous version -c 0 is just silently ignored instead of raising an error. Was burned by this today. Commented Sep 27, 2023 at 11:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.