Created
April 15, 2024 07:23
-
-
Save YohannesTz/aa50918930363b8e0b8e110452497a96 to your computer and use it in GitHub Desktop.
Simple scripts to add and remove copyright notice headers in Java and Kotlin files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define the text to add | |
copyright_text=" | |
/* | |
* Copyright (c) 2024, YohannesTz, Africa Technologies PLC //change your text here | |
* | |
*/" | |
# Find all .kt and .java files recursively in the given folder | |
find "$1" -type f \( -name "*.kt" -o -name "*.java" \) | while read -r file; do | |
# Check if the file starts with "/*" | |
if ! grep -q "^/\*" "$file"; then | |
# Add the copyright text to the beginning of the file | |
echo "$copyright_text" | cat - "$file" > temp && mv temp "$file" | |
echo "Added copyright text to $file" | |
else | |
echo "Copyright text already exists in $file" | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define the text to remove | |
copyright_text="/* | |
* Copyright (c) 2024, YohannesTz, Africa Technologies PLC | |
* | |
*/" | |
# Find all .kt and .java files recursively in the given folder | |
files=$(find "$1" -type f \( -name "*.kt" -o -name "*.java" \)) | |
# Prompt for confirmation | |
echo "This will remove the copyright text from all .kt and .java files in the directory $1 and its subdirectories." | |
read -p "This could affect your classes and could possibly result in a syntax error. Are you sure you want to continue? (y/n): " choice | |
if [[ $choice == "y" || $choice == "Y" ]]; then | |
# Loop through each file | |
for file in $files; do | |
# Check if the file starts with the copyright text | |
if grep -q "^/\*" "$file"; then | |
# Remove the copyright text from the beginning of the file | |
sed -i '1,/\*\/\$/d' "$file" | |
echo "Removed copyright text from $file" | |
else | |
echo "Copyright text not found in $file" | |
fi | |
done | |
echo "Copyright text removed from all files." | |
else | |
echo "Operation aborted." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo chmod +x ./add_copyright_notice.sh | |
$ sudo chmod +x ./remove_copyright_notice.sh | |
$ ./add_copyright_notice.sh ./your/folder | |
$ ./remove_copyright_notice.sh ./your/folder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment