LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-08-2009, 01:19 PM   #1
mashcaster
Member
 
Registered: Dec 2008
Posts: 67

Rep: Reputation: 15
Renaming files


If I have some files

1.txt
2.txt
3.txt
and so on

and I have another text file called "names.txt" which has the text

filenameone.txt
filenametwo.txt
filenamethree.txt
and so on

How do I rename the files based on the text in the "names.txt" file?
 
Old 05-08-2009, 01:49 PM   #2
dracofhc
Member
 
Registered: Dec 2005
Location: Kalamazoo, MI, US
Distribution: Gentoo
Posts: 44

Rep: Reputation: 16
I would say a shell script would be your best bet. It would open your file then start a for() loop that reads one line from the file (assuming your new file names are on separate lines), then renames the file. Each iteration of the for() loop will grab the next file name from your names.txt file and since the index is incremented it will operate on the next file in line. Sorry this is a little vague, I don't have the time to write the script myself. I hope this points you in the right direction, though.
 
Old 05-08-2009, 01:52 PM   #3
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

how do you assign the filenames in the directory to the according filenames in names.txt? Simply by the position in the "ls" output?

In this case you can do it like this:
1. create a file with the filename's list:
Code:
ls >/tmp/list.txt
2. merge the two files:
Code:
paste /tmp/list.txt /tmp/names.txt
You get the following 2 column output:
Code:
jan@jack:~/tmp> paste -d " " /tmp/list.txt /tmp/names.txt
1.txt one.txt
2.txt two.txt
3.txt three.txt
3. Now you can use this output to rename the files:
Code:
paste -d " " /tmp/list.txt /tmp/names.txt | while read line; do
  mv $line
done
The filenames may not contain spaces or tabs!

Jan
 
Old 05-08-2009, 03:14 PM   #4
mk27
Member
 
Registered: Sep 2008
Distribution: fedora, gentoo, ubuntu
Posts: 148

Rep: Reputation: 23
Quote:
Originally Posted by jan61 View Post
3. Now you can use this output to rename the files:
That is so freakin clever I had to thank you just for the trip
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming files Viablade Linux - Newbie 11 10-10-2008 01:54 PM
Renaming files leupi Linux - General 5 09-16-2008 03:22 AM
help renaming files balistic Linux - Newbie 4 07-29-2007 08:04 AM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 10:14 AM
Renaming files TomalakBORG Linux - Newbie 2 12-05-2005 04:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration