How can I change the first byte of multiple files in Linux? Willing to use perl/awk/sed/whatever. This must work correctly on binary files (i.e. not change any other bytes).

  • are you sure you mean byte? (a byte is a unit of data that is eight binary digits long). If you change the first byte of a file Im guessing you will corrupt the file. Are you referring to changing the first char of the filename? – DataPimp Mar 17 '10 at 17:18
  • No, I meant what I said. I need to invalidate the data for a bunch of torrents that my client has marked as completed, but the tracker doesn't agree. I figure the best way is to change the data as little as possible and force my client to re-check the data. Then it'll download at most one piece from each torrent, and tell the tracker the torrent is done. – jnylen Mar 17 '10 at 17:20

You can do something like echo -ne \\xFF | dd conv=notrunc bs=1 count=1 of=YOURFILE, replacing FF with your hex value. Try it first though :)

  • 1
    Nice and simple, works fine according to my testing. Is that easily adaptable to changing a byte other than the first? I played around with it a little but couldn't get it to work. – jnylen Mar 17 '10 at 20:10
  • 3
    @jnylen It should be adaptable to changing an arbitrary byte other than the first as well, the seek=<nblocks> option to dd is what you're looking for. – Kjetil Jørgensen Mar 19 '10 at 10:51
  • @Kjetil: thanks, that worked... I'd vote your comment up if I had 2 more reputation points :p – jnylen Mar 20 '10 at 19:14

compile this with gcc -o w1stb w1stb.c and use it like ./w1stb <file> <byte>:

#include <stdio.h>
int main(int argc, const char *argv[]) {

   int i;
   FILE* f;
   unsigned char b;

   if (argc < 3) {
       printf("usage: w <filename> <byte>\n");
       return 1;

   i = atoi(argv[2]);

   if (i < 0) {
       printf("error, negative byte\n");
       return 2;

   if (i > 255) {
       printf("error, to big byte\n");
       return 3;

   f = fopen(argv[1], "w");
   if (!f) {
       printf("error, can't open file\n");
       return 4;

   b = (unsigned char)i;
   fwrite(&b, 1, 1, f);

   return 0;

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.