如何在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.
1.1 Generate Sorted Markdown Links
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}'
findlocates all.mdfiles insd/starting withsd_grepfilters to only files matching thesd_YYYY-MM-DD.mdpattern- First
awkextracts the date string from filenames and pairs it with the full file path sort -rsorts dates in reverse chronological order (newest first)- Second
awkreformats dates toDD/MM/YYYYand 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
sedcommand targets the# Current Filesheading, then replaces all content between it and the next heading (or end of file) with your new list - The
.bakfile 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.mdfiles on themainbranch - You can also run it manually via your repo's "Actions" tab
- The
permissionsline 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
相关产品推荐
相关产品推荐

