I am attempting to rename a directory full of files based off the contents of two text files. OLD (which has the full names of the current files) and NEW (which has the names i wish to rename them) both in the same order.

How would I go about renaming the files from OLD to NEW?

Each file contains \n separated name

OLD example line: Blah Blah Blah Campaign 2 Episode 1.mp4

NEW example line: Campaign 2 Episode 1.mp4

  • 1
    Could you give more information about what exactly is written on "Bla bla bla"? Is it always the same or does it change? – Rafael Muynarsk Nov 28 '19 at 2:58
  • @RafaelMuynarsk It changes.Imagine Episode titles from a TV show or the like – Hirschy Nov 28 '19 at 3:03
  • ❯ cat /run/media/hirschy/U32\ Shadow/Vids/Critical\ Role/Campaign\ 2/OLD "A Dangerous Chase _ Critical Role _ Campaign 2, Episode 64-3yfLht7w7Yk.mp4" "A Favor in Kind _ Critical Role _ Campaign 2, Episode 16-lT-wFK4SgJc.mp4" "A Game of Names _ Critical Role _ Campaign 2, Episode 49-TBw3AGBI6s4.mp4" "Agreements _ Critical Role _ Campaign 2, Episode 61-Ck_yuplT8O8.mp4" "A Hole In the Plan _ Critical Role _ Campaign 2, Episode 42-758doI6FhA0.mp4" – Hirschy Nov 28 '19 at 4:00
  • 2
    Please edit your question instead of adding information in comments – steeldriver Nov 28 '19 at 4:42
  • Your comment with the data indicates that the filenames have literal double quotes in them. This is in contrast with the example given in the question. Could you please update the text of the question so that it reflects the actual contents of the input files, and then delete your comment (which confuses matters). – Kusalananda Nov 28 '19 at 8:43

A very zsh-specific one:

old=(${(f)"$(<OLD)"}) new=(${(f)"$(<NEW)"})
for o n (${old:^new}) echo mv -i -- $o $n

Remove echo when happy.

Or a portable one:

while IFS= read -r old <&3 && IFS= read -r new <&4; do
  echo mv -i -- "$old" "$new"
done 3< OLD 4< NEW

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.