0

I'm trying to compress a directory but I want to change the tar file name to have the current date. The problem is that tar doesn't accept:

#!/bin/bash
tar -cvjSf $(date +%d/%m/%y.%HH:%MM)home_backup.tar.bz2 /home

I want to make a compressed File with bzip2 with the actual date but the name is not accepted. It only works if I use a simple name like:

#!/bin/bash
tar -cvjSf home.tar.bz2 /home
John1024
  • 109,961
  • 14
  • 137
  • 171

1 Answers1

2

Don't put : or / in the name of the tar file.

Try:

tar -cvjSf "$(date +%d-%m-%y.%HH.%MM)home_backup.tar.bz2" /home

Notes:

  1. In Unix, / means directory. The expansion of $(date +%d/%m/%y.%HH:%MM)home_backup.tar.bz2 will contain two / and tar would want to create the file in specified subdirectory. In the command above, we replaced / with - and the problem is avoided.

  2. tar treats the part of a file name that precedes : as the name of a remote host. Since you are not trying to send the file to a remote host, all : should be removed from the date command that is used to create the file name. In the command above, we replaced : with . and the problem is avoided.

  3. The command above shows the name of the tar file inside double-quotes. With the specific command shown above, this is not necessary. The use of double-quotes, however, prevents word-splitting and this may save you from unpleasant surprises in the future.

John1024
  • 109,961
  • 14
  • 137
  • 171