Backup text file using date string
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to append a date string to the file name to back up a text file
So I did:
cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"
But its not working, how can I append the output of "date" in a string?
user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
20171222_075003
user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
date +%Y%m%d_%H%M%S
shell-script
add a comment |Â
up vote
1
down vote
favorite
I am trying to append a date string to the file name to back up a text file
So I did:
cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"
But its not working, how can I append the output of "date" in a string?
user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
20171222_075003
user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
date +%Y%m%d_%H%M%S
shell-script
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to append a date string to the file name to back up a text file
So I did:
cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"
But its not working, how can I append the output of "date" in a string?
user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
20171222_075003
user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
date +%Y%m%d_%H%M%S
shell-script
I am trying to append a date string to the file name to back up a text file
So I did:
cp "$infile" "$infile"_backup_ date +"%Y%m%d_%H%M%S"
But its not working, how can I append the output of "date" in a string?
user1@serv01:~/test_sh_append> date +"%Y%m%d_%H%M%S"
20171222_075003
user1@serv01:~/test_sh_append> echo date +"%Y%m%d_%H%M%S"
date +%Y%m%d_%H%M%S
shell-script
edited Dec 22 '17 at 16:00
asked Dec 22 '17 at 15:55
user648026
1084
1084
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Use the following:
cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")
$infile
- variable$infile
interpolated within concatenated string$(date +"%Y%m%d_%H%M%S")
- command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)
If the date
format specifier would contain whitespace(s) like %Y%m%d %H%M%S
- wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with thisdate
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g."$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the$(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
You might wantcp -p
instead of plaincp
to preserve the metadata such as timestamps and permissions.
â roaima
Dec 23 '17 at 15:11
add a comment |Â
up vote
1
down vote
I have done by below method
i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i
where i is variable which contains date command output
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Use the following:
cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")
$infile
- variable$infile
interpolated within concatenated string$(date +"%Y%m%d_%H%M%S")
- command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)
If the date
format specifier would contain whitespace(s) like %Y%m%d %H%M%S
- wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with thisdate
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g."$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the$(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
You might wantcp -p
instead of plaincp
to preserve the metadata such as timestamps and permissions.
â roaima
Dec 23 '17 at 15:11
add a comment |Â
up vote
3
down vote
accepted
Use the following:
cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")
$infile
- variable$infile
interpolated within concatenated string$(date +"%Y%m%d_%H%M%S")
- command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)
If the date
format specifier would contain whitespace(s) like %Y%m%d %H%M%S
- wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with thisdate
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g."$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the$(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
You might wantcp -p
instead of plaincp
to preserve the metadata such as timestamps and permissions.
â roaima
Dec 23 '17 at 15:11
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Use the following:
cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")
$infile
- variable$infile
interpolated within concatenated string$(date +"%Y%m%d_%H%M%S")
- command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)
If the date
format specifier would contain whitespace(s) like %Y%m%d %H%M%S
- wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"
Use the following:
cp "$infile" "$infile_backup_"$(date +"%Y%m%d_%H%M%S")
$infile
- variable$infile
interpolated within concatenated string$(date +"%Y%m%d_%H%M%S")
- command substitution, the output of which becomes the ending part of the resulting concatenated string (new filename)
If the date
format specifier would contain whitespace(s) like %Y%m%d %H%M%S
- wrap the entire concatenated sequence with double quotes "$infile_backup_$(date +"%Y%m%d %H%M%S")"
edited Dec 23 '17 at 8:49
answered Dec 22 '17 at 16:00
RomanPerekhrest
22.4k12145
22.4k12145
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with thisdate
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g."$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the$(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
You might wantcp -p
instead of plaincp
to preserve the metadata such as timestamps and permissions.
â roaima
Dec 23 '17 at 15:11
add a comment |Â
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with thisdate
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g."$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the$(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.
â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
You might wantcp -p
instead of plaincp
to preserve the metadata such as timestamps and permissions.
â roaima
Dec 23 '17 at 15:11
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
Thank you! This works! can you explain the magic?
â user648026
Dec 22 '17 at 16:06
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
@user648026, yes, see the explanation
â RomanPerekhrest
Dec 22 '17 at 16:09
+1. But...while this is safe enough with this
date
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the $(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.â cas
Dec 23 '17 at 6:07
+1. But...while this is safe enough with this
date
command using that format string, as a general rule it is better to wrap the entire string in double quotes. e.g. "$infile_backup_$(date +"%Y%m%d_%H%M%S")"
. This works correctly even though it looks like the double-quotes are nested, because the $(date...)
is in a completely new/separate quoting context. If the format contained any whitespace characters or other shell metacharacters then quoting it as I've mentioned would be required.â cas
Dec 23 '17 at 6:07
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
@cas, ok, added notation
â RomanPerekhrest
Dec 23 '17 at 8:49
1
1
You might want
cp -p
instead of plain cp
to preserve the metadata such as timestamps and permissions.â roaima
Dec 23 '17 at 15:11
You might want
cp -p
instead of plain cp
to preserve the metadata such as timestamps and permissions.â roaima
Dec 23 '17 at 15:11
add a comment |Â
up vote
1
down vote
I have done by below method
i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i
where i is variable which contains date command output
add a comment |Â
up vote
1
down vote
I have done by below method
i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i
where i is variable which contains date command output
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I have done by below method
i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i
where i is variable which contains date command output
I have done by below method
i=`date +%Y%m%d_%H%M%S`; cp inputfile inputfile_$i
where i is variable which contains date command output
answered Dec 23 '17 at 8:42
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412540%2fbackup-text-file-using-date-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password