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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-25-2008, 04:25 PM
|
#1
|
Member
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 393
Rep:
|
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.
|
|
|
09-25-2008, 04:29 PM
|
#2
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
What's the format of the text file - how about some example lines?
|
|
|
09-25-2008, 04:48 PM
|
#3
|
Member
Registered: Aug 2008
Distribution: Devuan; Gentoo; FreeBSD
Posts: 240
Rep:
|
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
|
|
|
09-25-2008, 06:08 PM
|
#4
|
Member
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 393
Original Poster
Rep:
|
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?
|
|
|
09-25-2008, 06:13 PM
|
#5
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
Sample lines?
|
|
|
09-25-2008, 06:15 PM
|
#6
|
Member
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 393
Original Poster
Rep:
|
The file will look like this:
Wonders
Magic
Lame
I hope it helps.
|
|
|
09-25-2008, 07:14 PM
|
#7
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
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).
|
|
|
09-25-2008, 08:47 PM
|
#8
|
Member
Registered: May 2007
Distribution: Debian
Posts: 754
Rep:
|
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
|
|
|
09-26-2008, 12:15 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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")
|
|
|
09-26-2008, 12:41 AM
|
#10
|
LQ Newbie
Registered: May 2005
Posts: 1
Rep:
|
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 10:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|