diff --git a/add_timestamp.sh b/add_timestamp.sh index 8ba00975..07cff781 100755 --- a/add_timestamp.sh +++ b/add_timestamp.sh @@ -1,29 +1,28 @@ #!/bin/bash -#別のマシンで作ったファイルはわからんので、それぞれのでやって古い方で上書きする必要がある cwd=`dirname "${0}"` +function rewritedate () { + echo "$1 : $2" + echo $2 | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$1" +} FILES=$(find $cwd/content -name "*.md") IFS=$'\n' for f in $FILES do BDATE=$(stat -f "%SB" "$f"); +DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE"); FLINE=$(head -n 1 "$f"); -if [[ "$FLINE" == "---" ]]; -then -echo "$f has frontmatter"; ORIGINDATE=$(yq --front-matter=extract '.date' "$f" | date "+%s") LOCALDATE=$(LC_ALL=C date -j -f "%b %d %H:%M:%S %Y" "$BDATE" | date "+%s"); - -if [ $LOCALDATE -lt $ORIGINDATE ]; then # -lt : '<' - echo "date of local file is older. overwriting." - DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE"); - echo "$f : $DATE" - echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f" +GITDATE=$(git blame $f | grep -Eo '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'| xargs -I{} date -j -f "%Y-%m-%d %H:%M:%S" "{}" | date "+%s"); +if [[ "$FLINE" == "---" ]]; +then +if [ $LOCALDATE -lt $ORIGINDATE ]; then + echo "$f has frontmatter and date of local file is older. overwriting." + rewritedate $f $BDATE fi else -echo "$f has no frontmatter, adding"; +echo "$f has no frontmatter, adding to it"; gsed -i '1i---\n---' "$f" -DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE"); -echo "$f : $DATE" -echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f" +rewritedate $f $DATE fi done \ No newline at end of file diff --git a/content/ACEが読むと面白いかもしれない漫画、映画、ドラマとか.md b/content/ACEが読むと面白いかもしれない漫画、映画、ドラマとか.md index bfae17af..6480ff73 100644 --- a/content/ACEが読むと面白いかもしれない漫画、映画、ドラマとか.md +++ b/content/ACEが読むと面白いかもしれない漫画、映画、ドラマとか.md @@ -1,5 +1,5 @@ --- -date: "2024-02-08T17:35:58+09:00" +date: "2024-02-08T17:46:04+09:00" --- #book #ace diff --git a/content/Obsidianノートの作成日を後から復元したい.md b/content/Obsidianノートの作成日を後から復元したい.md index 366fdf8f..f2aec808 100644 --- a/content/Obsidianノートの作成日を後から復元したい.md +++ b/content/Obsidianノートの作成日を後から復元したい.md @@ -17,33 +17,31 @@ date: "2024-02-08T17:18:56+09:00" ```bash #!/bin/bash -#別のマシンで作ったファイルのBirth Dateはわからんので、それぞれのマシンで実行して古い方で上書きする cwd=`dirname "${0}"` +function rewritedate () { + echo "$1 : $2" + echo $2 | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$1" +} FILES=$(find $cwd/content -name "*.md") IFS=$'\n' for f in $FILES do BDATE=$(stat -f "%SB" "$f"); FLINE=$(head -n 1 "$f"); -if [[ "$FLINE" == "---" ]]; -then -echo "$f has frontmatter"; ORIGINDATE=$(yq --front-matter=extract '.date' "$f" | date "+%s") LOCALDATE=$(LC_ALL=C date -j -f "%b %d %H:%M:%S %Y" "$BDATE" | date "+%s"); - -if [ $LOCALDATE -lt $ORIGINDATE ]; then #古い方で上書き - echo "date of local file is older. overwriting." +if [[ "$FLINE" == "---" ]]; +then +if [ $LOCALDATE -lt $ORIGINDATE ]; then + echo "$f has frontmatter and date of local file is older. overwriting." DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE"); - echo "$f : $DATE" - echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f" + rewritedate $f $BDATE fi -#フロントマターがあって、その日付が最古なら何もしない else -echo "$f has no frontmatter, adding"; -gsed -i '1i---\n---' "$f" +echo "$f has no frontmatter, adding to it"; DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE"); -echo "$f : $DATE" -echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f" +gsed -i '1i---\n---' "$f" +rewritedate $f $DATE fi done ```