TIL you can use TXT files for sitemaps

Until today I had only ever used XML files for creating sitemaps for search engines like Google, but today I saw that Google can read sitemaps as TXT files, as long as there is one URL per line.

Since many of my websites are folders of HTML files, it's very simple to generate a plain text sitemap using the command line. All I needed to do was:

  • empty a file named sitemap.txt of its contents
  • find all .html files in the main folder (and subfolders)
  • echo the site URL, followed by each filename as a new line in the sitemap.txt file

To do these steps I wrote a shell script that looked something like this:

#!/usr/local/bin/fish

true > sitemap.txt

echo "Building sitemap…"

for file in *.html */*.html
  echo " adding $file"
  echo "https://example.com/$file" >> sitemap.txt
end

echo "Sitemap built"

I'm using fish shell for my shell, but it would be similar and equally straightforward to write a script like this for bash, zsh, or any other shell you use.

Because of how simple this made my job tonight, I was able to add sitemaps to three of my websites. I hope knowing this tip makes somebody else's day easier too!

No Comments Yet