LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Linux Answers > Applications / GUI / Multimedia
User Name
Password

Notices


By Kristijan at 2004-07-23 04:37
=====================
Split and Reassemble files
=====================

There always comes a time, where you wish that file was only a few kilobytes/megabytes smaller. Wether it be so it can fit onto your floppy disk, CDR 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 (split)
=================

Now, for this example I will use the file 'karaoke.mp3' which is 13M

Code:
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 2M 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 $ split -b 1945k karaoke.mp3

Code:
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 $ cat xa* > karaoke-restored.mp3

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

Code:
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
kristijan@neo testing$
By using cat, we have now recreated the original file. The 'xaa', xab' etc files may now be deleted.

There we have it, the karaoke file is now restored and can be played as per normal.

by JZL240I-U on Wed, 2005-07-13 03:05
Nice and easy, thanks.

I'd suggest to take the md5sum of the original file, to send it along as "karaoke.mp3-md5sum"-text file and compare the newly created file's md5sum with the original, just to make sure nothing happened during emailing.

by merlinblack on Tue, 2012-10-23 23:29
I've found another use for split - putting very large files, in my case dmg archives from a Mac, onto a FAT32 formated USB external HDD. Xcode weighs in at 4.1GB. Too big for FAT32.

In addition to the email example above, I could imagine if rsync'ing or scp'ing large files over a WAN, you may want to chop the file up so you can restart with out starting from scratch.

The split program of course exists on Mac's (basically BSD) as well as Linux.


  



All times are GMT -5. The time now is 02:00 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration