[xubuntu-users] Using the _date_ variable in tar files?

Peter Flynn peter at silmaril.ie
Mon Jul 20 21:48:16 UTC 2015


On 07/20/2015 10:11 PM, JMZ wrote:
> Hi,
> 
> I've tried to set up a shell file to automatically create a compressed
> file of one directory in another directory.  This shell file will later
> be embedded in crontab.  What's difficult is that I can't get the _date_
> command to stamp a unique record on the tar file.  I thought something
> like this would work:
> 
> sudo tar -cjvf /x/y/(date +%a_%b_%F_%T)-b.tar.bz2 /a/b/*
> 
> That is, the contents of /a/b/* would be compressed into a file and sent
> to /x/y/.  The tar.bz2 file would be time and date stamped.
> 
> I get the following error:
> 
> bash: syntax error near unexpected token `('
> 
> Which I interpret to mean that _date_ can't be used this way when making
> tar files.

Not in parentheses. They invoke a subshell. The computed date would
vanish when the subshell terminated anyway.

> Is there a workaround for this?

It ought to work using backticks (grave accents):

  sudo tar -cjvf /x/y/`date +%a_%b_%F_%T`-b.tar.bz2 /a/b/*

On a related note, though:

1. It's A Bad Idea to use `date +%T` in filenames, as it creates colons,
which will need escaping when the filename is dereferenced (or the
filename will need putting in quotes as above). It might be safer to use
%H-%M-%S

2. It's A Very Bad Idea to format dates like this. Filenames will not be
sortable by chronological date in a directory listing. You should
probably look at putting the day name and the month name somewhere else,
later in the name than the ISO-style date.

Just my 2ยข...

///Peter






More information about the xubuntu-users mailing list