请求编写Shell脚本:检测文件名是否含时间戳并按需追加
Shell Script to Check and Append Timestamp to Filename
Got it, let's build a shell script that handles checking for an existing timestamp in a filename and appending one if it's missing. I'll use a common timestamp format (YYYYMMDD_HHMMSS) as an example—you can tweak this to match your preferred format easily.
The Script
Here's a complete, commented script that does exactly what you need:
#!/bin/bash # Optional: Accept target file as a command-line argument (uncomment below) # if [ $# -eq 0 ]; then # echo "Usage: $0 <target-file>" # exit 1 # fi # target_file="$1" # Define your target file (replace with your actual filename) target_file="document.pdf" # Define the timestamp format we want to use (adjust as needed) timestamp=$(date +"%Y%m%d_%H%M%S") # Regex pattern to detect the timestamp at the end of the filename # This matches "_YYYYMMDD_HHMMSS" before the extension (if any) timestamp_pattern="_[0-9]{8}_[0-9]{6}(\.[a-zA-Z0-9]+)?$" # Check if the filename already contains a valid timestamp if [[ "$target_file" =~ $timestamp_pattern ]]; then echo "✅ File '$target_file' already has a timestamp. Moving to next steps..." # Add your post-check logic here # Example: Upload to cloud storage, process the file, etc. # aws s3 cp "$target_file" s3://my-bucket/processed/ else echo "⚠️ No timestamp found in '$target_file'. Appending one now..." # Split filename into base name and extension (handles files without extensions) file_base="${target_file%.*}" file_ext="${target_file#*.}" # Construct the new filename if [[ "$file_base" == "$file_ext" ]]; then # No extension case (e.g., "README") new_filename="${file_base}_${timestamp}" else # With extension case (e.g., "document.pdf" → "document_20240520_143022.pdf") new_filename="${file_base}_${timestamp}.${file_ext}" fi # Rename the file mv "$target_file" "$new_filename" echo "✅ Successfully renamed to '$new_filename'" # Add your post-rename logic here (use new_filename instead of target_file) # Example: Compress the file # gzip "$new_filename" fi
Key Details to Note
- Timestamp Format: The
date +"%Y%m%d_%H%M%S"command generates a timestamp like20240520_143022. Change the format string (e.g.,%Y-%m-%dfor2024-05-20) if you need a different style. - Regex Pattern: The
timestamp_patternis tailored to match our example format. If you change the timestamp format, update the regex to match—for example, if using hyphens, adjust the pattern to-[0-9]{4}-[0-9]{2}-[0-9]{2}. - Command-Line Argument: Uncomment the top section to make the script accept a filename as an input parameter (e.g.,
./timestamp_script.sh report.txt), which makes it more reusable. - No Extension Handling: The script correctly handles files without extensions (like
README) by appending the timestamp directly to the base name.
Testing the Script
- Save the script as
timestamp_checker.sh - Make it executable:
chmod +x timestamp_checker.sh - Run it:
./timestamp_checker.sh(or pass a filename if you enabled the argument option)
内容的提问来源于stack exchange,提问作者awareness_seeker
相关产品推荐
相关产品推荐

