I'm trying to append data to a binary file in ubuntu using the hexedit software. Is it an impossibility to append data to a binary file without overwriting the data already present in the file in ubuntu?
hexedit does apparently overwrite the entire file if you save any change; even if you were merely appending data.
Entering symbols (such as "<" ">") can be problematic as they represent hexedit commands. To enter them as file content, use "tab" key to switch to hex editing and enter their hexadecimal representations ( 3c and 3e accordingly). And as these are hardly the only keys like this, I'd advise you have an ASCII table handy anytime you are in a hex editor.
Many Linux admins will use the venerable "dd" when faced with bit-fiddling in binary files. The following is an example of how to replace a 4 byte section of an 80 byte file of random numbers with zeros at a position 15 bytes into the file:
dd if=/dev/urandom of=file.dat bs=1 count=80 #create primary file
dd if=/dev/zero of=block.dat bs=1 count=4 #create replacement data
dd if=./block.dat of=./file.dat bs=1 count=4 seek=15 conv=notrunc #replace data
To "insert" data into a binary file using dd is not strictly practical as it involves relocating the second half of the binary file; an evolution that overwrites itself.
However, you can construct a third file that is a concatenation of the first half of your data file, the inserted data, and then the second half of your data file and produce the net effect:
# Define where the insertion will take place; the 25th byte
insertPoint=25
# Create new file with the first 24 bytes of data file
dd if=file.dat bs=1 count=$(expr $insertPoint - 1) > file2.dat
# Append insert data
cat insert.dat >> file2.dat
# Append rest of data file
dd if=file.dat bs=1 skip=$(expr $insertPoint - 1) >> file2.dat
It should be pointed out that binary files are often such because they are used by services or applications. As such, care must be taken to ensure you are not altering a file that is actively being used by some other process.
-
I don't want to replace the 4 byte section but will like to insert it into any position in the file without overwriting another four byte section. Let me know if you understand what I'm saying. – Emmanuel Sep 19 '20 at 23:20
-
1
hexedit
, the<
and>
characters are used to toggle between the start and end of the file - what are you expecting them to do? To append data, press>
then type the new byte values, then hit F2 to save – steeldriver Sep 19 '20 at 22:23