Renaming files in mass
2 Attachment(s)
Hello all,
I recently downloaded a few .mp3 files from a website. They are all named non-descriptive names and I want to change them all at once. I KNOW that I can do this if I could some how make two arrays array1 = old names and array2 = new names. Then issue the mv command with something like: Quote:
I almost forgot, the file named almost.txt is my script. The other file is self explanatory... |
You don't need arrays at all. Try this:
Code:
#!/bin/bash It will display the commands it will execute. If they look ok, then remove the '#' from the line with the actual mv command (in red). As with any script/command that will move/delete files, I suggest running it on a small portion to test before letting it loose on your whole data set. EDIT: I should make mention: the while-read command assumes that the old_filename in the data file does not include a space. From what I saw in your data file, that was not the case. Be that as it may, I felt I should point that out. EDIT2: Removed the sed command (not necessary), and moved the input file to the "end" of the while loop. |
Not to sound rude, but it's generally a good idea to run a search on your topic before posting, so you can read some of the hundreds of other threads that have already been posted on the bulk renaming of files. (If you have done this, please accept my apologies in advance.)
One option you might be interested in is qmv, part of the renameutils package. It opens up a list of filenames in the text editor of your choice, lets you modify them, then renames the files when you exit. But to answer your exact query, to use arrays, you must load the two arrays so that they have matching index numbers. Then you loop through those index numbers one-by-one, renaming each in turn. Here's one way to do it, with a safer technique for parsing the values than Dark Helmet's. Code:
shopt -s extglob # We need extended globbing in order to parse the variable no. of spaces between the two fields. As DH showed though, it's not actually necessary to use arrays here. I included them above mostly in order to demonstrate the technique. |
The first answer is compact enough.
You should do a listing of original file names: Code:
ls -1 Code:
ls -1 > list Well, I just noticed your sample files. The one that contain the files names is okay enough to go: Code:
Code:
while read old_name new_name; do echo "mv $old_name '$new_name'"; done < songs.txt > script.sh Code:
[demo@localhost Bash]$ /bin/bash script.sh Well, you can change the "echo" statement from the command so that the files are renamed instantly. Here it is: Code:
do eval $(echo "mv $old_name '$new_name'"); Code:
|
By the way, from where did you download those music files? ;)
|
Thank you
1 Attachment(s)
Thank you Dark_Helmet! I ended up using the output from the echo command as the basis of a new script. I know it was not the way that you described but that is just the way I work sometimes...
As for DevUnix, sorry man or lady. I found a Chinese lesson website that had single click mp3 link downloads. I decided to view the source file and sure enough it had the full http URLs for the files. I decided that it would be faster to edit the source file in Linux, remove all of the crap and just have the URLs left. While I was playing with the script given to me by Dark_Helmet I accidentally deleted all of my files in the HOME dir. Buh-bye file with pure http URLs. As a constellation prize I have attached the original source file. Enjoy! |
Here is the list of the songs that can be downloaded:
Code:
http://s3.amazonaws.com/chinesepod.com/0001/fe245fd3e266ff2726113b9327bd0aee7654ee67/mp3/chinesepod_A0001pb.mp3 Code:
chinesepod_A0001pb.mp3 Good Morning You can use wget in a loop to download them all without manually downloading them one by one. Code:
By the way, I am a guy. |
Meh
I just used the file with the http URLs and used
Code:
wget -i FILENAME |
Quote:
|
All times are GMT -5. The time now is 07:01 AM. |