Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux shell command to read/print file chunk by chunk

Tags:

file

linux

shell

Is there a standard Linux command i can use to read a file chunk by chunk? For example, i have a file whose size is 6kB. I want to read/print the first 1kB, and then the 2nd 1kB ... Seems cat/head/tail wont work in this case.

Thanks very much.

like image 516
datasunny Avatar asked Mar 05 '10 19:03

datasunny


2 Answers

You could do this with read -n in a loop:

while read -r -d '' -n 1024 BYTES; do
    echo "$BYTES"
    echo "---"
done < file.dat
like image 197
John Kugelman Avatar answered Nov 09 '22 08:11

John Kugelman


dd will do it

dd if=your_file of=output_tmp_file bs=1024 count=1 skip=0

And then skip=1 for the second chunk, and so on.

You then just need to read the output_tmp_file to get the chunk.

like image 35
sorpigal Avatar answered Nov 09 '22 07:11

sorpigal