LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   directory renaming shell script? (https://www.linuxquestions.org/questions/linux-general-1/directory-renaming-shell-script-290223/)

jschiwal 02-22-2005 06:50 AM

Perhaps this might help you

> DIR='ab cd/a b c/de f/g h i'
> echo $(dirname "$DIR")/$(basename "${DIR// /_}")
ab cd/a b c/de f/g_h_i

If you install the 'tac' command, the find listing will be reversed so that the subdirectories occur first.

> find . -type d | tac
./ab cd/de fg/a b c
./ab cd/de fg
./ab cd
.

However, the last two entries may cause a problem because there are no subdirectories. But you need to rename the subdirectory before the main directory, so the answer may to write a recursive routine that transverses down the directories, or testing for a '/' charactor in $DIR after the second character.

> find ./ -type d | cut -b 3- | tac
ab cd/de fg/a b c
ab cd/de fg
ab cd

The cut command gets rid of the initial ./ which might cause problems.

However the problem I have is the spaces causing "${DIR}" to be chopped up in the for loop. Changing IFS to a newline character doesn't help, because everything gets lumps together.

jschiwal@matrix:~/test2> find ./ -type d | cut -b 3- | tac
ab cd/de fg/a b c
ab cd/de fg
ab cd

jschiwal@matrix:~/test2> IFS=\n; for dir in `/usr/bin/find ./ -type d | /usr/bin/cut -b 3- | /usr/bin/tac`; do /bin/echo - "${DIR}" -; done
- fg
ab cd -


All times are GMT -5. The time now is 11:22 PM.