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:
Define Base Directory:
- The
BASE_DIRECTORY
variable should be set to the directory where you want to search forWeather.md
files.
- The
Find Files:
find "$BASE_DIRECTORY" -type f -name "Weather.md"
finds all files namedWeather.md
in the base directory and its subdirectories.
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.
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.
Define Front Matter:
- The front matter is defined with the file creation date and time.
Insert Front Matter:
- A temporary file is created with the front matter and the original content.
Preserve Timestamps:
stat -c %x "$FILE"
gets the original access time.stat -c %y "$FILE"
gets the original modification time.
Overwrite Original File:
- The original file is overwritten with the new content.
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:
Get File Creation and Modification Dates:
CREATION_DATE
andMODIFICATION_DATE
are retrieved usingstat
.CREATION_DATETIME
is formatted to match the required front matter format.
Define Front Matter:
- The front matter includes the
CREATION_DATETIME
as the date.
- The front matter includes the
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.