Get file's first x bytes

is there a better way to do this:

head -c 10000k /var/dump.log | head -c 6000k

unfortunately, the "-c" option is not available on sun solaris. so i'm looking at "dd". but i dont know how to use it to achieve the same exact goal as the above head command.

this needs to work on both solaris and linux systems.

I can supply a translation from head to dd, but I need to understand why you used the above command line on Linux rather than just:

head -c 6000k /var/dump.log

and I need to understand what -c 6000k means to you. The Linux head man page defines the meaning for 6000kB and for 6000K, but it does not specify any meaning for 6000k???

Try this:

head -1 file | cut -c1-5

Print 1st 5 bytes of first record.

it means nothing to me. please show me your suggested solution.

If you don't know what you want, I have no idea whether what I suggest will succeed. I am guessing that the intent of the pipeline:

head -c 10000k /var/dump.log | head -c 6000k

is to extract the first 100001000 bytes from /var/dump.log and then extract the first 60001000 bytes from that. That is equivalent to extracting the first 60001000 bytes from /var/dump.log. If you had specified 10000kB and 6000kB, that is what would be done according to the Linux head man page. If you had used 10000K and 6000K, the multiplier would be 1024 instead of 1000 according to the Linux head man page. If the count had been negative in the 2nd call to head, the command line would make more sense since it would then be getting the last 6000(1000 or 1024) bytes from the first 10000*(1000 or 1024) bytes. But, I can only work with the command line you supplied and guess at the multiplier (or block size) you want. So:

dd if=/var/dump.log count=6000 bs=1000

might do what you want. If you want a block size of 1024 instead of 1000, change the bs=1000 to bs=1024 .

If wanted the last 6000 blocks of the first 10000 blocks from the file, you could try:

dd if=/var/dump.log skip=4000 count=6000 bs=1000
1 Like

Here are some examples of using 'dd' to accomplish what you are asking:

# dd outputs 1st 5 bytes of file to screen with diagnostic info
dd if=input_file bs=1 count=5
# dd outputs 1st 5 bytes of file to screen with diagnostic info going to file
dd if=input_file bs=1 count=5 2>test.txt
# dd outputs 1st 5 bytes of file to file with diagnostic info going to file
dd if=input_file bs=1 count=5 of=output_file 2>test.txt
1 Like