0

This is on a Debian system.

I have had the compression of files and contents into individual folders working fine but not with the date added to the file names. Removing the date part, it works as it should.

I'm pretty sure it's something to do with syntax as I've based it on a couple of examples I came across...

Here's the relevant code:

for i in */; do tar -zcvf "$i $(date '+%y-%m-%d').tar.gz" "$i"; done

1 Answer 1

0

It looks to me like the "$i... bit is the problem - and the space character probably doesn't help either. $i is getting the "/" at the end of the path, and this is most likely the cause of your problems. I posit the following will solve your problem (the {i::-1} removes the last character :

for i in */; do tar -zcvf ${i::-1}-$(date '+%y-%m-%d').tar.gz "$i"; done
3
  • Thanks for your answer. It doesn't seem to work for me - there is mention of files with .tar.gz but they don't seem to exist.
    – winder28
    Feb 23, 2016 at 22:31
  • Try now. I left a debugging "echo" statement which would have forced the command to display but not run.
    – davidgo
    Feb 23, 2016 at 22:37
  • You removed double-quotes from around the option-argument to -f. If $i contains space character(s), the command will fail because tar will try to archive nonexistent files. The feedback from the OP may fit this scenario. Regardless of the original culprit maybe you sabotaged your solution by dropping the quotes. Jul 17, 2019 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .