Skip to content

Instantly share code, notes, and snippets.

@icio

icio/size-split.sh

Created Sep 24, 2011
Embed
What would you like to do?
Split (by line) a file into similarly-sized parts each less than a given size
#!/usr/bin/env bash
# Filesize limit (MiB)
limit=$(($1 * 1000**2))
shift
# Accept multiple files in the arguments
for file in $@
do
# Skip arguments that aren't regular files
if [ ! -f $file ]
then
echo Skipping $file
continue
fi
# File counts
size=$(ls -l $file | awk '{ print $5 }')
lines=$(wc -l $file | awk '{ print $1 }')
parts=$((1 + $size/$limit))
linelimit=$((1 + $lines/$parts))
echo $file $parts parts $linelimit lines per part
split -l $linelimit $file "$file."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment