Created
September 12, 2026 10:53
-
-
Save mypapit/df6615f8611f03678bc93ee0485001be to your computer and use it in GitHub Desktop.
batch-docx-to-markdown.sh - converts all docx files to markdown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -u | |
| shopt -s nullglob | |
| echo "Batch convert DOCX to Markdown" | |
| echo "Script downloaded from https://blog.mypapit.net" | |
| if ! command -v pandoc >/dev/null 2>&1; then | |
| printf 'Pandoc is missing. Install it with: sudo apt install pandoc\n' >&2 | |
| exit 1 | |
| fi | |
| files=(./*.docx) | |
| if ((${#files[@]} == 0)); then | |
| printf 'No .docx files found in the current folder.\n' | |
| exit 0 | |
| fi | |
| mkdir -p -- markdown || exit 1 | |
| status=0 | |
| for file in "${files[@]}"; do | |
| [[ -f "$file" ]] || continue | |
| name="${file##*/}" | |
| [[ "$name" == '~$'* ]] && continue | |
| output="markdown/${name%.docx}.md" | |
| if pandoc "$file" --from=docx --to=markdown --wrap=none -o "$output"; then | |
| printf 'Converted: %s -> %s\n' "$file" "$output" | |
| else | |
| printf 'Conversion failed: %s\n' "$file" >&2 | |
| status=1 | |
| fi | |
| done | |
| exit "$status" | |
| Open a terminal in |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment