Skip to content

Instantly share code, notes, and snippets.

@aidanhmiles
Created January 31, 2017 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidanhmiles/8781928519f3c03b8222d689799f1756 to your computer and use it in GitHub Desktop.
Save aidanhmiles/8781928519f3c03b8222d689799f1756 to your computer and use it in GitHub Desktop.
Copy files from A to B, preserving directory hierarchy from wherever the script is executed
#!/usr/bin/env bash
main() {
local files=()
# Method 1
# while read -r fname; do
# derp+=("$fname")
# done < <(ag -g html base/)
# Method 2
mapfile -t files < <( ag -g html . )
for item in "${files[@]}"
do
local target_dir="../templates/$(dirname $item)/"
if [[ -d "$target_dir" ]]; then
echo "dir $target_dir does exist";
else
echo "dir $target_dir no exist"
echo "creating it"
mkdir -p $target_dir
fi
echo "copying $item to $target_dir"
cp "$item" "$target_dir"
echo
echo
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment