How can one recursively rename directories without sed or rename?

I am trying to rename a few directories that contain "Fever" to contain "Malaria" instead. The instruction is to do it without sed or rename. So far, my errors include mostly lines like mv: cannot stat ‘retest\nretest/Section-01\nretest/Section-02\nretest/Section-03\nretest/Section-04’: No such file or directory.

The best my code have done is rename directories in the first level.

Here's my directory structure:

Fever-A/Malaria-A-A

Fever-B/Fever-B-A

Fever-B/Fever-B-B

Fever-C/Malaria-A

Fever-C/Fever-C-A

Fever-C/Fever-C-B

Fever-C/Fever-C-C-C

Fever-D/Malaria-A

Fever-D/Malaria-B

The code I have so far is :

#!/bin/bash  # Access directory #cd $1  # Find all subdirectories in $1 and load up array all=($(find $1 -type d)) #echo ${all[@]}  # Loop through directories above for dir in ${all[@]} do     # echo "$dir"     cd $dir     # List files with "Section" in name     subdir=(:"Section*")      # A second loop for directories in each dir with "Section*"     for item in ${subdir[@]}     do             echo $item             echo "--------------------"              # Rename operation             mv $item ${item//Section/Chapter}     done     cd $1 done 

Another approach I've considered is using a function like so, but it's not working either: #!/bin/bash

rename(){     old_names=($(find $1 -maxdepth 1 -type d))      for item in ${old_names[@]}     do             if [[ $item = *Section* ]]; then                     new_name=${item//Section/Chapter}                     mv $item $new_name             elif [[ $1 != $item ]]; then                     rename $item             fi              rename $1     done }  rename $1 

Replay

Category: bash Time: 2016-07-28 Views: 3

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.209 (s). 12 q(s)