2

I have lots of index.html files in unique named folders (one per folder). I wish to rename each file with that of it's parent folder name.

From:

/folder1/index.html
/folder2/index.html
/folder3/index.html
/folder4/index.html
/folder5/index.html

To:

/folder1/folder1.html
/folder2/folder2.html
/folder3/folder3.html
/folder4/folder4.html
/folder5/folder5.html

Attempt 1

I have tried rename -vn 's:(/[^/]*)/[^/]*$:$1$1.html:' folder/*/*.html which returns with:

Using expression: sub { use feature ':5.18'; s:(/[^/]*)/[^/]*$:$1$1.html: }
'folder/*/*.html' unchanged

I don't really understand what's happening to be able to fix it.

Attempt 2

Next, I tried rename -n -v '...' folder/*/*.html, which returned with:

Using expression: sub { use feature ':5.18'; ... }
Unimplemented at (eval 2) line 1.

I am using OSX with rename installed.

2 Answers 2

1

That works for me:

rename -vn 's/([^\/]+)\/index/$1\/$1/' folder*/*.html

Prints:

folder1/index.html renamed as folder1/folder1.html
folder2/index.html renamed as folder2/folder2.html
folder3/index.html renamed as folder3/folder3.html
folder4/index.html renamed as folder4/folder4.html
folder5/index.html renamed as folder5/folder5.html

Another solution with sed:

ls -1 folder*/*.html | sed 's/\([^\/]\+\)\/index.html/mv "\0" "\1\/\1.html"/'

Prints:

mv "folder1/index.html" "folder1/folder1.html"
mv "folder2/index.html" "folder2/folder2.html"
mv "folder3/index.html" "folder3/folder3.html"
mv "folder4/index.html" "folder4/folder4.html"
mv "folder5/index.html" "folder5/folder5.html"

If that prints the correct commands just pipe it to a bash. But first make sure that the printed commands would work before executing it:

ls -1 folder*/*.html | sed 's/\([^\/]\+\)\/index.html/mv "\0" "\1\/\1.html"/' | bash
4
  • I still get Using expression: sub { use feature ':5.18'; s/([^\/]+)\/index/$1\/$1/ } 'folder*/*.html' unchanged. Not sure why. I have even reinstalled rename. May 29, 2015 at 10:08
  • @evolutionxbox Do you have perl installed? Then you could just use that rename utillity: tips.webdesign10.com/files/rename.pl.txt. I don't know your homebrew rename utillity posted in your question.
    – chaos
    May 29, 2015 at 10:11
  • I don't have perl installed. I checked the code for homebrew rename and they are very similar. Without -v I don't get any output (which is obvious to me now, even with -n enabled). May 29, 2015 at 10:17
  • @evolutionxbox see my edit, I added a 2nd solution
    – chaos
    May 29, 2015 at 10:32
0

you can try something like:

for in in `find . -name index.html -type f`
do
bname=$(dirname $i|sed 's:/::g')
mv $i $(dirname $i)/${bname}.html
done

P.S. this will work only with one level of directories and files with extension .html

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.