Software-Installation

使用 GNU install 遞歸安裝目錄

  • November 15, 2020

如果我有一個文件樹,比如說/usr/share/appname/,我如何使用 GNUinstall和 mode遞歸地安裝它們644

我假設我首先需要install創建目錄,-d因為目錄權限需要不同(755)。

當然這不是解決方案:

 local dir file
 for dir in "${dirs_with_files[@]}"; do
   for file in "$srcdir/$dir"/*; do
     # install fails if given a directory, so check:
     [[ -f $file ]] && install -Dm644 -t "$dir" "$file"
   done
 done

install遞歸地製作安裝文件沒有神奇的咒語。install在這種情況下可能不是最好的工具:您最好使用cp複製文件和目錄結構,然後chmod修復模式。

我首先創建目錄樹,然後復製文件,如下所示:

# arg1: Source directory
# arg2: Target directory
# returns 1 on error
# example: install_directory dist/data /usr/share/myapp || install_failed
install_directory() {
   [ ! -d "${1}" ] && return 1 # Source doesn't exist or isn't a directory

   # directories
   while IFS= read -r path; do
       [ -d "${2}/${path}" ] || mkdir -m 0755 -p "${2}/${path}" || return 1
   done <<<$(find "${1}" -type d -printf '%P\n')

   # files
   while IFS= read -r path; do
       [ -z "${path}" ] && continue
       install -m 0644 "${1}/${path}" "${2}/${path}" || return 1
   done <<<$(find "${1}" -type f -printf '%P\n')

   # symlinks
   while IFS= read -r path; do
       [ -z "${path}" ] && continue
       cp -fPT "${1}/${path}" "${2}/${path}" || return 1
   done <<<$(find "${1}" -type l -printf '%P\n')
}

引用自:https://unix.stackexchange.com/questions/475242