home · contact · privacy
Enhance bookworm setup.
[config] / bookworm / setup_scripts / copy_dirtree.sh
1 #!/bin/sh
2 # Copy files in argument-selected subdirectories of $1 to subdirectories
3 # of $2 (which may be an empty string), e.g. with $1 of "etc_files", $2
4 # of "" and $3 of "all", copy files below etc_files/all such as
5 # etc_files/all/etc/foo/bar to equivalent locations below / such as
6 # /etc/foo/bar. Create directories as necessary. Multiple arguments after
7 # $3 are possible.
8 #
9 # CAUTION: This removes original files at the affected paths.
10 set -e
11 . ./misc.sh
12
13 expect_n_args 3 "(source root, target root, modules)" "$@"
14
15 source_root="$1"
16 target_root="$2"
17 shift 2
18
19 for target_module in "$@"; do
20     mkdir -p "${source_root}/${target_module}"
21     cd "${source_root}/${target_module}"
22     for path in $(find . -type f); do
23         target_path="${target_root}"$(echo "${path}" | cut -c2-)
24         source_path=$(realpath "${path}")
25         dir=$(dirname "${target_path}")
26         mkdir -p "${dir}"
27         cp "${source_path}" "${target_path}"
28     done
29 done