I would like to add 8 bytes of data to the beginning of a binary file.
Is there a Linux command for this?
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.
btrfs with a custom kernel?
– jiggunjer
Jan 25 '17 at 10:40
it is not 'the' command, it is 'a bunch of commands' (in good old unix tradition):
or:
% 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.
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
abc
look at the hex of a.a
user@comp ~
$ xxd -p a.a
6162630a
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
1
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/'
61626364656162630a
user@comp ~
$ xxd -p a.a | sed 's/^/6162636465/' | xxd -r -p > a2.a
user@comp ~
$ xxd -p a2.a
61626364656162630a
user@comp ~
$ cat a2.a
abcdeabc
user@comp ~
$
To prepend data to a binary file, increase the file size, then prepend with data:
xxd file.old | xxd -r -s 8 > file.new;
data="e.i..ght";
echo -n $data | xxd -p | xxd -r -p - file.new
file.old
00000000 00 00 00 13 02 d1 20 83 00 73 02 b3 00 73 04 33 |...... ..s...s.3|
00000010 00 73 64 b3 00 00 00 13 |.sd.....|
00000018
file.new
00000000 65 2e 69 2e 2e 67 68 74 00 00 00 13 02 d1 20 83 |e.i..ght...... .|
00000010 00 73 02 b3 00 73 04 33 00 73 64 b3 00 00 00 13 |.s...s.3.sd.....|
00000020