If you didn't know the exact filename, you might do something like this:
find /users/tnea01 -maxdepth 1 -name '*.log' -type f -exec sh -c \
'for f; do
gzip -c <"$f" >"/users/tnea01/logfile_archive/${f##*/}_$(date -r "$f" +%F).gz"
done' _ {} +
To explain the moving pieces:
- The only secure way to use
sh -c
is with a completely constant string; substituting variables into it (including filenames) creates security vulnerabilities. Thus, we don't use any kind of replacement facility in the code, but pass the filename(s) as extra arguments.
for f; do
is the same as for f in "$@"; do
-- it iterates over all command line arguments.
${f**#/}
evaluates to everything after the last /
in $f
; see the bash-hackers page on parameter expansion.
- Expansions, including
$(date ...)
, need to be inside a double-quoted context to be safe; here, we're putting the entire destination filename in such quotes.
However, since you do, that's all entirely needless.
f=/users/tnea01/logfile10.log
d=/users/tnea01/logfile_archive
gzip -c <"$f" >"$d/${f##*/}_$(date -r "$f" +%F).gz"