If you happen to have a file that looks like this:
old_name1, new_name1
old_name2, new_name2
old_name3, new_name3
You can do a dirty little trick:
sed 's/^/mv -vi "/;s/, /" "/;s/$/";/' < names.csv | bash -
The sed comamnds (delimited by semicolons) does this (s
is for substitute, /
is used as a delimiter, any other character would do, @
is often used as well):
s/^/mv -vi "/
- adds mv -vi "
at the beginning of each line (^
is beginning of line);
s/, /" "/
- replaces comma followed by a space with " "
;
s/$/";/
- appends double quotes to all lines ($
means end of line)
and hence the output of sed
will be this:
mv -vi "old_name1" "new_name1"
mv -vi "old_name2" "new_name2"
mv -vi "old_name3" "new_name3"
which will be fed to bash
to execute. The quotes around filenames are not mandatory, they just guard any possible spaces in the filenames (however they will defintely not help against spaces followed by a comma, since that is used as delimiter). the -v
instructs mv
to display what it is doing (in case something goes wrong, you'll know what happened), -i
will cause it to ask if it were to overwrite already existing file.
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
When you are sure that it does what you want it to do, remove the
echo
.The double dash (
--
) is necessary to stopmv
from interpreting the-
in the filenames as an option. Quoting the variables is necessary for the cases where the filenames contains spaces.