I have a list like the following, in a file called lista.txt:
mickey
donald
daffy
bugs
I have a folder containing many files: filename1, filename2, ... filenameN.
I want to iterate through those files to achieve:
filename1 => mickey
filename2开发者_StackOverflow中文版 => donald ...
Can you provide me working sample code for this task?
It's not my style to do your work for you. I'd rather you post what you've already tried, so I can help you debug it, but this problem is so easy, I'm going to bite anyway.
x=1; for y in $(cat lista.txt); do mv $y filename$x; let x=$x+1; done
Using bash arrays:
files=( * )
i=0
while read -r new_name; do
mv "${files[$i]}" "$new_name"
(( i++ ))
done < lista.txt
let "count=1"
for newname in $(cat lista.txt); do
mv "filename$count" "$newname"
let "count++"
done
I used double redirection with new file descriptors to rename all flac files of a directory according to a given "filelist.txt" this way:
while read -u 9 filename ; do
read -u 8 newname
echo mv "$filename" "$newname"
done 9<<<"$(ls -1 *flac)" 8< filelist.txt
Each .flac file name on the directory goes through file descriptor 9 to the variable "filename" while in the same iteration, each line of the filelist.txt goes through file descriptor 8 to the variable "newname".
Yes, one of the new file descriptors could have been avoided and simply use the default standard input (deleting "-u 9" and "9"), but I got used to not use stdin for those; that way you can include a read statement inside the loop for interactivity or control, and it won't be filled by the redirected data.
The echo is for "test-only" and should be deleted after approving the operation.
You can use small linux app name krename, with javascript plugin turned on. It is a free tool with very powerful renaming capabilities.
- Install Krename and open it
- Add files
Go to plugins tab and add your javascript function to function definitions section, something like this:
var files = [ "Mickey", "Donald", "Duffy" ]; function rename(){ // krename_index is one of many special variables which can be added via ui return files[krename_index]; }
This is a simple script that gets the job done, but it can be as complex as you like.
Go to filename tab and call your function in the template input as shown below:
[js; rename()]
You can prefix the above code with $
to keep the original file name and add to it. You can use "Functions" button to experiment further.
- Preview new names and assign new names by clicking Finish button.
精彩评论