Skip to content

Instantly share code, notes, and snippets.

@bykbtzr
Created January 11, 2026 02:07
Show Gist options
  • Select an option

  • Save bykbtzr/aae0843d21434b7359a203a419c5a7f4 to your computer and use it in GitHub Desktop.

Select an option

Save bykbtzr/aae0843d21434b7359a203a419c5a7f4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Target directory to watch (current directory)
WATCH_DIR="${HOME}/Downloads"
# Directory to move processed files to
ARCHIVE_DIR="${WATCH_DIR}/archive"
# Ensure the archive directory exists
mkdir -p "$ARCHIVE_DIR"
# Function to process a single file
process_file() {
local filename="$1"
local filepath="$WATCH_DIR/$filename"
# Check if file exists and ends with .md
if [[ -f "$filepath" && "$filename" == *.md ]]; then
echo "Processing $filename..."
# Cat the contents and pipe to wl-copy (Wayland clipboard)
# We use a small delay or retry might be redundant but safe
if cat "$filepath" | wl-copy; then
# Send notification
notify-send "Markdown Watcher" "Copied $filename to clipboard"
# Move the file to the archive directory
mv "$filepath" "$ARCHIVE_DIR/"
echo "Copied to clipboard and moved to $ARCHIVE_DIR/"
else
echo "Failed to copy $filename to clipboard"
fi
fi
}
echo "Started watching $WATCH_DIR for Markdown files using inotifywait..."
# 1. Process existing files first
shopt -s nullglob
for file in "$WATCH_DIR"/*.md; do
process_file "$(basename "$file")"
done
# 2. Watch for new files
# -m: monitor indefinitely
# -e close_write: file finished writing (download complete)
# -e moved_to: file moved into directory
# --format "%f": output only filename
# -q: quiet (suppress startup message)
inotifywait -m -q -e close_write -e moved_to --format "%f" "$WATCH_DIR" | while read -r filename; do
process_file "$filename"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment