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
Stunnel Setup
Viewed 17179 times since Fri, Sep 28, 2018
What Is /dev/shm And Its Practical Usage
Viewed 7464 times since Tue, Mar 12, 2019
How to convert RAW image to VDI and otherwise
Viewed 14532 times since Wed, Oct 3, 2018
YUM CRON Enabling automatic updates in Centos 7 and RHEL 7
Viewed 11656 times since Fri, Oct 26, 2018
Secure Secure Shell
Viewed 10125 times since Fri, Aug 21, 2020
debian Debian/Ubuntu Linux: Find If Installed APT Package Includes a Fix/Patch Via CVE Number
Viewed 9163 times since Sun, Sep 23, 2018
RHEL: What is "SysRq key" and how to use it
Viewed 4729 times since Sat, Jun 2, 2018
15 Linux Yum Command Examples – Install, Uninstall, Update Packages
Viewed 2940 times since Thu, Oct 25, 2018
Top 25 Best Linux Performance Monitoring and Debugging Tools
Viewed 6585 times since Sun, Sep 30, 2018
Watchdog script to keep an application running
Viewed 17636 times since Tue, Jul 31, 2018