-   Linux - Software (
-   -   Rename 100+ files from a text file (

AGazzaz 09-25-2008 04:25 PM

Rename 100+ files from a text file
I have a folder with 114 files, these files are numbered 001, 002,...
I want to rename these files to names found in a text file
How can I do that?!


billymayday 09-25-2008 04:29 PM

What's the format of the text file - how about some example lines?

darthaxul 09-25-2008 04:48 PM

i just did something similar
I have no idea if this works lol...

comm oldname newname > both
for f in both; do
first=`cat both | 'awk {print$1}'`
second=`cat both | 'awk {print$2}'`
mv $second $first

AGazzaz 09-25-2008 06:08 PM

Thank you for your replies,

The files extention is mp3 and the text file format is txt

I would like to change the files as follow:
001.mp3 ---> Wonders.mp3
002.mp3 ---> Magic.mp3
003.mp3 ---> lame.mp3

now the names; "wonders","magic","lame" are found in a text file

how can I rename the long list of files?

billymayday 09-25-2008 06:13 PM

Sample lines?

AGazzaz 09-25-2008 06:15 PM

The file will look like this:


I hope it helps.

billymayday 09-25-2008 07:14 PM

I'm sure you could do it more elegantly wit hawhk or some thing, btu try creating a script file like


#!/bin/sh -x

echo "" > outfile

while read song
do a=$(($a+1));
if [ $a -le 9 ]; then
  echo "mv 00"$a".mp3" $song".mp3" >> outfile
else if [ $a -le 99 ]; then
      echo "mv 0"$a".mp3" $song".mp3" >> outfile
    else echo "mv "$a".mp3" $song".mp3" >> outfile
done < "myfile"
chmod u+x outfile

This will take a list of names like you gave above in myfile and rename via an intermediate script (outfile).

Telemachos 09-25-2008 08:47 PM

This could be far better. At the moment, I'm assuming that the number of files is the same as the number of names and also that the order of items in your text file correctly matches the order of mp3 files. Both are bad assumptions. But this may give some ideas for you or others to improve:

use strict;
use warnings;

my @names;
my @files = <*.mp3>;

while( <> )
  push @names, $_;

chomp( @names );

my $i = 0;

while( $i < scalar @names )
  my $title = $names[$i] . '.mp3';
  rename $files[$i], $title;

When you run it, give the name of the text file as a command line argument, eg. if you call the script "fix", do perl fix textfile

ghostdog74 09-26-2008 12:15 AM

if you have Python

#!/usr/bin/env python
import os,glob
data=[i.strip() for i in data]
for n,files in enumerate(sorted(FILES)):
    print "rename %s to %s.mp3 "%( files, data[n])

litw 09-26-2008 12:41 AM

use bash

[ $# -lt 2 ] && { echo "Usage: script mp3dir namelist"; exit; }

for f in $1/*; do
name=$(sed -n "${n}p" $2)
mv $f ${f/[0-9]*./${name}.}

All times are GMT -5. The time now is 01:27 AM.