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
Transform XML to CSV Format | Unix String Pattern Manipulation The Ugly Way
Viewed 5197 times since Sun, Jan 9, 2022
Open SSL HowTo: Decode SSL Certificate
Viewed 6017 times since Mon, Feb 18, 2019
List of 10 Must Know Oracle Database Parameters for Database Administrator
Viewed 125605 times since Thu, Jun 21, 2018
RHEL: Building a custom kernel on RHEL 6
Viewed 3998 times since Sat, Jun 2, 2018
Tips to Solve Linux & Unix Systems Hard Disk Problems
Viewed 4033 times since Fri, May 15, 2020
RHEL: Adding a boot entry to GRUB/GRUB2 configuration
Viewed 4508 times since Sun, May 27, 2018
Get UUID of Hard Disks [Update]
Viewed 2120 times since Tue, Jul 17, 2018
Red Hat ADDING SWAP SPACE
Viewed 1958 times since Fri, Jun 8, 2018
LVM: Create a new Volume Group
Viewed 1998 times since Sat, Jun 2, 2018
RHCS6: Clustered LVM
Viewed 2135 times since Sun, Jun 3, 2018