Skip to content

Instantly share code, notes, and snippets.

@teocci
Created December 2, 2021 11:05
Show Gist options
  • Save teocci/85a46b0477da7ad9a6d5a2b702c9bb14 to your computer and use it in GitHub Desktop.
Save teocci/85a46b0477da7ad9a6d5a2b702c9bb14 to your computer and use it in GitHub Desktop.
bash-tar-without-dir.sh
#!/bin/bash
# mkdir -vp ~/tmp/{web/gui,cpp/bin}
# touch cpp/bin/gcs.out
# touch web/gui/gui-{a,b,c,d}.js
# touch web/gui/other-{a,b}.js
# touch web/conf-{a,b,c}.json
# touch web/other-{1,2,3,4}
## Builds directories
BUILDS_DIR='builds'
BUILDS_GCS_DIR='gcs'
BUILDS_GCS_TAR_FILE="gcs-$(date +"%Y%m%d-%H%M%S").tar.gz"
## Project directories
PROJECT_DIR='tmp'
GCS_BIN_DIR='cpp/bin'
GCS_BIN_FILE='gcs.out'
GCS_WEB_DIR='web'
GCS_GUI_DIR='gui'
## Defines the valid files to add to the tar
GCS_FILES=(
conf-{a,b,c}.json
gui/gui-{a,b,c,d}.js
)
# for i in "${GCS_FILES[@]}"; do echo "$i"; done
# for i in "${GCS_FILES[*]}"; do echo "$i"; done
cmd_file_exits() {
[ -f "$1" ] && return 0 || {
echo "$1 does not exits."
exit 1
}
}
cmd_expand_files() {
files=()
for i in "${GCS_FILES[@]}"; do files+=("$gcs_web_path/$i"); done
# echo "${files[*]}"
join=$(
IFS=,
printf "${files[*]}"
)
printf $join
}
cmd_remove_first_dir_path() {
echo $1 | cut -d'/' -f2-
}
## Defines paths
builds_path="~/$BUILDS_DIR"
builds_path="${builds_path/#\~/$HOME}"
builds_gcs_path="$builds_path/$BUILDS_GCS_DIR"
builds_gcs_gui_path="$builds_gcs_path/$GCS_GUI_DIR"
builds_gcs_tar_path="$builds_path/$BUILDS_GCS_TAR_FILE"
project_path="~/$PROJECT_DIR"
project_path="${project_path/#\~/$HOME}"
gcs_bin_path="$project_path/$GCS_BIN_DIR"
gcs_web_path="$project_path/$GCS_WEB_DIR"
gcs_bin_file_path="$gcs_bin_path/$GCS_BIN_FILE"
## Verify if they exit
cmd_file_exits $gcs_bin_file_path
for i in "${GCS_FILES[@]}"; do cmd_file_exits "$gcs_web_path/$i"; done
# echo $(cmd_expand_files)
# string_list="$(cmd_expand_files)"
# readarray -td, gcs_web_files <<<"$string_list"
# declare -p gcs_web_files
# readarray -td '' gcs_web_files < <(awk '{ gsub(/,/,"\0"); print; }' <<<"$string_list, "); unset 'gcs_web_files[-1]';
# declare gcs_web_files
# declare -p gcs_web_files;
# gcs_web_files+=($gcs_bin_file_path)
# for i in "${gcs_web_files[@]}"; do echo "$i"; done
if [ ! -d "$builds_gcs_path" ]; then
mkdir -vp "${builds_gcs_path}"
fi
if [ ! -d "$builds_gcs_gui_path" ]; then
mkdir -vp "${builds_gcs_gui_path}"
fi
rsync -chavzP "${gcs_bin_file_path}" "${builds_gcs_path}"
for i in "${GCS_FILES[@]}"; do
rsync -chavzP "$gcs_web_path/$i" "${builds_gcs_path}/$i"
done
tree "$builds_gcs_path"
# rsync -chavzP "${gcs_web_files[@]}" "${builds_gcs_path}"
# cp -vp "${gcs_web_files[@]}" "${builds_gcs_path}"
## tar czvf $BUILDS_GCS_TAR_FILE -C "$builds_gcs_path" .
## or
## cd "$builds_gcs_path" && tar czvf "$BUILDS_GCS_TAR_FILE" .
tar czvf "$builds_gcs_tar_path" -C "$builds_path" "$BUILDS_GCS_DIR"
[ "$?" -eq 0 ] && exit 0 || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment