J. Raji
J. Raji

Reputation: 143

How do I split a file in different sizes?

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

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

Upvotes: 0

Views: 637

Answers (1)

John1024
John1024

Reputation: 113994

Saving the first 1724 bytes to one file and the remaining 300 bytes to another

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

Saving the first 300 bytes to one file and the remaining 1724 bytes to another

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

Upvotes: 2

Related Questions