Split a large file, without wasting disk space

FILE=file_name; CHUNK=$((64*1024*1024)); SIZE=$(stat -c "%s" $FILE); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done
It's common to want to split up large files and the usual method is to use split(1). If you have a 10GiB file, you'll need 10GiB of free space. Then the OS has to read 10GiB and write 10GiB (usually on the same filesystem). This takes AGES. . The command uses a set of loop block devices to create fake chunks, but without making any changes to the file. This means the file splitting is nearly instantaneous. The example creates a 1GiB file, then splits it into 16 x 64MiB chunks (/dev/loop0 .. loop15). . Note: This isn't a drop-in replacement for using split. The results are block devices. tar and zip won't do what you expect when given block devices. . These commands will work: hexdump /dev/loop4 . gzip -9 < /dev/loop6 > part6.gz . cat /dev/loop10 > /media/usb/part10.bin
Sample Output
$ truncate -s 1G big_file
$ ls -lh big_file
-rw-r--r--. 1 flatcap flatcap 1.0G Oct  3 13:55 big_file

$ FILE=big_file; SIZE=$(stat -c "%s" $FILE); CHUNK=$((64*1024*1024)); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done

By: flatcap
2014-10-03 13:18:19

What Others Think

Sweet! This problem is exactly the kind of thing that most people just accept as an unavoidable irritation. They know there's a better way to split a huge file, but they don't know how to express it. (Or, even worse, some people are stuck using a GUI where creative expression of solutions is prohibited.) This is an excellent example of the power and flexibility of the command line. P.S. Kudos for being the first person on commandlinefu to use the losetup(8) command.
hackerb9 · 361 weeks and 1 day ago
Many thanks hackerb9 :-) Despite working with large virtual machine images for many years, I only recently figured out what I should have been doing.
flatcap · 361 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: