J. Raji J. Raji - 1 year ago 32
Bash Question

How do I split a file in different sizes?

I want to split a file (using

for example, I have a file of 2024 bytes, I want to split this file in two parts: 300 bytes and 1724 bytes.

The first has to be 300 bytes and the second 1724 bytes.

Answer Source

Answer for first version of question

Let's start by creating a test file of 2024 bytes:

$ head -c2024 /dev/urandom >testfile

Now, let's use split with a preferred size of 1724 bytes:

$ split -b1724 testfile

We now have testfile split into two files, the first of size 1724 and the second of size 300:

$ ls -l xa*
-rw-rw---- 1 john1024 john1024 1724 Sep 22 21:42 xaa
-rw-rw---- 1 john1024 john1024  300 Sep 22 21:42 xab

Answer for second version of question

To split the file again, this time taking 300 bytes from the beginning and saving them part1 while the remaining 1724 bytes go into file part2:

$ head -c300 testfile >part1
$ tail -c+301 testfile >part2
$ ls -l part*
-rw-rw---- 1 john1024 john1024  300 Sep 22 22:01 part1
-rw-rw---- 1 john1024 john1024 1724 Sep 22 22:01 part2