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.

4
  • 2
    usually head can do bytes, too - which flavor are you using? May 9, 2022 at 21:10
  • @frostschutz It seems I simply overlooked this! Coreutils 8.32. If you post this as an answer, I'll accept it - thank you for pointing out the (seemingly) obvious! May 9, 2022 at 21:22
  • @cyrus, I can't comment on your answer: couldn't you just switch the numbers? dd bs=2M count=1? May 10, 2022 at 16:43
  • related: unix.stackexchange.com/questions/346367/…
    – thanasisp
    May 11, 2022 at 0:13

1 Answer 1

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
3
  • A question about dd: when count does not have a suffix, isn't that counting 2,000,000 64k blocks (131,072,000,000 bytes)? Jun 9, 2022 at 18:32
  • 1
    @glennjackman, with the count_bytes iflag, the count is in bytes. Jun 9, 2022 at 21:29
  • Thanks, I didn't see that in my dd man page. Jun 10, 2022 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .