Code Snippets

Little bits of code that help.

Table of Contents

Sync everything in a folder to a remote server #

Very handy if you want to avoid sending things via Filezilla or Transmit. It only sends up new or updated files, so it’s smarter than just overwriting every file for each update.

rsync -ahv --progress -e "ssh" ~/path/to/file/ user@SERVER_IP:/path/to/folder/

Copy all text from PDF to TXT file #

I was doing a lot of research for my book and wanted to get text from PDFs into a more searchable format.

pdftotext -layout file.pdf

Convert image formats #

This site relies on .webp for a lot of its images, and they’re far lighter than a traditional .jpg or .png. Like, about 80% lighter. I wouldn’t use this for images you’re going to edit in Photoshop, but they’re perfect for the web.

mogrify -format webp -quality 90 *.jpeg

Markdown to Word conversion #

Relies on python3 and pandoc. Helpful if you’re exporting from a tool like Notion.

# install pandoc
# $ apt-get install pandoc  # On Debian/Ubuntu systems
# $ brew install pandoc     # On macOS

Take this script, put it in the directory with your .md files (I called it rename.py) and run it. If you use the proper markdown formatting (which Notion respects), you’ll get a pretty decent export. You won’t have it honor things like callout and other more customized functions, so you’ll need to account for that on your own.

import os
import subprocess

def convert_md_to_docx(folder_path="."):
    for filename in os.listdir(folder_path):
        if filename.endswith('.md'):
            input_file_path = os.path.join(folder_path, filename)
            output_file_path = os.path.join(folder_path, filename.replace('.md', '.docx'))
            
            # Call pandoc to perform the conversion
            subprocess.run(['pandoc', input_file_path, '-s', '-o', output_file_path])

if __name__ == '__main__':
    convert_md_to_docx()