Unless I'm missing something, split
does split by line if you use -l
switch:
-l, --lines=NUMBER
put NUMBER lines per output file
so
split -l 1 inputfile
should do what you want.
I am assuming you use Linux and GNU split
. If so, you can do this directly with split
.
So, how does split
work? As with most *nix software, its manual is available by running man split
. Specifically, the general usage is
split [OPTION]... [INPUT [PREFIX]]
That means that you can specify the prefix yourself. For example, if you split a file called foo
and give the prefix bar:
$ ls
foo
$ split foo bar
$ ls
baraa barab barac foo
As you can see, since a prefix was given to split
, it has created the files called bar
followed by a suffix (aa
to ac
in this case). So, in your case, you want to give the name of the file as a prefix:
for f in *mp3; do split "$f" "$f"
But you also want to remove the extension so that splitting foo.mp3
does not result in foo.mp3aa
but fooaa
. This can be done using bash's string manipulation capabilities by writing ${f%.mp3}
instead of simple $f
.
Finally, you can use another nifty feature of split
to add the extension:
--additional-suffix=SUFFIX
append an additional SUFFIX to file names.
So, putting it all together:
for f in *mp3; do
split --bytes=2500k --additional-suffix=".mp3" -d "$f" "${f%.mp3}_";
done
I ran this in a directory that contained the following files:
aa.mp3 bb.mp3 cc.mp3
And it resulted in these split file names:
aa_00.mp3 aa_02.mp3 bb_00.mp3 bb_02.mp3 cc_00.mp3 cc_02.mp3
aa_01.mp3 aa_03.mp3 bb_01.mp3 bb_03.mp3 cc_01.mp3 cc_03.mp3
Best Answer
Not with
split
, but you can easily rename them afterwards, or you can do it inawk
: