Issue
I would like to append a line including date of file modification (preferably just the year) to all .m files.
To append variable text to all .sh files:
find . -name "*.m" -exec sed -i '$a \\n# File last modified in '"$date"'' {} \;
Could I get the year for each file and insert it into the file contents in the same line?
Solution
This find
one-liner might be what you want:
find -name '*.sh' -exec bash -c 'printf "# %(%Y)T\n" $(stat -c %Y "$1") >> "$1"' _ {} \;
assuming you have stat
from GNU coreutils.
A few notes:
The bash
syntax to execute a command string is bash -c command_string arg_0 arg_1 ... arg_n
(args are optional). arg_0
corresponds to bash special parameter $0
, arg_1
corresponds to bash special parameter $1
, and so on, within the command string. For each file the find
finds, the {}
is replaced with the pathname and the bash command is executed. The string _
is assigned to $0
(it normally stands for the name of the shell or shell script, which doesn't interest us here because we're running a command string, not a file), and the pathname is assigned to the $1
, then the command printf "# %(%Y)T\n" $(stat -c %Y "$1") >> "$1"
is executed. stat -c %Y
prints out the time of last data modification as seconds since Epoch and the format specification %(%Y)T
in printf
builtin of bash
interprets this value as a year.
Answered By - M. Nejat Aydin Answer Checked By - Mildred Charles (WPSolving Admin)