Created
January 9, 2026 03:07
-
-
Save CookieBox26/318fdc89e6f10684cd489b875f1004a3 to your computer and use it in GitHub Desktop.
指定ディレクトリ以下の .txt を一斉に .md にリネームする (あるいはその逆をする) スクリプト (混在時はエラー)
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/bash | |
| # Ex. ./convexts.sh ./Dropbox/obsidian/Mercury/References/ | |
| set -euo pipefail # Fail fast on errors, undefined variables, and broken pipelines | |
| [ "$#" -eq 1 ] || { echo "Usage: $0 <target_dir>" >&2; exit 1; } | |
| target_dir=$1 | |
| [ -e "$target_dir" ] || { echo "Error: not found: $target_dir" >&2; exit 2; } | |
| cd "$target_dir" | |
| shopt -s nullglob # Make globs expand to empty when no files match | |
| li_txt=( *.txt ) | |
| li_md=( *.md ) | |
| [ ${#li_txt[@]} -gt 0 ] && [ ${#li_md[@]} -gt 0 ] && { echo "Error: Mixed exts" >&2; exit 3; } | |
| [ ${#li_txt[@]} -gt 0 ] && { li=( "${li_txt[@]}" ); from=txt; to=md; } | |
| [ ${#li_md[@]} -gt 0 ] && { li=( "${li_md[@]}" ); from=md; to=txt; } | |
| [ -v li ] || { echo "Error: No targets" >&2; exit 4; } | |
| for src in "${li[@]}"; do | |
| dst="${src%.$from}.$to" | |
| mv -- "$src" "$dst" | |
| echo $dst | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment