3

I have a tab-delimited list of hundreds of names in the following format

old_name    new_name
apple       orange
yellow      blue

All of my files have unique names and end with *.txt extension and these are in the same directory. I want to write a script that will rename the files by reading my list. So apple.txt should be renamed as orange.txt. I have searched around but I couldn't find a quick way to do this.I can change one file at a time with 'rename' or using perl "perl -p -i -e ’s///g’ *.txt", and few files with sed, but I don't know how I can use my list as input and write a shell script to make the changes for all files in a directory. I don't want to write hundreds of rename command for all files in a shell script. Any suggestions will be most welcome!

Tom O'Connor
  • 27,480
  • 10
  • 73
  • 148
psaima
  • 31
  • 2
  • The `shell` tag tells us nothing. What operating system. Are those files all in the same directory? What scripting language do you want this in? What have you tried so far? – John Gardeniers Oct 11 '12 at 08:38
  • Thanks John for your suggestions, I have edited my question to improve it. – psaima Oct 11 '12 at 08:56

1 Answers1

2

This should work as it's fairly standard bash

#!/bin/bash
while read line
    do
        set -- $line
        echo renaming "$1.txt to $2".txt
        mv "$1".txt "$2.txt"

    done <input.file
user9517
  • 115,471
  • 20
  • 215
  • 297