Last active
August 16, 2026 12:09
-
-
Save f-lawe/f2b99dabbc761a0f90b44952ac021363 to your computer and use it in GitHub Desktop.
Dev container utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Script to copy a source file/directory to a destination with specified permissions and ownership | |
| # Usage: ./copy_if_exists.sh source destination user:group dir_perms [file_perms] | |
| copy_if_exists() { | |
| local source="$1" | |
| local target="$2" | |
| local owner="$3" | |
| local dir_perms="$4" | |
| local file_perms="${5:-}" | |
| if [ -f "$source" ]; then | |
| mkdir -p "$(dirname "$target")" | |
| cp "$source" "$target" | |
| chown "$owner" "$target" | |
| chmod "$dir_perms" "$target" | |
| if [ -n "$file_perms" ]; then | |
| chmod "$file_perms" "$target" | |
| fi | |
| echo "✅ Copied: $source → $target" | |
| elif [ -d "$source" ]; then | |
| mkdir -p "$(dirname "$target")" | |
| cp -rT "$source" "$target" | |
| chown -R "$owner" "$target" | |
| chmod "$dir_perms" "$target" | |
| echo "✅ Copied: $source → $target" | |
| else | |
| echo "⚠️ Not found or empty: $source" | |
| fi | |
| } | |
| # Main execution | |
| if [ $# -lt 4 ] || [ $# -gt 5 ]; then | |
| echo "Usage: $0 source destination user:group dir_perms [file_perms]" | |
| exit 1 | |
| fi | |
| copy_if_exists "$1" "$2" "$3" "$4" "$5" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment