Compare two files line by line in bash

Hello All!
Thanks for taking time out and helping.

My issue is, I have two files that have file names in it. Now, i need to go through each line of both the files and when the file names are different, i need to rename the file. Below is the example:

File1</

fil1ename1.txt
filename2,txt
Afilename3.txt

/>

File2</

filename1.txt
filename2.txt
filename3.txt

/>

Expected outcome:
rename filename3.txt in file2 to be Afilename3.txt.

I have tried many ways but its just going in infinite loop.

Any help would be appreciated.

Thank you!

Try something like this:

while 
  read newfile &&
  read oldfile <&3
do
  if [ "$oldfile" != "$newfile" ]; then
    if [ -f "$oldfile" ]; then
      echo mv -- "$oldfile" "$newfile"
    fi
  fi
done < file1 3<file2

Remove echo when you are done modifying and testing the code.

Hi.

We can push the (implicit) loop down into a standard command and feed it with pairs of strings from the two files. So the core of this solution is paste -> xargs , the remainder of the demonstration script is setup and reporting:

#!/usr/bin/env bash

# @(#) s3       Demonstrate rename of files, xargs, mv.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C paste xargs mv

pl " Input data file data?"
head data?

pl " Situation before rename:"
ls a* b*

pl " Results:"
paste data1 data2 | xargs -n 2 mv

pl " Situation after rename, expecting a2 b1 b2 b3:"
ls a* b*

exit 0

producing:

$ ./s3

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
paste (GNU coreutils) 8.23
xargs (GNU findutils) 4.4.2
mv (GNU coreutils) 8.23

-----
 Input data file data?
==> data1 <==
a1
b2
a3
a4

==> data2 <==
b1
b2
b3
b4

-----
 Situation before rename:
a1  a2  a3  b2

-----
 Results:
mv: 'b2' and 'b2' are the same file
mv: cannot stat 'a4': No such file or directory

-----
 Situation after rename, expecting a2 b1 b2 b3:
a2  b1  b2  b3

Best wishes ... cheers, drl