I would like to add 8 bytes of data to the beginning of a binary file.

Is there a Linux command for this?


Here is one way to do that.

printf "\x68\x65\x6c\x6c\x6f\x20\x77\x6f" | cat - oldfile > newfile

The argument to printf is a sequence of 8 bytes in hex. Just replace the values I used (which are the ASCII characters "hello wo") with yours.

  • This should be an accepted answer. – Dmitry Volosnykh Jun 15 '16 at 9:25
  • So, I need to prepend 256kB to a 120GB file and I don't want to wait 30 minutes to completely copy and write the whole 120GB ... is there no way? – mxmlnkn Dec 18 '16 at 6:01
  • @mxmlnkn you'd need OS and filesystem driver support for that kind of merging. Maybe btrfs with a custom kernel? – jiggunjer Jan 25 '17 at 10:40
  • @mxmlnkn If you meant 256 KiB, which is a multiple of 512 B, then see answers to this question. You can concatenate two or more files into one special device without writing the result to any disk. – Kamil Maciorowski Feb 18 '18 at 23:10

it is not 'the' command, it is 'a bunch of commands' (in good old unix tradition):

  • put your 8 bytes to a file
  • append the original file to that file
  • rename the new file to the name of the original file.


% echo -n "12345689" > new_file
% cat original >> new_file
% mv new_file original

or, if you need to read the 8 bytes from somewhere else:

% dd if=inputstream of=new_file bs=1 count=8

and then continue as above.


This is not a proper answer to the original question, but merely a comment to address the very appropriate concern in the comment of @mxmlnkn

So, I need to prepend 256kB to a 120GB file and I don't want to wait 30 minutes to completely copy and write the whole 120GB ... is there no way?

Web search for 'fallocate prepend to file', this should show you a few StackExchange-based answers (EDIT: e.g. https://stackoverflow.com/a/37884191/9378469).

The fallocate approach, given Linux 4.1+ (XFS) or 4.2+ (XFS, ext4) allows you to insert filesystem-page-sized holes in files, hopefully in constant time. This may or may not be sufficiently flexible for your issue.

  • 2
    Welcome to Super User! It's admittedly tricky to approach a 7 year old question, but "search for <phrase>" is not a complete answer- can you expand on this or perhaps include an example here? :) – bertieb Feb 18 '18 at 22:56
  • I'm not aiming to be complete, merely to add some information to prevent readers from going away with incorrect conclusions. I didn't even want to post this as an answer, but a comment requires 50 rep, which leads to suboptimal situations like this one. I've edited the "answer" with a link to the most comprehensive answer I could find at the moment, though. – Foo Barian Feb 18 '18 at 23:32

tested in cygwin, should work on linux.

uses xxd and sed

in one line

$ xxd -p a.a | sed 's/^/6162636465/' | xxd -r -p > a2.a

broken down

display a.a
$ cat a.a

look at the hex of a.a  
user@comp ~
$ xxd -p a.a

The file actually contains a new line in the end as xxd shows above.

see how many lines xxd -p prints..would only ever be one regardless of the file it is given, since all characters within the file are turned into hex, and it prints an actual \n at the end of what it dumped. (which you'd see if you did xxd -p a.a | xxd -p)

$ xxd -p a.a | wc -l

sed operates on each line, and when inputting xxd -p, sed is always going to be being inputted only one line so very simple.

user@comp ~
$ xxd -p a.a | sed 's/^/6162636465/'

user@comp ~
$ xxd -p a.a | sed 's/^/6162636465/' | xxd -r -p > a2.a

user@comp ~
$ xxd -p a2.a

user@comp ~
$ cat a2.a

user@comp ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.