-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync-markdown-content.sh
More file actions
executable file
·64 lines (52 loc) · 1.74 KB
/
sync-markdown-content.sh
File metadata and controls
executable file
·64 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
# Usage: ./sync-markdown-content.sh <source_dir> <target_dir>
#
# Description:
# - Recursively loops through the source directory.
# - If a markdown file (.md) exists in both source and target, replaces the target file content.
# - If any file (of any type) or directory exists in source but not in target, creates or copies it.
# - Ignores hidden files and directories (starting with .).
set -euo pipefail
SRC="${1:-}"
DEST="${2:-}"
if [ -z "$SRC" ] || [ -z "$DEST" ]; then
echo "Usage: $0 <source_dir> <target_dir>"
exit 1
fi
if [ ! -d "$SRC" ]; then
echo "❌ Source directory not found: $SRC"
exit 1
fi
mkdir -p "$DEST"
echo "📂 Source: $SRC"
echo "📁 Target: $DEST"
echo "============================================================="
# --- 1️⃣ Ensure all directories from source exist in target ---
find "$SRC" -type d ! -path "*/.*" | while read -r SRC_DIR; do
REL_PATH="${SRC_DIR#$SRC/}"
DEST_DIR="$DEST/$REL_PATH"
if [ ! -d "$DEST_DIR" ]; then
mkdir -p "$DEST_DIR"
echo "📁 Created directory: $REL_PATH"
fi
done
# --- 2️⃣ Copy or replace files ---
find "$SRC" -type f ! -path "*/.*" | while read -r SRC_FILE; do
REL_PATH="${SRC_FILE#$SRC/}"
DEST_FILE="$DEST/$REL_PATH"
# Ensure destination directory exists (redundant but safe)
mkdir -p "$(dirname "$DEST_FILE")"
if [ -f "$DEST_FILE" ]; then
if [[ "$SRC_FILE" == *.md ]]; then
cp "$SRC_FILE" "$DEST_FILE"
echo "🔄 Replaced markdown: $REL_PATH"
else
echo "⚙️ Skipped existing non-markdown file: $REL_PATH"
fi
else
cp "$SRC_FILE" "$DEST_FILE"
echo "🆕 Copied new file: $REL_PATH"
fi
done
echo "============================================================="
echo "🎯 Done syncing."