Skip to content

Instantly share code, notes, and snippets.

@AJeschor
Last active February 29, 2024 21:41
Show Gist options
  • Save AJeschor/8e2790e10b97f2d6286b978fcabf84e2 to your computer and use it in GitHub Desktop.
Save AJeschor/8e2790e10b97f2d6286b978fcabf84e2 to your computer and use it in GitHub Desktop.
This Bash script copies Oh My ZSH plugin docs to Obsidian format, prompts for source and destination dirs, uses rsync to preserve structure, then renames files for categorization in Obsidian.
#!/bin/bash
# Prompt user for source directory
read -p "Select source Oh My ZSH plugin directory. Default is ~/.oh-my-zsh/plugins. Press Enter or 'y' to use default: " source_dir_input
source_dir_input=${source_dir_input:-~/.oh-my-zsh/plugins}
# Check if the provided source directory is a valid directory
if [ ! -d "$source_dir_input" ]; then
echo "Invalid directory. Please provide a valid path or press Enter/'y' for the default."
exit 1
fi
source_dir=$source_dir_input
# Prompt user for destination directory
read -p "Select destination notes directory. Default is ~/Obsidian/Notes/ZSH. Press Enter or 'y' to use default: " destination_dir_input
destination_dir_input=${destination_dir_input:-~/Obsidian/Notes/ZSH}
# Check if the provided destination directory is a valid directory
if [ ! -d "$destination_dir_input" ]; then
echo "Invalid directory. Please provide a valid path or press Enter/'y' for the default."
exit 1
fi
destination_dir=$destination_dir_input
# Use rsync to copy all .md files from subdirectories to the destination directory
rsync -a --include='*/' --include='*.md' --exclude='*' "$source_dir" "$destination_dir"
# Rename copied .md files in the destination directory
cd "$destination_dir" || exit
find . -type f -name "*.md" |
while read -r file; do
# Extract subdirectory name
subdirectory=$(dirname "$file" | sed 's|./||')
# Create new filename with subdirectory name
new_filename="$(basename "$subdirectory") - $(basename "$file")"
# Rename the file
mv "$file" "$new_filename"
done
echo "Copying and renaming complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment