-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path,image-move
executable file
·51 lines (40 loc) · 1.7 KB
/
,image-move
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
#!/bin/bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <from_dir> <to_dir>"
exit 1
fi
from_sourcedir="$1"
to_destdir="$2"
mkdir -p "$to_destdir/assets"
echo "Extracting..."
# Store directories with images, videos, etc. in an array
dir_with_assets=()
while IFS= read -r -d '' dir; do
dir_with_assets+=("$dir")
dirname=$(basename "$dir")
# Rename images to their directory name and move them to to_destdir
find "$dir" -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.mov" -o -iname "*.webp" -o -iname "*.gif" -o -iname "*.mp4" \) -print0 | while IFS= read -r -d '' file; do
filename=$(basename "$file")
new_filename="$dirname-${filename// /_}" # Replace spaces with underscores
# If file already exists, append a random number to the filename
while [ -f "${to_destdir}/assets/${new_filename}" ]; do
new_filename="$dirname-${filename// /_}-${RANDOM}"
done
mv "$file" "${to_destdir}/assets/${new_filename}"
echo "Moved $file to ${to_destdir}/assets/${new_filename}"
# Handle renaming image references in markdown files
echo "Updating image references in markdown files..."
find "$from_sourcedir" -type f -name "$dirname.md" -print0 | while IFS= read -r -d '' mdfile; do
# Update image references in markdown files
sed -i "s/!\[.*\](.*\/)*Untitled[[:space:]]*\(\.jpg\|\.jpeg\|\.png\|\.mov\|\.webp\|\.gif\|\.mp4\)//g" "$mdfile"
done
done
done < <(find "$from_sourcedir" -mindepth 1 -maxdepth 1 -type d -print0)
# Remove empty directories
find "$from_sourcedir" -type d -empty -delete
if [ ${#dir_with_assets[@]} -eq 0 ]; then
echo "Error: No directories found."
exit 1
fi
echo "Extraction completed."