Bash Scripting: How to fix files? (write the given line at the given position of the file)

Im writing a script to change the UUID of an NTFS partition (AFAIK, it's not there). This means writing 8 bytes from 0x48 to 0x4F (72-79 decimal) from /dev/sdaX (X is # of my section).

If I wanted to change it to a random UUID, I could use this:

 dd if=/dev/urandom of=/dev/sdaX bs=8 count=1 seek=9 conv=notrunc 

Or I could change /dev/urandom to /dev/sdaY to clone the UUID from another partition.

But ... what if I want to create a personalized UUID? I already saved (and checked the regular expression) in the $UUID variable in hexadecimal string format (16 characters), for example:

 UUID="2AE2C85D31835048" 

I thought about it:

 echo "$UUID" | xxd -r -p | dd of=/dev/sdaX ... 

It's just a scratch ... I'm not sure about the exact option to make it work. My question is:

  • Is echo $var | xxd -r | dd really echo $var | xxd -r | dd echo $var | xxd -r | dd echo $var | xxd -r | dd best approach?
  • What will be the exact command and parameters to make it work?

As for the answers, Im also looking for:

  • An explanation of all the options used and what they do.
  • If possible, an alternative command to check it in the file and / or screen before changing the section.

I already have a 100-byte ntfs.bin dump file which I can use for tests and check the results with

 xxd ntfs.bin 

Therefore, any solution that gives me a way to check the results using xxd on the screen, so I can compare with the original ntfs.bin file, will be highly appreciated.

Thanks!

+3
source share
2 answers

Try:

 UUID="2AE2C85D31835048" echo "$UUID" | xxd -r -p | wc -c echo "$UUID" | xxd -r -p | dd of=file obs=1 oseek=72 conv=block,notrunc cbs=8 
+1
source

Look here:

Replace characters in a file by index

 # start at char 20, replace the next 11 chars with '#' echo '###########' | dd of=FILENAME seek=20 bs=1 count=11 conv=notrunc 
+1
source

Source: https://habr.com/ru/post/1432253/


All Articles