Naming a tar file with date Naming a tar file with date shell shell

Naming a tar file with date


The problem is the : in the file name, which is treated specially by GNU tar.

On my own system, where I have a directory named foo, I get this:

$ tar zcf foo:42.tar.gz footar (child): Cannot connect to foo: resolve failedtar: Child returned status 128tar: Error is not recoverable: exiting now$ 

There doesn't seem to be any way to escape the : character -- but you can inhibit the special interpretation with the --force-local option (thanks to Daniel Serodio for pointing this out).

Colon characters in file names, though they're legal, are typically a bad idea anyway. For example, you probably wouldn't be able to access the file via scp.

Here's the relevant section from the GNU tar manual (info tar and go to section 6.1):

To specify an archive file on a device attached to a remote machine, use the following:

--file=hostname:/dev/file-name

tar will set up the remote connection, if possible, and prompt you for a username and password. If you use --file=@hostname:/dev/file-name, tar will attempt to set up the remote connection using your username as the username on the remote machine.

When I make file names that include a timestamp, I usually use YYYY-MM-DD-HHMMSS

$(date +%F-%H%M%S)

or you can use YYYY-MM-DD_HH_MM_SS if you prefer

$(date +%F-%H_%M_%S)


Your filenames and variables are incorrect:

${imageName-$date}

Should be

${imageName}${date}

and

${imageName}:${date}.tar.gz

is going to be interpreted as an NFS location because of the : in the name, e.g. server:filename. Replace the : with a - or other non-special character.


try avoiding all colons in the tarfilename, as this will make tar attempt to output to a remote file:

 date=$( date +%Y%m%d_%H%M%S ) tar -czf ${imageName}-${date}.tar.gz ${imageBasename} 2>>${ERRORLOG}