How to append data in a file by dd?

20

6

I want to append new data in a file stored in SSD.

dd if=/dev/shm/test of=/data/sdb/test bs=1G oflag=append

But df -h shows the dd command always overwrite the test file, instead appends new data in the test file. I also tried

dd if=/dev/shm/test of=/data/sdb/test bs=1G conv=notrunc

It does not work, either.

city

Posted 2014-12-09T05:52:20.213

Reputation: 313

Answers

12

What about:

 dd if=/dev/shm/test bs=1G >>/data/sdb/test

mdpc

Posted 2014-12-09T05:52:20.213

Reputation: 4 176

your solution works. Thanks. But do you know why my solutions don't work? I have checked the man page. cannot find the reason. thanks. – city – 2014-12-09T06:46:20.600

29

dd if=/dev/shm/test of=/data/sdb/test bs=1G oflag=append conv=notrunc 

That is what I think you should have used.

REF : https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=373736

Charm_quark

Posted 2014-12-09T05:52:20.213

Reputation: 735

2Without count=1 option it appends all available space. – mixel – 2017-01-29T17:21:08.890

1

In Linux kernel 4.1 FALLOC_FL_INSERT_RANGE option was added. From fallocate(2) man page:

Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1) in mode increases the file space by inserting a hole within the file size without overwriting any existing data. The hole will start at offset and continue for len bytes. When inserting the hole inside file, the contents of the file starting at offset will be shifted upward (i.e., to a higher file offset) by len bytes. Inserting a hole inside a file increases the file size by len bytes.

And recently this option support was added to util-linux:

   -i, --insert-range
          Insert a hole of length bytes from offset, shifting existing
          data.

So when util-linux version 2.30 will be released and your linux distro will update to this version we will be able to increase file size in a flash by running:

fallocate -i -l 1G -o 128M /path/to/file

where 128M is the current file size.

mixel

Posted 2014-12-09T05:52:20.213

Reputation: 311

1

There is an easier way to append a sparse hole to a file.

truncate is much faster than dd. To grow the file with 10 bytes use:

 truncate -s +10 file.txt 

answer found in: https://serverfault.com/a/343726/70242

akostadinov

Posted 2014-12-09T05:52:20.213

Reputation: 1 140