1

I am piping information to a file using myTool > file.txt 2>&1, but the tool might generate GigaBytes worth of data - I need to cut off after the first N bytes, let say 2MB. It seems pv can not do that, and sadly it is not an option to go by lines (head).

Is there no basic tool to do this?

Ideally, it would work like this: myTool | limiter --amount 2M > file.txt 2>&1.

1 Answers1

1

Several implementations of head support a -c option for that. The GNU implementation also accepts suffixes, M for mebibytes, recent versions also supporting MB for megabyte (1,000,000) and MiB for mebibyte (1,048,576).

head -c 2097152
head -c 2M
head -c 2MiB
head -c 2000000
head -c 2MB

With pv, you can specify the size with -s and tell it to stop reading as soon as it's reached with -S. Again, suffixes are supported, but that's for powers of 1024, not 1000.

pv -Ss 2M
pv -Ss 2097152
pv -Ss 2000000

(add -q if you don't want progress information)

With GNU dd, you can do:

dd iflag=fullblock,count_bytes bs=64k count=2097152 status=none
dd iflag=fullblock,count_bytes bs=64k count=2MiB status=none
dd iflag=fullblock,count_bytes bs=64k count=2M status=none
dd iflag=fullblock,count_bytes bs=64k count=2000000 status=none
dd iflag=fullblock,count_bytes bs=64k count=2MB status=none