LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-25-2008, 04:25 PM   #1
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Rep: Reputation: 31
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.
 
Old 09-25-2008, 04:29 PM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
What's the format of the text file - how about some example lines?
 
Old 09-25-2008, 04:48 PM   #3
darthaxul
Member
 
Registered: Aug 2008
Distribution: Gentoo; Funtoo
Posts: 201

Rep: Reputation: 18
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
 
Old 09-25-2008, 06:08 PM   #4
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Original Poster
Rep: Reputation: 31
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?
 
Old 09-25-2008, 06:13 PM   #5
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Sample lines?
 
Old 09-25-2008, 06:15 PM   #6
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Original Poster
Rep: Reputation: 31
The file will look like this:

Wonders
Magic
Lame

I hope it helps.
 
Old 09-25-2008, 07:14 PM   #7
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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).
 
Old 09-25-2008, 08:47 PM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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
 
Old 09-26-2008, 12:15 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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")
 
Old 09-26-2008, 12:41 AM   #10
litw
LQ Newbie
 
Registered: May 2005
Posts: 1

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
Rename files by stripping text from either desired string? varney Linux - General 2 02-21-2008 10:04 PM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-07-2007 11:24 PM
deleting 100 rows in text file (1-100) via terminal itz2000 Linux - Newbie 5 01-17-2006 11:19 PM
set file permissions for 100 directories & its files cevjr Linux - Security 6 01-06-2005 10:23 AM


All times are GMT -5. The time now is 07:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration