How it exactly works
Generate a list of file names:
grep -ilr --exclude=revar.sh --exclude=README.md "[DATE]" *
Search recursively (-r) for files and list (-l) the names of those where the contents of the file match case-insensitively (-i) the regex '[DATE]' (which means that the file contains any one of the 8 characters "AaDdEeTt"); exclude the names revar.sh and README.md, and
Remove any files names from the list that contain a character followed by 'git' (so the file name 'agitator' will be removed):
grep -v .git
Process the files one at a time, applying a specific 'sed' script to the file. In the modern notation (POSIX - and MacOS X, and Linux, AIX, HP-UX, Solaris, etc):
xargs -I@ sed -i "s/\[DATE\]/${today}/g" @
The '-i' option to 'sed' means overwrite the input file(s) after processing. This is a GNU and BSD extension to 'sed' not specified in the POSIX standard.
What is implausible about it
This script can be criticized at numerous levels.
- The first search string is incorrect; it should have backslashes before the brackets; most files contain at least one of the letters 'dateDATE'.
- The second grep is presumably meant to remove anything under the '.git' directories and needs fixing.
- The 'sed' command is not case-insensitive when looking for '[DATE]', unlike the first 'grep'.
How to Fix it
So, one of two alternatives makes sense.
Either:
grep -ilr --exclude=revar.sh --exclude=README.md "\[DATE\]" * |
grep -v '\.git/' |
xargs sed -i "s/\[[Dd][Aa][Tt][Ee]\]/${today}/g"
Or:
grep -lr --exclude=revar.sh --exclude=README.md "\[DATE\]" * |
grep -v '\.git/' |
xargs sed -i "s/\[DATE\]/${today}/g"
As Donal Fellows noted, there is no need in this context to use the '-i' or '-I' option to 'xargs'.
Is it useful even when fixed?
This just leaves me puzzled as to how it useful. On the first day, all occurrences of '[DATE]' are mapped to '2010-04-17'; what happens on the next day? How do you unmap the dates before you commit to the git repository?
Still, at least you now know what it does and how it does it.