-10

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 
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 2
    This sounds suspiciously like homework... What is your specific question? – sam Mar 22 '16 at 04:45
  • i want to rename the directories and sub-directories. for ex: supersectiondir-->subsectiondir-->lowersubsectiondir it should become: superchapterdir-->subchapterdir-->lowersubchapterdir – jayinee desai Mar 22 '16 at 04:56
  • (shopt -s nullglob && _() { for P in "$1"*/; do Q="${P//section/chapter}"; mv -- "$P" "$Q"; _ "$Q"; done } && _ ./)

    Try this this will solve the problem

    – Piyush Jain Mar 22 '16 at 10:04
  • thank you for the help :) this worked #!/usr/bin/env bash for file in find -name '*section*' do mv $file ${file//section/chapter} done – jayinee desai Mar 23 '16 at 04:12

2 Answers2

1

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.
Wildcard
  • 36,499
-1

You can use the rename tool. An example of this use case is:

rename section chapter *

Or in your case, you would probably pipe the output of find.

find . -type d -print | xargs rename section chapter
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232