You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在README中按规则更新sd目录下的文件列表

Solution

Step 1: Shell Commands to Process Files & Update README

First, use these commands to locate valid files, reformat dates, sort them, and replace the content in your README's # Current Files section.

Run this command to create a list of links sorted from newest to oldest:

find sd/ -type f -name 'sd_*.md' | grep -E 'sd_[0-9]{4}-[0-9]{2}-[0-9]{2}\.md' | awk -F'[_.-]' '{print $2"-"$3"-"$4 "|" $0}' | sort -r | awk -F'|' '{split($1, d, "-"); printf "- [%s/%s/%s](%s)\n", d[3], d[2], d[1], $2}'
  • find locates all .md files in sd/ starting with sd_
  • grep filters to only files matching the sd_YYYY-MM-DD.md pattern
  • First awk extracts the date string from filenames and pairs it with the full file path
  • sort -r sorts dates in reverse chronological order (newest first)
  • Second awk reformats dates to DD/MM/YYYY and builds markdown links

1.2 Replace README Section

Use sed to overwrite the existing content under # Current Files with the new list:

# Store the generated list in a variable
FILE_LIST=$(find sd/ -type f -name 'sd_*.md' | grep -E 'sd_[0-9]{4}-[0-9]{2}-[0-9]{2}\.md' | awk -F'[_.-]' '{print $2"-"$3"-"$4 "|" $0}' | sort -r | awk -F'|' '{split($1, d, "-"); printf "- [%s/%s/%s](%s)\n", d[3], d[2], d[1], $2}')

# Update README.md
sed -i.bak -e '/^# Current Files$/{n;:a;N;/^#/!ba;s/.*\n/'"$FILE_LIST"'\n/}' README.md

# Clean up temporary backup file
rm README.md.bak
  • This sed command targets the # Current Files heading, then replaces all content between it and the next heading (or end of file) with your new list
  • The .bak file is a temporary backup we delete afterward

Step 2: GitHub Actions Workflow Automation

Create a workflow file at .github/workflows/update-readme.yml to run this process automatically when new files are added, or trigger it manually:

name: Update Current Files in README

permissions:
  contents: write # Allow pushing changes back to the repository

on:
  push:
    branches: [ main ]
    paths:
      - 'sd/sd_*.md' # Trigger when valid files are added/modified
  workflow_dispatch: # Allow manual trigger via GitHub UI

jobs:
  update-readme:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Required for git commit operations

      - name: Generate file list and update README
        run: |
          FILE_LIST=$(find sd/ -type f -name 'sd_*.md' | grep -E 'sd_[0-9]{4}-[0-9]{2}-[0-9]{2}\.md' | awk -F'[_.-]' '{print $2"-"$3"-"$4 "|" $0}' | sort -r | awk -F'|' '{split($1, d, "-"); printf "- [%s/%s/%s](%s)\n", d[3], d[2], d[1], $2}')
          
          sed -i.bak -e '/^# Current Files$/{n;:a;N;/^#/!ba;s/.*\n/'"$FILE_LIST"'\n/}' README.md
          
          rm README.md.bak

      - name: Commit and push changes
        run: |
          git config --global user.name "GitHub Actions Bot"
          git config --global user.email "actions@github.com"
          git add README.md
          # Only commit if there are actual changes
          if ! git diff --quiet && ! git diff --staged --quiet; then
            git commit -m "Update Current Files list in README"
            git push
          fi

Key Details:

  • The workflow triggers automatically when you push changes to valid sd_YYYY-MM-DD.md files on the main branch
  • You can also run it manually via your repo's "Actions" tab
  • The permissions line grants the action permission to push changes back to your repository
  • The final step skips empty commits by checking for changes before committing

内容的提问来源于stack exchange,提问作者confusedandbemused

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 05:13:19