Skip to content

Instantly share code, notes, and snippets.

@f-lawe
Last active August 16, 2026 12:09
Show Gist options
  • Select an option

  • Save f-lawe/f2b99dabbc761a0f90b44952ac021363 to your computer and use it in GitHub Desktop.

Select an option

Save f-lawe/f2b99dabbc761a0f90b44952ac021363 to your computer and use it in GitHub Desktop.
Dev container utilities
#!/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