Split and Reassemble files

I wrote this a few years back for LinuxQuestions.org. It was around 2004 from memory, and majority of the mail servers in Internet land only allowed for 2MB file attachments. This has now change, but I thought I’d post the article anyway.

There always comes a time, where you wish that file was only a few kilobytes/megabytes smaller. Whether it be so it can fit onto your floppy disk, CD-R etc, or so you can meet the attachment limit on an e-mail server. This isn’t really a command that you would use everyday, but it might come in handy.

Splitting the file

kristijan@slackware testing$ ls -lh
total 13M
-rw-rw-r--  1 kristijan users 13M Jul 23 18:18 karaoke.mp3

Just say we wanted to e-mail this file to a friend, but the e-mail server only allowed a maximum of 2MB attachments.

(Note** I like to play safe, so I will make my chunks of data 1.9M)

1.9 x 1024 = 1945.6

(Note** To play safe once again, I will leave out the decimal and just use 1945)

The command that we will use is:

kristijan@slackware testing$ split -b 1945k karaoke.mp3
kristijan@slackware testing$ split -b 1945k karaoke.mp3
kristijan@slackware testing$ ls -lh
total 26M
-rw-rw-r--  1 kristijan users  13M Jul 23 18:18 karaoke.mp3
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaa
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xab
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xac
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xad
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xae
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaf
-rw-rw-r--  1 kristijan users 1.4M Jul 23 18:22 xag

OK, what we have now done is told ‘split’ to use bytes per output file (-b). For more information and arguments on split, view the man pages (man split). Split how now ‘split’ karaoke.mp3 into 7 smaller files named ‘xaa’, ‘xab’, ‘xac’ etc, which are all under 2M in size.

These files now meet the e-mail server’s attachment limit and can be sent.

Recreating the file (cat)

Recreation of the karaoke.mp3 is even easier. For this, we will be using ‘cat’. See the man pages for more information on ‘cat’ (man cat).

The command that we will use is:

kristijan@slackware testing$ cat xa* > karaoke-restored.mp3

(Note** Before removing the original file, I recommend that you make a backup of it)

kristijan@slackware testing$ rm karaoke.mp3
kristijan@slackware testing$ ls -lh
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaa
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xab
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xac
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xad
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xae
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaf
-rw-rw-r--  1 kristijan users 1.4M Jul 23 18:22 xag
kristijan@slackware testing$ cat xa* > karaoke-restored.mp3
kristijan@slackware testing$ ls -lh
total 26M
-rw-rw-r--  1 kristijan users  13M Jul 23 18:39 karaoke-restored.mp3
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaa
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xab
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xac
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xad
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xae
-rw-rw-r--  1 kristijan users 1.9M Jul 23 18:22 xaf
-rw-rw-r--  1 kristijan users 1.4M Jul 23 18:22 xag

By using cat, we have now recreated the original file. The ‘xaa’, xab’ etc files may now be deleted.

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Using Kerberos security with Server for NFS
Viewed 9493 times since Wed, Jun 27, 2018
RHEL: Force system to prompt for password in Single User mode
Viewed 7174 times since Sat, Jun 2, 2018
debian Install a newer kernel in Debian 9 (stretch) stable
Viewed 1716 times since Sun, Sep 23, 2018
Setting up encrypted tunnel using stunnel
Viewed 2359 times since Fri, Sep 28, 2018
How to encrypt a partition using LUKS?
Viewed 1769 times since Fri, Jul 13, 2018
LVM: Move allocated PE between Physical Volumes
Viewed 3806 times since Sat, Jun 2, 2018
Script to Offline and Remove A Disk In Linux
Viewed 1868 times since Mon, Jan 28, 2019
Linux - How to get IP and MAC address of ethernet adapter in Linux
Viewed 2622 times since Fri, Jun 8, 2018
How To Create a Linux Swap File
Viewed 2359 times since Fri, Jun 8, 2018
RHEL: Remove existing SAN LUNs
Viewed 13981 times since Sat, Jun 2, 2018