To ensure the metadata of the modified file (such as the file creation time and date) remains the same, we can use touch to preserve the original file’s timestamp. Here is an updated version of the script that includes this functionality:

#!/bin/bash

# Define the base directory
BASE_DIRECTORY="/path/to/directory"

# Find all files named Weather.md in the base directory and its subdirectories
find "$BASE_DIRECTORY" -type f -name "Weather.md" | while read -r FILE; do
    # Check if the file already contains front matter
    FIRST_LINE=$(head -n 1 "$FILE")
    if [[ "$FIRST_LINE" != "+++" ]]; then
        # Get the file creation and modification dates
        CREATION_DATE=$(stat -c %w "$FILE" 2>/dev/null || stat -c %y "$FILE")
        MODIFICATION_DATE=$(stat -c %y "$FILE")
        CREATION_DATETIME=$(date -d "$CREATION_DATE" +"%Y-%m-%dT%H:%M:%S%z")
        
        # Define the front matter with the creation date and time
        FRONT_MATTER="+++
title = 'Weather'
date = \"$CREATION_DATETIME\"
draft = false
tags = ['weather', 'automatic']
+++"

        # Create a temporary file with the front matter and the original content
        TEMP_FILE="${XDG_RUNTIME_DIR:-/tmp}/temp_weather.md"
        echo -e "$FRONT_MATTER\n$(cat "$FILE")" > "$TEMP_FILE"
        
        # Preserve the original timestamps
        ORIGINAL_ATIME=$(stat -c %x "$FILE")
        ORIGINAL_MTIME=$(stat -c %y "$FILE")
        
        # Overwrite the original file with the new content
        mv "$TEMP_FILE" "$FILE"
        
        # Restore the original timestamps
        touch -d "$ORIGINAL_ATIME" "$FILE"
        touch -d "$ORIGINAL_MTIME" "$FILE"
    fi
done

Explanation:

  1. Define Base Directory:

    • The BASE_DIRECTORY variable should be set to the directory where you want to search for Weather.md files.
  2. Find Files:

    • find "$BASE_DIRECTORY" -type f -name "Weather.md" finds all files named Weather.md in the base directory and its subdirectories.
  3. Check for Existing Front Matter:

    • head -n 1 "$FILE" reads the first line of the file.
    • If the first line is not +++, it indicates that the file does not already contain front matter.
  4. Get File Creation and Modification Dates:

    • stat -c %w "$FILE" gets the file creation date. If this is not supported, stat -c %y "$FILE" gets the last modification date as a fallback.
    • stat -c %y "$FILE" gets the last modification date.
    • The creation date is formatted to match the front matter requirements.
  5. Define Front Matter:

    • The front matter is defined with the file creation date and time.
  6. Insert Front Matter:

    • A temporary file is created with the front matter and the original content.
  7. Preserve Timestamps:

    • stat -c %x "$FILE" gets the original access time.
    • stat -c %y "$FILE" gets the original modification time.
  8. Overwrite Original File:

    • The original file is overwritten with the new content.
  9. Restore Timestamps:

    • touch -d "$ORIGINAL_ATIME" "$FILE" restores the original access time.
    • touch -d "$ORIGINAL_MTIME" "$FILE" restores the original modification time.

Save this script as insert_front_matter.sh, make it executable with chmod +x insert_front_matter.sh, and run it to process the Weather.md files in the specified directory while preserving their original metadata.

We’ll use the original creation/modification datetime for the front matter as well.

#!/bin/bash

# Define the base directory
BASE_DIRECTORY="/path/to/directory"

# Find all files named Weather.md in the base directory and its subdirectories
find "$BASE_DIRECTORY" -type f -name "Weather.md" | while read -r FILE; do
    # Check if the file already contains front matter
    FIRST_LINE=$(head -n 1 "$FILE")
    if [[ "$FIRST_LINE" != "+++" ]]; then
        # Get the file creation and modification dates
        CREATION_DATE=$(stat -c %w "$FILE" 2>/dev/null || stat -c %y "$FILE")
        MODIFICATION_DATE=$(stat -c %y "$FILE")
        CREATION_DATETIME=$(date -d "$CREATION_DATE" +"%Y-%m-%dT%H:%M:%S%z")
        
        # Define the front matter with the creation date and time
        FRONT_MATTER="+++
title = 'Weather'
date = \"$CREATION_DATETIME\"
draft = false
tags = ['weather', 'automatic']
+++"

        # Create a temporary file with the front matter and the original content
        TEMP_FILE="${XDG_RUNTIME_DIR:-/tmp}/temp_weather.md"
        echo -e "$FRONT_MATTER\n$(cat "$FILE")" > "$TEMP_FILE"
        
        # Preserve the original timestamps
        ORIGINAL_ATIME=$(stat -c %x "$FILE")
        ORIGINAL_MTIME=$(stat -c %y "$FILE")
        
        # Overwrite the original file with the new content
        mv "$TEMP_FILE" "$FILE"
        
        # Restore the original timestamps
        touch -d "$ORIGINAL_ATIME" "$FILE"
        touch -d "$ORIGINAL_MTIME" "$FILE"
    fi
done

Explanation:

  1. Get File Creation and Modification Dates:

    • CREATION_DATE and MODIFICATION_DATE are retrieved using stat.
    • CREATION_DATETIME is formatted to match the required front matter format.
  2. Define Front Matter:

    • The front matter includes the CREATION_DATETIME as the date.

The rest of the script remains the same, ensuring the front matter is inserted correctly and the original file metadata is preserved. Save this script as insert_front_matter.sh, make it executable with chmod +x insert_front_matter.sh, and run it to process the Weather.md files in the specified directory while preserving their original metadata and using the correct date in the front matter.