-4

How can I gzip this file? I get an error:

find /users/tnea01/logfile10.log -type f -exec sh -c \ 'gunzip /users/tnea01/logfile_archive/$(basename $0)_$(date -r {} +%F).gz $0' {} \;

Here is the error I get:

gzip: /users/tnea01/logfile10.tar.gz: No such file or directory gzip: /users/tnea01/logfile10.log: unknown suffix -- ignored

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
connollyc4
  • 155
  • 1
  • 3
  • 15

1 Answers1

1

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"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thank you Charles Duffy ! There are actually 2 files that need to be backed up so I used your first suggesting that worked perfectly. I have one more question which is no big deal if it requires too much work. My current log file is called logfile.log.10 which then rolls off and gets deleted. is it possible to modify it so it only says logfile.log + last modified date.gz? Basically using the current log file name minus the .10 at the end. Thank you again!!! – connollyc4 Jul 29 '16 at 19:33
  • @connollyc4, by "last modified date" you mean looking at which logfiles exist in the directory, finding the latest one that actually exists, and expanding to that? You'd want to follow BashFAQ #3 for identifying that file; see http://mywiki.wooledge.org/BashFAQ/003 – Charles Duffy Jul 29 '16 at 19:43
  • Sorry I think I didn't explain that right. The log file is called "logfile.log.10 which I want to use the same script as your first suggestion with the find command but rename it to logfile.log + the date and time as it already does with date -r "$f" +%F).gz" – connollyc4 Jul 29 '16 at 19:51
  • so that's to say that the change from current behavior is you want to strip the `.10` before appending the suffix? – Charles Duffy Jul 29 '16 at 19:55
  • exactly, so the current log file is called "logfile.log.10 while the script you provided would call it "logfile.log.10-2016-07-29.gz" which I was hoping to strip the .10 suffix and have it named logfile.log.-2016-07-29.gz. – connollyc4 Jul 29 '16 at 20:04
  • so, that would be something like `f_base=${f##*/}; f_base=${f_base%%.[[:digit:]]*}; gzip -c <"$f" >"$d/${f_base}_$(date -r "$f" +%F).gz"` – Charles Duffy Jul 29 '16 at 20:07
  • ...and, to be clear, everything I just used in the above comment is explained in the link I gave in the answer, http://wiki.bash-hackers.org/syntax/pe. You *did* follow that link, right? :) – Charles Duffy Jul 29 '16 at 20:07