Skip to content

Instantly share code, notes, and snippets.

@jmhublar
Last active March 5, 2023 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhublar/c405d38becabbda538fa68ac2da74c57 to your computer and use it in GitHub Desktop.
Save jmhublar/c405d38becabbda538fa68ac2da74c57 to your computer and use it in GitHub Desktop.
gist

File Operations

File Sync Script

Prompt

write some bash that can be used as a cronjob. The script will take two arguments: A filepath for origin file and a filepath of a destination file. Use a sha hash to compare the files. If there is a difference, write the origin file contents to the destination file. Create a comment block that explains the operation.

Output

Here's a sample bash script that can be used as a cronjob, which takes two arguments - a filepath for origin file and a filepath of a destination file. It uses a sha hash to compare the files and if there is a difference, it writes the origin file contents to the destination file.

#!/bin/bash

################################################################################
# Script:      File Sync Script
# Description: This script compares the sha hash of two files and if there is a
#              difference, writes the origin file contents to the destination file.
# Arguments:   arg1 - origin file path
#              arg2 - destination file path
################################################################################

ORIGIN_FILE=$1
DESTINATION_FILE=$2

# Get the sha256 hash of the origin file
ORIGIN_HASH=$(sha256sum "$ORIGIN_FILE" | awk '{print $1}')

# Get the sha256 hash of the destination file
if [ -f "$DESTINATION_FILE" ]; then
  DESTINATION_HASH=$(sha256sum "$DESTINATION_FILE" | awk '{print $1}')
else
  DESTINATION_HASH=""
fi

# Compare the sha256 hash of the origin file with the destination file
if [ "$ORIGIN_HASH" != "$DESTINATION_HASH" ]; then
  # Copy the origin file to the destination file
  cp "$ORIGIN_FILE" "$DESTINATION_FILE"
  echo "File copied from $ORIGIN_FILE to $DESTINATION_FILE"
else
  echo "No changes found"
fi

You can save this script to a file (e.g., file_sync.sh), make it executable (chmod +x file_sync.sh), and run it as a cronjob by adding an entry to your crontab file (crontab -e):

* * * * * /path/to/file_sync.sh /path/to/origin_file /path/to/destination_file

Markdown Gist Updater Script

Prompt

write a script that can be executed as a cronjob. The script should list all markdown files in a subfolder have a property named gist defined in the top matter. For each file with such a defined property that is not null, apply the command gist -u [gist property value] [filepath]

Output

Sure! Here's a script that you can execute as a cronjob to list all markdown files in a subfolder that have a property named gist defined in the YAML front matter. For each file with such a defined property that is not null, the script will apply the gist -u command to update the specified Gist with the contents of the file.

#!/bin/bash

# Markdown Gist Updater Script
# This script lists all markdown files in a subfolder that have a property named gist defined in the YAML front matter.
# For each file with such a defined property that is not null, the script will apply the gist -u command to update the specified Gist with the contents of the file.

# Change directory to the subfolder containing the markdown files
cd /path/to/markdown/files

# Find all markdown files with a YAML front matter that contains a "gist" property
find . -type f -name "*.md" -print0 | xargs -0 grep -l '^---$.*gist:' | while read file; do
  # Get the Gist ID from the YAML front matter
  gist_id=$(awk '/^gist:/ {print $2}' "$file")

  # Check if the Gist ID is not null
  if [ -n "$gist_id" ]; then
    # Apply the gist -u command to update the specified Gist with the contents of the file
    gist -u "$gist_id" "$file"
    echo "File $file updated in Gist $gist_id"
  fi
done

Here's how this script works:

  • Change directory to the subfolder containing the markdown files.
  • Use the find command to find all markdown files with a YAML front matter that contains a "gist" property.
  • Use xargs and grep to extract the filenames of the matching files.
  • Loop through each matching file and extract the Gist ID from the YAML front matter using awk.
  • Check if the Gist ID is not null.
  • If the Gist ID is not null, apply the gist -u command to update the specified Gist with the contents of the file.
  • Echo a message to the console to indicate that the file has been updated in the specified Gist.

To execute this script as a cronjob, you can add the following line to your crontab file:

*/5 * * * * /path/to/markdown_gist_updater.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment