-   Linux - Newbie (
-   -   Split a file, byte-wise, not character-wise. (

stf92 11-17-2010 12:08 AM

Split a file, byte-wise, not character-wise.

I want to split file foo into two parts, and I only intersted in the first one. But the N bytes in which the first part consists, I want it to be an exact replica of the corresponding bytes of foo. More exactly:

foo        foo1        foo2
90        90          05
00        00

That is, I have foo, and what I want is foo1, foo2 being only the rest of foo. Any hint would be welcome. Regards.

rew 11-17-2010 02:31 AM

There is a program called "split". It will count the number of bytes if you pass it the -b argument.

split -b 2 foo

will work in your case. (but it will continue to create 2-byte files if foo is much larger).

If you only need the first part of foo, you can use:

head -c 2 foo > foo1

If you don't need to keep the old "foo", you can use the truncate system call. I believe there is now a "truncate" program somewhere, but I wrote it for my own use a long time ago.

truncate 2 foo

colucix 11-17-2010 02:53 AM

You can also use dd. To get the first N bytes into foo1, use:

dd if=foo of=foo1 ibs=N count=1
to put the rest into file foo2, you have to skip the first N bytes and copy the rest:

dd if=foo of=foo2 ibs=N skip=1
Pay attention to what you type, especially in writing the input and the output files correctly. See man dd for details about the various operands (if, of, ibs, skip and count).

JZL240I-U 11-17-2010 04:48 AM


Originally Posted by colucix (Post 4161605)
...See man dd for details about the various operands (if, of, ibs, skip and count).

Or go to


stf92 11-17-2010 06:43 AM

Thank you guys. I had used 'head' before writing LQ but something went wrong with a calculation I did and as a result I came to conclude head interprets bytes as characters, filtering non printable chars out.

All times are GMT -5. The time now is 07:11 PM.