[Bash] Move Directories and Modify Path in Files
Question
In the code/ directory, there are sub-directories like the following:
bash/bash-big5-to-utf8/bash-redundant-file/bash-wget/
I want to move the bash-*/ sub-directories to bash/ sub-directory and remove the bash- prefix in the name of bash-*/ sub-directories. The result will be
bash/big5-to-utf8/bash/redundant-file/bash/wget/
And also modify the string bash-*/ to bash/*/ via sed in the reStructuredText files in articles directory.
Answer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/bin/bash langPrefix=bash- targetDir=../../ rstdir=../../../articles/ for srcdir in $(ls -d ${targetDir}*/) do dirprefix=$(dirname ${srcdir}) # remove extension rmext=${srcdir%%/} # remove prefix rawdir=${rmext##*/} if [[ ${rawdir} == ${langPrefix}* ]]; then newdir=${rawdir##${langPrefix}} dstdir=${dirprefix}/${langPrefix%%-}/${newdir} mv ${srcdir} ${dstdir} find ${rstdir} -type f -name "*.rst" | xargs sed -i "s/${rawdir}/${rawdir/-/\\\/}/g" fi done |
References:
[2] |
bash basename - DuckDuckGo search linux - Extract File Basename Without Path and Extension in Bash - Stack Overflow |
[5] |
[6] | bash test if directory exists - Google search bash test if directory exists - DuckDuckGo search bash test if directory exists - Bing search bash test if directory exists - Yahoo search |