LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2010, 10:59 PM   #1
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Rep: Reputation: 0
Renaming multiple files from filnames within a text file


Here is the situation. I have some random files in a folder. I want to rename all of the files in a batch process. I have a text file that contains the CURRENTname of all the files in the folder, as well as a text file with all of the NEWname of files in the folder. I want to replace CURRENTnames with NEWnames.

For example, here are the names of the files in the folder:
1.mp4
2.mp4
3.mp4

I have a text file with the CURRENTname of all the files in the folder:
1.mp4
2.mp4
3.mp4

I have a text file with the proper NEWname of the file:
a.mp4
b.mp4
c.mp4

I want to rename CURRENTname with NEWname in the folder. So when I go to the folder the NEWname of the files are:
a.mp4
b.mp4
c.mp4

Does anybody know how to accomplish this. I tried to use a nested loop but couldn't figure out how to do it.

Last edited by lt1776; 02-21-2010 at 11:01 PM. Reason: clarity
 
Old 02-21-2010, 11:23 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
How many files are you talking about. Loading each file into a pair of array variables and using the values of each in a single loop will do the trick.

You can read a file into a variable like this:
orignames=($(cat names.txt))
propnames=($(cat propernames.txt))

You can use ${#orignames[*]} to get the number of entries in the arrays. Use a single loop where you reference both variables with the same index.

Last edited by jschiwal; 02-21-2010 at 11:31 PM.
 
Old 02-22-2010, 08:17 AM   #3
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Ok, can you give me an example of the loop. I am not sure how to create the loop formula. I have never done a pair array. Thanks.
 
Old 02-22-2010, 08:24 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Hi,

So you've shown us your need! What's your deed?

Smells like homework to me.

Just a few links to aid you, the two in red will provide some useful information but you will have to do the work;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Utimate Linux Newbie Guide
LinuxSelfHelp
Getting Started with Linux
Advanced Bash-Scripting Guide
Linux Home Networking


The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 02-22-2010, 01:20 PM   #5
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
lol - whatever. I work full time in finance and have never taken a programming class in my life. I am in the works to make a script that downloads youtube videos in mass. I need to find out how to rename the file w/ the proper name.

For some reason, when you use youtube-dl the option to use the friendly name as the name of the file is broken and doesn't work. So when i download the video using youtube-dl the name of the file is the URL ID. Something like "CndjgDnwo.mp4"

I can make two text files, one with the name of the file and the other with the correct name of the file.

Thanks for the links though. I will check them out.
 
Old 02-22-2010, 01:40 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
A very rough but (I think) effective solution can be: use the paste command to put the file names side by side, add "mv" at the beginning of each line using sed, save the output to a file (script), carefully check it and run. Easier done than said:
Code:
paste -d' ' CURRENTname.txt NEWname.txt | sed 's/^/mv /' > test.sh
# Check the content of test.sh and if you're satisfied...
bash test.sh
Anyway, trying to find out how to solve the issue by following the advices from jschiwal and onebuck, could be a very interesting and fruitful experience. As to say... learn the art and set it aside.
 
Old 02-22-2010, 01:56 PM   #7
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Nice- the paste command works perfect. Thanks
 
Old 02-22-2010, 02:04 PM   #8
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Can someone explain how do implement the solution provided by jschiwal

Quote:
You can read a file into a variable like this:
orignames=($(cat names.txt))
propnames=($(cat propernames.txt))

You can use ${#orignames[*]} to get the number of entries in the arrays. Use a single loop where you reference both variables with the same index.
 
Old 02-22-2010, 06:43 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Start here: http://tldp.org/LDP/abs/html/arrays.html
 
Old 02-23-2010, 01:54 PM   #10
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Thumbs up

Ok, I am getting an error. Here is the sample problem I am working with:
List of files:
a.txt
b.txt
c.txt

File with CURRENT file names:
old

Contents of old:
a.txt
b.txt
c.txt

File with NEW file names:
new

Contents of new:
abby.txt
barry.txt
cindy.txt

My script is as follows:
old=$(cat old)
new=$(cat new)

#$old now contains 'a.txt b.txt c.txt'
#$new now contains 'abby.txt barry.txt cindy.txt'

for file in *.txt; do mv $old $new; done

=======================================
After the I run the commands I get an error:
mv: target `cindy.txt' is not a directory
mv: target `cindy.txt' is not a directory
mv: target `cindy.txt' is not a directory


Does anybody know what I am doing wrong?
 
Old 02-23-2010, 06:14 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
He's prompting you to use arrays; see my link above.
 
Old 02-23-2010, 06:51 PM   #12
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
You are trying to rename all the files at one.

Code:
while read line; do
    mv $line;
done < <(paste curr.txt new.txt)
You need to feed mv with 1 line at a time.
 
Old 02-23-2010, 11:17 PM   #13
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
He's prompting you to use arrays; see my link above.
I read over the link and did some of the exercises. I still don't understand how I can use a pair array to rename all the files. How would you use the array formula to do that?

I already tried:
for file in *.txt; mv $old $new; done

However, I can't get that to work. I am just a beginner and am just learning as I go. Any help on how to use a pair array to rename files would be helpful.
 
Old 02-24-2010, 05:52 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The problem with your solution is what whizje already pointed out, two posts above. The loop
Code:
for file in *.txt
do
  mv $old $new
done
is dummy, since it doesn't use the loop variable "file" in its body. Furthermore upon the shell substitution of the two variables "old" and "new" with their values, you end-up with a command line like this:
Code:
mv a.txt b.txt c.txt abby.txt barry.txt cindy.txt
that is not as expected and wrong, since you can move multiple files at once, but only when the last argument is the name of a directory (the destination).

Regarding the array solution, jschiwal already provided it in post #2. You have just to translate its words into shell code. Here is a working example of what you can obtain..

1) first assign the content of the two files to two different arrays (as shown above)
Code:
orignames=( $(cat old) )
propnames=( $(cat new) )
here we use command substitution $(command) to retrieve the content of the files, and array assignment using parentheses.

2) do a loop over the array indexes, but take in mind that they are zero-based, that is the first index is 0, the second one is 1 and so on...
Code:
for ((i=0; i < ${#orignames[*]} ; i++))
do
  echo mv ${orignames[i]} ${propnames[i]}
done
here I used a C-style for loop (which is permitted in bash), but you can try a different syntax and even a while loop, if you prefer. The syntax ${#orignames[*]} returns the length of the array, as you already should know. The echo statement is just for testing purposes (it prints out the command without actually executing it).

Hope this helps. Keep up your learning process from the basics and you will see that at some point you will spare a lot of time to accomplish both simple and complex tasks.
 
1 members found this post helpful.
Old 02-24-2010, 01:27 PM   #15
lt1776
LQ Newbie
 
Registered: Feb 2010
Posts: 13

Original Poster
Rep: Reputation: 0
I appreciate the tutorial. I am beginning to understand how it works not. Works perfect. Thanks colucix

Last edited by lt1776; 02-24-2010 at 01:32 PM.
 
  


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
Renaming multiple files Changes Linux - Newbie 1 02-01-2009 10:58 PM
renaming multiple files yanchina12 Linux - Newbie 5 12-21-2007 08:56 AM
Combine multiple one column text file into one text file with multiple colum khairilthegreat Linux - Newbie 7 11-23-2007 02:31 PM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-08-2007 12:24 AM
Renaming multiple files ajcns Linux - Newbie 2 01-20-2007 12:04 AM


All times are GMT -5. The time now is 12:06 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration