How to write a file at various offsets to a filesystem partition with dd command

19,529

You can't "write a file" at an "offset into the partition" using dd this way -- you are just writing data into a file named "aaa" within the mounted file system on that partition.

"seek=" will indeed cause dd to lseek to the given position before beginning its writes -- that means that it will simply create a file called /mypart/aaa and lseek the given number of blocks into that file before writing.

If you omit "seek=", dd will write starting at the beginning of the file named "aaa".

Share:
19,529

Related videos on Youtube

Cees
Author by

Cees

Updated on September 18, 2022

Comments

  • Cees
    Cees over 1 year

    Requirement

    I want to write a file at various offsets into the partition

    Partition /dev/part2 is mounted at /mypart

    I tried the command below:

    dd if=/dev/urandom of=/mypart/aaa bs=1024 seek=0 count=15000
    dd if=/dev/urandom of=/mypart/aaa bs=1024 seek=15000 count=15000
    dd if=/dev/urandom of=/mypart/aaa bs=1024 seek=30000 count=15000
    

    Are they doing what I want to do? Are they writing a file to the partition at offset 0, 15000K and 30000K?

    At what offset is the file written if I omit seek from dd

    dd if=/dev/urandom of=/mypart/aaa bs=1024 count=15000
    
    • Admin
      Admin about 12 years
      Why? What are you really trying to do?
  • Cees
    Cees about 12 years
    I see. If I write directly to the device file at an offset, I basically destroy the filesystem on the partition. I tried that already. So how do I write data to a file at a particular offset in the file system? See my original requirement above.
  • Kapil Paranjape
    Kapil Paranjape about 12 years
    You're asking how to do something you can't do. Of course if you just write to some arbitrary location in the partition you will destroy it, and there is no API for telling a file system to write at an arbitrary location -- you don't even know if that location is available for data to be written in it and not, say, allocated for metadata. I must confess that I have no idea why you would want to be able to do this.
  • XTL
    XTL about 12 years
    If you have a filesystem on a device (like partition), you should think that the fs "owns" that device and you shouldn't touch it except by going through the fs.