Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a 1TB big file (disk-image from a damaged drive) and a 1.3MB small file (beginning of a disk-file).

Using the contents of the small file, I want to overwrite portions of the big file. That is, I want to insert/overwrite the first 1.3MB of the 1TB-image using the small file.

Using small temporary files for testing I was unable to overwrite parts of the files. Rather, dd overwrote the files completely. This is not what I want.

Is dd able to do this?

share|improve this question
3  
In addition to the very good answers below, you can use the seek=nn and skip=nn parameters to set the starting points in your files, and count=nn to copy less than the whole input file. –  Guntram Blohm Jul 28 at 9:29

2 Answers 2

up vote 15 down vote accepted

If you use the conv=notrunc argument, you can replace just the first however many bytes.
e.g. dd conv=notrunc if=small.img of=large.img

root@debian:~/ddtest# dd if=/dev/zero of=file1.img bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 1.14556 s, 9.2 MB/s
root@debian:~/ddtest# dd if=/dev/urandom of=file2.img bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.207185 s, 5.1 MB/s
root@debian:~/ddtest# head file1.img 

<< Blank space here as it's all Zeroes >>

root@debian:~/ddtest# dd conv=notrunc if=file2.img of=file1.img 
2048+0 records in
2048+0 records out
1048576 bytes (1.0 MB) copied, 0.00468016 s, 224 MB/s
root@debian:~/ddtest# head file1.img 
^�v�y�ے!� E�91����  << SNIP Random garbage >>
root@debian:~/ddtest# 
share|improve this answer
2  
And it wasn't part of the original question, but you can write to somewhere other than the beginning of the output file using the seek option, and start reading somewhere other than the beginning of the input file using the skip option. Together with the other options, you can copy an arbitrary part of one file to an arbitrary position in another. –  hobbs Jul 28 at 19:58

If you want to overwrite only at the start of the file, and leave the rest intact, use the conv=notrunc option to prevent truncation:

dd conv=notrunc if=/dev/zero of=test bs=1024 count=1024

notrunc means:

Do not truncate the output file. Preserve blocks in the output file not explicitly written by this invocation of the dd utility.

It is in POSIX and so supported by every version of dd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.