how to append current date to filename.tgz in perl

i would like to know how to append current date in a filename with .tgz extension.

#!/usr/bin/perl
my $date = `date + %Y%m%d`;

system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz");

im getting this error message:

sh: line 1: .tgz: command not found

just print the $date and make sure you have the date in the $date varibale

my $date = `date + %Y%m%d`;
print $date;

seems $date is not filled

dont give space after + sign

date +%Y%m%d

thanks but im still getting the same error message

 
bash-3.00$ date + %Y%m%d
 
bash-3.00$ date +%Y%m%d
20110616

dont give space after + symbol

did u print $date ?

yup.. actually that's not my problem. the issue here is in ff code:

system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz");

Thanks!

the problem is $date has \n (new line) character

 
bash-3.00$ /tmp/myfile
/tmp/nyucs01_config_backup.tgz
/misc/nyucs01_config_backup_20110616.tgz

bash-3.00$ cat /tmp/myfile
#!/usr/bin/perl
my $date=`date +%Y%m%d`;
chomp($date);
my $source_file="/tmp/nyucs01_config_backup.tgz";
my $destination_file="/misc/nyucs01_config_backup_" . $date . ".tgz";
print "$source_file\n";
print "$destination_file\n";
system("sudo mv /tmp/nyucs01_config_backup.tgz /misc/nyucs01_config_backup_$date.tgz");

use chomp($date); to remove the new line character :slight_smile:

1 Like

thanks... its working now...

by the way, whats the use of chomp ?

chomp will remove the new line character at the end of the variable.

Using the Perl chomp() function