LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Rename 100+ files from a text file (http://www.linuxquestions.org/questions/linux-software-2/rename-100-files-from-a-text-file-672376/)

AGazzaz 09-25-2008 05:25 PM

Rename 100+ files from a text file
 
Hello,
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?!

Thanks.

billymayday 09-25-2008 05:29 PM

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

darthaxul 09-25-2008 05:48 PM

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

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

AGazzaz 09-25-2008 07: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 07:13 PM

Sample lines?

AGazzaz 09-25-2008 07:15 PM

The file will look like this:

Wonders
Magic
Lame

I hope it helps.

billymayday 09-25-2008 08:14 PM

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

Code:

#!/bin/sh -x
#

echo "" > outfile

a=0
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
    fi
fi
done < "myfile"
chmod u+x outfile
./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 09: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:
Code:

#!/usr/bin/perl
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;
  $i++;
}

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 01:15 AM

if you have Python
Code:

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


litw 09-26-2008 01:41 AM

use bash
 
#!/bin/bash

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

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


All times are GMT -5. The time now is 03:55 AM.