This is a Script to Automate adding the Copyright text to one or more source files Recursively.
This is a Script to Automate adding the Copyright text to one or more source files Recursively. The Copyright texts get added to the top of the source file. Ice on the Cake is, it doesn’t duplicate the copyright text if it is added already!
Note: The same tool can be used to add any such text blocks like, License, File level Comments etc.
Here is an example of JavaScript(.js) file.
/*
© Copyright 2020 tapasadhikary.com or one of its affiliates.
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
*/
const test = () => {
console.log('test');
}
test();
addcopyright.sh
and few test files to test out things before you feel comfortable to try it on your source code.add-copyright
folder.The above command will set some variable value that will be used by the
export COPYRIGHTLEN=`wc -l copyright.txt | cut -f1 -d ' '`
addcopyright.sh
script.copyright.txt
to your source code:Where the
find <SOURCE_CODE_DIRECTIRY> -type d -name "<EXCLUDE_DIRECTORY>" -prune -o -name "*.js" -print0 | xargs -0 ./addcopyright.sh
<SOURCE_CODE_DIRECTIRY>
is the path of your source code.<EXCLUDE_DIRECTORY>
is the directory to exclude if it exists under <SOURCE_CODE_DIRECTIRY>
for updating the Copyright information.Make sure you have given the execute permission to the addcopyright.sh
script file. You can provide the permission using following command:
chmod +x addcopyright.sh
If your user is not having root privileges, add your $USER to ‘roots’ group using command :
useradd -m -G root $USER
Alternatively:
Insert ‘sudo’ when executing the run command. For example:
sudo find <SOURCE_CODE_DIRECTIRY> -type d -name "<EXCLUDE_DIRECTORY>" -prune -o -name "*.js" -print0 | sudo xargs -0 ./addcopyright.sh
For running the tool on the Source code without excluding any folders, use this command:
find <SOURCE_CODE_DIRECTIRY> -name "*.js" -print0 | xargs -0 ./addcopyright.sh
.
(dot)/home/atapas/code
directory, use the above command as,
find <SOURCE_CODE_DIRECTIRY> -name "*.js" -print0 | xargs -0 ./addcopyright.sh
For Running the tool on the Source Code by excluding the folder of your desire(say, node_modules for nodeJs based applications), use this command:
find /opt/atapas/code -type d -name "node_modules" -prune -o -name "*.js" -print0 | xargs -0 ./addcopyright.sh
If your source code is in /home/atapas/code
and want to exclude node_modules
and test
directories, use the above command as,
find /opt/atapas/code -type d -name "node_modules" -prune -o -type d -name "test" -prune -o -name "*.js" -print0 | xargs -0 ./addcopyright.sh
To Change the file type, just replace the *.js
with any other extentions like, *.java
, *.cpp
etc.
This tool can be made use for different language files like javascript, java, c, c++, html, shell-script etc. However the content of the copyright.txt
should be changed according to the mult-line comment format. For example,
For Javascript(.js) or Java(.java) files this is the format:
/*
This is a comment
*/
For HTML(.htm or .html) file the format should be,
<!--
This is a HTML Comment
-->
The soution was found as part of this discussion on the StackOverflow and keep improving thereafter!
Thanks goes to these wonderful people (emoji key):
IAMLEGION98 ⚠️ | Tapas Adhikary 💻 | Pradeep Repaka 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!