You need to create a small script file to do your rename/move: call it rnmv
, say, and put the following command into it:
f="${1#./}"; echo mv "$f" "${f%%/*}.${f##*.}"
Don't forget to chmod +x rnmv
. Then go to the root directory and call rnmv
from a find
command:
cd {wherever temp2 root is}
find . -name "temp2.txt" -exec rnmv "{}" \;
When you are happy that the correct mv
commands are being created, remove the echo
from rnmv
and your rename/move will be done.
One final task is to remove the empty subdirectories:
find . -name temp2 -exec rmdir "{}" \;
This is safe, as rmdir
will not remove a directory with files in it. Depending on the order of finding the directories, you may need to repeat the command for structures like ABC/temp2/temp2, as the temp2 will not be empty until the second is deleted.
Note that if ABC, say, has more that one temp2.txt file in its subdirectories, each mv
command will overwrite the previous ABC.txt, and only the last found will be left afterwards.