Rename Filename with Parent Folder Name – Scripting Guide

filenamesfilesrenamescripting

I have a bunch of files each in their own subdirectory. I need to write a script to rename each file to have the same name as its directory: For example, given:

./1111/1234.pdf
./2222/2345.pdf
./1234/3214.pdf

I need them to be renamed to:

./1111/1111.pdf
./2222/2222.pdf
./1234/1234.pdf

How can I do that?

Best Answer

Assuming your shell is zsh, put autoload -U zmv in your ~/.zshrc, and run

zmv '(*)/*.pdf' '$1/$1.pdf'
Related Question