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 17557 times since Fri, Sep 28, 2018
How to Register and Enable Red Hat Subscription, Repositories and Updates for RHEL 7.0 Server
Viewed 11754 times since Mon, Oct 29, 2018
Linux nslookup Command Examples for DNS Lookup
Viewed 8745 times since Sat, Sep 29, 2018
Red Hat Cluster Tutorial
Viewed 1918 times since Sun, Jun 3, 2018
Fix rpmdb: Thread died in Berkeley DB library
Viewed 20865 times since Fri, Feb 14, 2020
Jak wygenerować silne hasła jednorazowe w Linuksie?
Viewed 2186 times since Thu, May 24, 2018
Applescript: Run or Call a Shell Script
Viewed 4058 times since Tue, Aug 6, 2019
stunnel Securing telnet connections with stunnel
Viewed 1348 times since Sun, Dec 6, 2020
Logrotate Example for Custom Logs
Viewed 2403 times since Sun, Jan 12, 2020
LUKS List available methods of encryption for LUKS
Viewed 2801 times since Fri, Jul 13, 2018