Skip to content

Instantly share code, notes, and snippets.

@pd76
Created November 9, 2024 01:03
Show Gist options
  • Save pd76/75639931a2e0fb7403842a8ff4891e79 to your computer and use it in GitHub Desktop.
Save pd76/75639931a2e0fb7403842a8ff4891e79 to your computer and use it in GitHub Desktop.
Quickly and securely bulk-redact text documents in Linux
#!/bin/bash
# Check if a directory path was provided as an argument
if [ -z "$1" ]; then
echo "Please provide a directory path as an argument."
exit 1
fi
# Define the words to redact:
redact_words=("Cat" "Dog" "Horse")
# WHat you want to replace those words with:
replacement=("REDACTED")
# Function to recursively process files in a directory
process_directory() {
local dir="$1"
local changed_files=()
# Iterate through all files and directories in the current directory
for item in "$dir"/*; do
if [ -d "$item" ]; then
# Recursively process subdirectories
process_directory "$item"
elif [ -f "$item" ]; then
# Process regular files
for word in "${redact_words[@]}"; do
if grep -q "$word" "$item"; then
# Redact the word and save the file path
sed -i "s/$word/$replacement/g" "$item"
changed_files+=("$item")
fi
done
fi
done
# Return the list of changed files
echo "${changed_files[@]}"
}
# Call the function with the provided directory path
changed_files=($(process_directory "$1"))
# Display the list of changed files
if [ ${#changed_files[@]} -eq 0 ]; then
echo "No files were changed."
else
echo "The following files were changed:"
for file in "${changed_files[@]}"; do
echo "- $file"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment