-1

Here is my directory structure i want to rename the .html files only under the named directory rev by its previous two directory names .How can i do it using command?

    prj3
    |-- blk1
    |   |-- blk1.config
    |   |-- blk1.html
    |   |-- checklist13
    |   |   |-- checklist13.config
    |   |   |-- checklist13.html
    |   |   |-- checklist13.html~
    |   |   `-- rev2
    |   |       |-- rev2.config
    |   |       `-- rev2.html
    |   |-- checklist14
    |   |   |-- checklist14.config
    |   |   |-- checklist14.html
    |   |   |-- checklist14.html~
    |   |   `-- rev5
    |   |       |-- rev5.config
    |   |       |-- rev5.html
    |   |       `-- rev5.html~

Expected output:

    prj3
    |-- blk1
    |   |-- blk1.config
    |   |-- blk1.html
    |   |-- checklist13
    |   |   |-- checklist13.config
    |   |   |-- checklist13.html
    |   |   |-- checklist13.html~
    |   |   `-- rev2
    |   |       |-- rev2.config
    |   |       `--checklist_13_rev2.html
    |   |-- checklist14
    |   |   |-- checklist14.config
    |   |   |-- checklist14.html
    |   |   |-- checklist14.html~
    |   |   `-- rev5
    |   |       |-- rev5.config
    |   |       |-- checklist_14_rev5.html
    |   |       `-- rev5.html~

The same expected output should happend for all rev*.html files only inside the rev* folders.

2
  • ⑴ You want to rename the .html files … by its previous two directory names, but the .html file under checklist13 gets renamed to checklist_13_…. What are the rules for changing checklist13 to checklist_13? ⑵ Will the .html file always have the same name as the directory (e.g., rev2/rev2.html), or can there be a rev2/foo.html? ⑶ If the .html file under rev2 is rev2/foo.html, should it be renamed to checklist_13_rev2.html or checklist_13_foo.html? ⑷ Can there be subdirectories in the rev… directories?  Can there be .html files in them?  How should they be handled? Jun 28, 2017 at 4:52
  • yes underscorE should be added between two directory names and the .html file will always have the same name as directory (i.e rev2/rev2.html).so now the file should be renamed as checklist13_rev2.html.There wont be any sub directories under rev_.Only html files lie under rev* folders. @Scott
    – nasa aneer
    Jun 28, 2017 at 5:00

1 Answer 1

0

Here's a one-liner, using a combination of find, basename, and dirname:

find . -regex '.*/rev[0-9]+/rev[0-9]+.html' -exec sh -c 'mv -iv {} $(dirname {})/$(basename $(dirname $(dirname {})))_$(basename $(dirname {})).html' \;

It basically uses find regex to get paths ending in rev#/rev#.html, and constructs the new name using combinations dirname and basename to extract the names of the parent directory and parent directory's parent directory.

2
  • Please use mv -i for mass renames like this, to prevent files being silently overwritten if something goes wrong. Jun 28, 2017 at 5:54
  • @GordonDavisson good point, edited Jun 28, 2017 at 5:54

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