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. |
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. |
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.
|
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 :hattip: The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links! |
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. |
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 |
Nice- the paste command works perfect. Thanks
|
Can someone explain how do implement the solution provided by jschiwal
Quote:
|
Start here: http://tldp.org/LDP/abs/html/arrays.html
|
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? |
He's prompting you to use arrays; see my link above.
|
You are trying to rename all the files at one.
Code:
while read line; do |
Quote:
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. |
The problem with your solution is what whizje already pointed out, two posts above. The loop
Code:
for file in *.txt Code:
mv a.txt b.txt c.txt abby.txt barry.txt cindy.txt 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) ) 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++)) 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. |
I appreciate the tutorial. I am beginning to understand how it works not. Works perfect. Thanks colucix
|
All times are GMT -5. The time now is 07:34 PM. |