Fabio B.
Fabio B.

Reputation: 9400

Rename a list of file with names from another list

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 => donald ...

Can you provide me working sample code for this task?

Upvotes: 8

Views: 10535

Answers (5)

snnsnn
snnsnn

Reputation: 13630

You can use small linux app name krename, with javascript plugin turned on. It is a free tool with very powerful renaming capabilities.

  1. Install Krename and open it
  2. Add files
  3. 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.

  1. 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.

  1. Preview new names and assign new names by clicking Finish button.

Upvotes: 0

BeGood
BeGood

Reputation: 21

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.

Upvotes: 2

Jonathan Hall
Jonathan Hall

Reputation: 79604

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

Upvotes: 8

glenn jackman
glenn jackman

Reputation: 246837

Using bash arrays:

files=( * )
i=0
while read -r new_name; do
  mv "${files[$i]}" "$new_name"
  (( i++ ))
done < lista.txt

Upvotes: 6

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

let "count=1"

for newname in $(cat lista.txt); do
  mv "filename$count" "$newname"
  let "count++"
done

Upvotes: 3

Related Questions