How to easily append a timestamp to tar archive name ?

  • Thread starter Matthew Lincoln
  • Start date
M

Matthew Lincoln

I would like to create a tar archive with the well known tar command

tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/

It works. However I would like to automatically append a timestamp whenever I call this command
in a (perl) script.

The created archive file name should look like

bkp-20071109-1523.tar.gz

Where the format is preferred as YYYYMMDD-HHMM

Does Gnu-tar offer such an automatic timestamp appending?

If not: Is there a (short) work around (in perl) which let me create such a timestamp?

Thank you
Matthew
 
M

Martin Klar

Matthew said:
I would like to create a tar archive with the well known tar command

tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/

It works. However I would like to automatically append a timestamp whenever I call this command
in a (perl) script.

The created archive file name should look like

bkp-20071109-1523.tar.gz

Where the format is preferred as YYYYMMDD-HHMM

Does Gnu-tar offer such an automatic timestamp appending?

AFAIK no.

Assuming you are using a bash you could solve it by:

tar czvf "/usr/local/user234/bkp-$(date +%Y%m%d-%H%M).tar.gz" /home/users/pmiller/

It *should* also work when called from a perl script, but I didn't test it.


HTH Martin
 
B

Ben Morrow

Quoth Martin Klar said:
Assuming you are using a bash you could solve it by:

tar czvf "/usr/local/user234/bkp-$(date +%Y%m%d-%H%M).tar.gz"
/home/users/pmiller/

It *should* also work when called from a perl script, but I didn't test it.

Not with that syntax :).

Something like

system 'tar', 'czvf', (
'/usr/local/user234/bkp-' . qx(date +%Y%m%d-%H%M) . '.tar.gz'
), '/home/users/pmiller/';

or
system sprintf 'tar cvf "/usr/.../bkp-%s.tar.gz" /home/...',
qx(date +%Y%,%d-%H%M);

would work, but it would be better to do it from within Perl using
POSIX::strftime.

Ben
 
G

Gregory Shearman

Matthew said:
I would like to create a tar archive with the well known tar command

tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/

It works. However I would like to automatically append a timestamp
whenever I call this command in a (perl) script.

The created archive file name should look like

bkp-20071109-1523.tar.gz

Where the format is preferred as YYYYMMDD-HHMM

Does Gnu-tar offer such an automatic timestamp appending?

If not: Is there a (short) work around (in perl) which let me create such
a timestamp?

Thank you
Matthew

==================================

#!/your/perl/path

use strict;
use warnings;
use POSIX qw(strftime);

my $now_string = strftime "%Y%m%d-%H%M", localtime;

my $your_filename = "bkp-".$now_string".tar.gz

==================================

man perlfunc

man strftime
 
J

John Bokma

my $your_filename = "bkp-".$now_string".tar.gz

o_O too much PHP?

my $your_filename = "bkp-$now_string.tar.gz";

or to make the scalar stand out a bit more:

my $your_filename = "bkp-${now_string}.tar.gz";

or, if you insist on . :

my $your_filename = 'bkp-' . $now_string . '.tar.gz';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top