Build a Renaming Script [closed]

write a script that will check each folder/directory and rename the folder title if it contains the word “section.” The folder should be renamed to use the word “chapter” instead of “section”;Your script should be recursive, executing on each directory, subdirectory, and lower subdirectory, until all child directories have been checked and renamed.

For example:

supersectiondir-->subsectiondir-->lowersubsectiondir
should become:
superchapterdir-->subchapterdir-->lowersubchapterdir

My attempt (from directory above supersectiondir):

find /c/Users/cmd2/supersection -type d -exec sed -i 's/section/chapter/g' {} ; 

$ sh renaming.sh

sed: couldn't edit /c/Users/cmd2/supersection: not a regular file
sed: couldn't edit /c/Users/cmd2/supersection/subsection: not a regular file
sed: couldn't edit /c/Users/cmd2/supersection/subsection/lowersubsection: not a regular file 

Answer

The rename tool is extremely non-portable; there is almost nothing in common between the RHEL/CentOS/Fedora family version of rename and the version found on Ubuntu or Debian.

I wrote an answer giving example usage for both versions of rename a while back.

You haven’t said what OS you’re using, so it’s hard to be very specific—and since this is homework, you should do some work on it yourself so I will not spell out the answer even if you say which OS.

However, a couple of tips:

  • sed operates on the contents of a text file; it does not change the name of a file and certainly not the name of a directory. It cannot be used for that, and that’s why you get errors from the command you wrote.
  • You are on the correct track with your find command.
  • I suggest you look up the -name and -iname operators for find as they may come in handy (you don’t need to try to rename files that don’t match the given pattern).
  • Using -exec with rename (the appropriate version for your OS) is probably the easiest/best solution for this.

Leave a Reply

Your email address will not be published. Required fields are marked *