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

基于文件名日期归档文件并100天后删除归档,脚本故障求助

Fixing Your CSV Archiving Script Issues

Hey there, let's work through getting your script back on track. It sounds like you're dealing with a few common pitfalls when handling filename patterns, archiving logic, and auto-deletion rules. Let's break this down step by step.

First, Let's Cover the Most Likely Problems

Chances are your script is failing for one of these reasons:

  • Your regex isn't correctly parsing the YYYYMMDD date from that specific filename format
  • The script isn't creating the 2018 archive directory properly (or lacks permissions to do so)
  • The auto-deletion logic is targeting the wrong files, or the date calculation is off
  • You're hitting permission issues when trying to move or delete files

Here's a Corrected Bash Script That Meets Your Needs

I've written a script that handles your exact filename structure, archives 2018 files, and sets up 100-day auto-deletion, with comments to explain each part:

#!/bin/bash

# Update these paths to match your system
SOURCE_CSV_DIR="/home/your-user/csv-files"
ARCHIVE_ROOT="/home/your-user/csv-archives"

# 1. Create 2018 archive directory if it doesn't exist
mkdir -p "${ARCHIVE_ROOT}/2018"

# Loop through all CSV files in the source directory
for csv_file in "${SOURCE_CSV_DIR}"/*.csv; do
    # Skip if there are no matching CSV files
    [ -e "$csv_file" ] || continue

    # Extract the YYYYMMDD date from the filename using regex
    # Filename format: 11 random letters_6 random alnum_YYYYMMDD.random_num_random_num.csv
    if [[ "$csv_file" =~ ^[a-zA-Z]{11}_[a-zA-Z0-9]{6}_([0-9]{8})\.[0-9]+_[0-9]+\.csv$ ]]; then
        extracted_date="${BASH_REMATCH[1]}"
        
        # Check if the date belongs to 2018
        if [[ "$extracted_date" == "2018"* ]]; then
            echo "Archiving: $csv_file -> ${ARCHIVE_ROOT}/2018/"
            mv "$csv_file" "${ARCHIVE_ROOT}/2018/"
        fi
    else
        echo "Skipping non-matching file: $csv_file"
    fi
done

# 2. Delete archived CSV files older than 100 days
echo "Cleaning up archived files older than 100 days..."
find "${ARCHIVE_ROOT}" -type f -name "*.csv" -mtime +100 -delete

# Optional: Print status to verify
echo "Done! Here's what's left in the 2018 archive:"
ls -lh "${ARCHIVE_ROOT}/2018/"

How to Debug Your Original Script

If you want to fix your existing script instead of using this example, try these checks:

  1. Test your regex: Run this quick test to make sure your pattern captures the date correctly:
    test_filename="abcdefghijk_123abc_20180515.789_101.csv"
    if [[ "$test_filename" =~ ^[a-zA-Z]{11}_[a-zA-Z0-9]{6}_([0-9]{8})\.[0-9]+_[0-9]+\.csv$ ]]; then
        echo "Captured date: ${BASH_REMATCH[1]}"
    else
        echo "Pattern didn't match!"
    fi
    
  2. Check directory permissions: Run ls -ld "${SOURCE_CSV_DIR}" "${ARCHIVE_ROOT}/2018" to make sure your user has read/write access to both locations.
  3. Validate the auto-deletion rule: Test the find command without the -delete flag first to see which files it would target:
    find "${ARCHIVE_ROOT}" -type f -name "*.csv" -mtime +100
    
  4. Add debug echoes: Insert echo "Processing $csv_file" or echo "Date extracted: $extracted_date" into your original script to see where it's failing.

Key Fixes in the Example Script

  • Precise regex matching: The pattern exactly matches your filename structure, so only valid files get processed
  • Safe directory creation: mkdir -p ensures the archive directory exists (even if parent folders don't)
  • Guard against empty loops: The [ -e "$csv_file" ] check prevents errors when there are no CSV files in the source directory
  • Explicit date filtering: We explicitly check if the extracted date starts with 2018 to avoid archiving the wrong files

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:32:08