Otaqui.com Blog

Split and Rename text files from the Bash command line

I occasionally read text files (usually downloaded from the Gutenberg Project ) on my mobile phone. This is not the best user experience, but bearable and very useful at times.

The most annoying issue is the incredibly slow management of very large text files (sometimes greater than 1Mb) that my phone displays – so i decided to rename them.

Using my mac, I came up with this couple of commands to do the work for me, between them they will split all text files into 1000 line chunks, and assuming an original filename of “somefile.txt” also rename them to “somefile_XX.txt” where XX is actually two letters denoting the part, starting at aa, ab, ac, etc.

The first command is dead easy and splits the files:

for file in *.txt ; do split $file $file ; done

This will split the files and turn “somefile.txt” into “somefile.txtaa”, “somefile.txtab”, “somefile.txtac” etc. which is obviously not great – so we need the next step:

for file in * ; do mv $file `echo $file |
sed 's/(.*).txt([a-z]{2})/1_2.txt/'` ; done

Now you should have all the files nicely named “somefile_aa.txt”, “somefile_ab.txt”, “somefile_ac.txt” etc.

You might prefer numbers rather than letters, in which case just check out the man page for the ‘split’ command.