Is there a standard command for RedHat (except for the dd + truncate pair) for replacing data with zeros in the middle of a binary file?

  • if you need to replace all the characters, you can use the tr program (the octal 377 is decimal 255): $ cat файл1 | tr '\1-\377' '\0' > файл2 $ cat файл1 | tr '\1-\377' '\0' > файл2 - aleksandr barakin
  • Can bvi help? - don Rumata
  • @donRumata, bvi what is this? - avp
  • @avp, according to the description in turnips from bubunta, is a vim-like binary file editor. I didn’t pick it myself, but I remembered that there was such a thing when I saw your question. bvi.sourceforge.net - don Rumata
  • @donRumata, thanks. In this case, emacs also suitable. I thought, since dd and truncate mentioned in the question, it is obvious that we are talking about using it in the script (of course, if you wish, you can use the editor from the script) - avp

2 answers 2

You can use a one-liner in Python or another scripting language:

 python -c "f=open('some','r+b');f.seek(123);f.write('\0'*4);" 
  • Is the resulting length stored? Check for files larger than 4k. - 0andriy
  • @ 0andriy, yes, everything is OK. And what is the reason for doubt? - avp
  • one
    @avp should not open binary files in text mode. with open('file.bin', 'r+b') as f: f.seek(123); f.write(b'\0'*4) with open('file.bin', 'r+b') as f: f.seek(123); f.write(b'\0'*4) although for this dd sufficient. In zsh there is a sysseek. <> allows file to read and write to open. - jfs
  • Yes, of course it is better to open in binary mode - Abyx
  • @jfs, why? I tested in Ubuntu and RedHat. Works. - avp

In the piggy bank.

Emacs supports working with binary files as with ordinary files:

 Mx hexl-find-file 

open the file in hex format

 CMd 

insert byte in decimal format.

 CMo 

insert byte in hex format

 CMx 

insert byte in hexadecimal format

Links

  • Yes. You can also use the (quoted-insert) command (quoted-insert) usually attached to Ctrl-q ) by entering a byte code in the number system specified in the variable read-quoted-char-radix (default 8) - avp