[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

run.sh | repository | view raw
 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:

[1]

bash list directories - Google search

bash list directories - DuckDuckGo search

bash list directories - Bing search

bash list directories - Yahoo search

bash list directories - Baidu search

bash list directories - Yandex search

directory - Listing only directories using ls in bash: An examination - Stack Overflow

[2]

bash basename - Google search

bash basename - DuckDuckGo search

bash basename - Bing search

bash basename - Yahoo search

bash basename - Baidu search

bash basename - Yandex search

linux - Extract File Basename Without Path and Extension in Bash - Stack Overflow

[3]

bash string starts with - Google search

bash string starts with - DuckDuckGo search

bash string starts with - Bing search

bash string starts with - Yahoo search

bash string starts with - Baidu search

bash string starts with - Yandex search

In bash, how can I check if a string begins with some value? - Stack Overflow

[4]

bash remove prefix - Google search

bash remove prefix - DuckDuckGo search

bash remove prefix - Bing search

bash remove prefix - Yahoo search

bash remove prefix - Baidu search

bash remove prefix - Yandex search

bash - remove a fixed prefix/suffix from a string - Stack Overflow

[5]

bash dirname - Google search

bash dirname - DuckDuckGo search

bash dirname - Bing search

bash dirname - Yahoo search

bash dirname - Baidu search

bash dirname - Yandex search

[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

bash test if directory exists - Baidu search

bash test if directory exists - Yandex search

[7]

bash string replace character - Google search

bash string replace character - DuckDuckGo search

bash string replace character - Bing search

bash string replace character - Yahoo search

bash string replace character - Baidu search

bash string replace character - Yandex search

linux - Replacing some chars with another - Stack Overflow

[8]

sed process multiple files - Google search

sed process multiple files - DuckDuckGo search

sed process multiple files - Bing search

sed process multiple files - Yahoo search

sed process multiple files - Baidu search

sed process multiple files - Yandex search

sed - Change multiple files - Stack Overflow

[9]

sed single quote - Google search

sed single quote - DuckDuckGo search

sed single quote - Bing search

sed single quote - Yahoo search

sed single quote - Baidu search

sed single quote - Yandex search

escaping - How to escape single quote in sed? - Stack Overflow