Skip to content

Instantly share code, notes, and snippets.

@teklynk
Created October 7, 2025 00:43
Show Gist options
  • Select an option

  • Save teklynk/85e4336a31b01230bebfb5c70c9ffae7 to your computer and use it in GitHub Desktop.

Select an option

Save teklynk/85e4336a31b01230bebfb5c70c9ffae7 to your computer and use it in GitHub Desktop.
Bash scrip to remove dead links from your bookmarks
# Export your bookmarks from a chromium based browser as a html file
# Use the exported html bookmarks file as the <input_file>
# Run the script
# The script will use cURL to check if a link is dead. It will attempt a check 5 times before removing the link.
#!/bin/bash
# Function to display usage information
usage() {
echo "Usage: $0 -f <input_file>"
exit 1
}
# Parse command line options
while getopts ":f:" opt; do
case $opt in
f)
input_file="$OPTARG"
;;
*)
usage
;;
esac
done
# Check if input file is provided
if [ -z "$input_file" ]; then
usage
fi
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: Input file '$input_file' not found"
exit 1
fi
# Create output filename by appending _updated.html to the input filename
output_file="${input_file%.*}_updated.html"
# Create a temporary file to store valid bookmarks
temp_file=$(mktemp)
# Copy header part of the file
grep -A 10 "<DL>" "$input_file" > "$temp_file"
# Extract URLs and check them
grep -A 1000 "<DT><A" "$input_file" | while IFS= read -r line; do
# Get the URL from the bookmark line
url=$(echo "$line" | grep -oP '(?<=HREF=")[^"]*')
# Skip empty lines
if [[ -z "$url" ]]; then
continue
fi
# Check if URL is valid using curl with timeout
echo "Checking URL: $url"
response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$url" 2>/dev/null)
# If response code starts with 2 or 3, it's a valid URL
if [[ "$response" =~ ^[23] ]]; then
echo "Valid URL, adding to output"
echo "$line" >> "$temp_file"
else
echo "Invalid URL, skipping"
fi
done
# Add closing tags
echo "</DL>" >> "$temp_file"
echo "</DL>" >> "$temp_file"
# Move temp file to output file
mv "$temp_file" "$output_file"
echo "Done! Valid bookmarks saved to $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment