How to make new file with date in it?











up vote
2
down vote

favorite












I can create a new file and put the date in it.



touch example.txt
date >> example.txt


But I must create the file with the date in it the moment I created the new file.
How do I do that with only one command?










share|improve this question




















  • 3




    touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
    – rexkogitans
    Dec 7 at 19:18

















up vote
2
down vote

favorite












I can create a new file and put the date in it.



touch example.txt
date >> example.txt


But I must create the file with the date in it the moment I created the new file.
How do I do that with only one command?










share|improve this question




















  • 3




    touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
    – rexkogitans
    Dec 7 at 19:18















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I can create a new file and put the date in it.



touch example.txt
date >> example.txt


But I must create the file with the date in it the moment I created the new file.
How do I do that with only one command?










share|improve this question















I can create a new file and put the date in it.



touch example.txt
date >> example.txt


But I must create the file with the date in it the moment I created the new file.
How do I do that with only one command?







command-line date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 at 16:54









PerlDuck

5,14911231




5,14911231










asked Dec 7 at 16:43









Nonkocka

91




91








  • 3




    touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
    – rexkogitans
    Dec 7 at 19:18
















  • 3




    touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
    – rexkogitans
    Dec 7 at 19:18










3




3




touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
– rexkogitans
Dec 7 at 19:18






touch updates the modification time of a file, or creates the file if it does not exist. Since the file would be created with the redirection >> or > anyway, this command is unnecessary here.
– rexkogitans
Dec 7 at 19:18












2 Answers
2






active

oldest

votes

















up vote
15
down vote













Simply use the date command to redirect into file and it will be created:



date > example.txt


A prefered way would using the >> append operator:



date >> example.txt


Both the redirection operator (>) and the append operator (>>) will create the target file if it doesn't exist. You never need to create it first and write to it later.






share|improve this answer



















  • 7




    Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
    – Random832
    Dec 7 at 17:42










  • I know that and the edit by terdon has clearly stated this!
    – George Udosen
    Dec 7 at 17:44






  • 5




    The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
    – Random832
    Dec 7 at 17:45






  • 1




    Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
    – George Udosen
    Dec 7 at 17:53










  • @GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
    – kasperd
    Dec 8 at 0:10


















up vote
0
down vote













It's worth noting that, if the problem with




I must create the file with the date in it the moment I created the new file




is due to race conditions (e.g. there's a process periodically scanning for a file with that name, and expects to find a date in there), even doing



date > example.txt


isn't correct, as there's still a very small window between when the shell opens the file and when date actually writes its stuff (which may also be written non-atomically as well).



In that case, the solution is to write to a separate file and then perform a mv to the correct file name:



date > example.txt.tmp
mv example.txt.tmp example.txt


A move on the same file system is guaranteed to be atomic, so when example.txt appears, it already contains the expected content.





If instead the question is just about typing a single command, the original solution is of course the correct and most straightforward one.






share|improve this answer





















  • The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
    – ckujau
    Dec 11 at 0:13










  • @ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
    – Matteo Italia
    Dec 11 at 0:23











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1099220%2fhow-to-make-new-file-with-date-in-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
15
down vote













Simply use the date command to redirect into file and it will be created:



date > example.txt


A prefered way would using the >> append operator:



date >> example.txt


Both the redirection operator (>) and the append operator (>>) will create the target file if it doesn't exist. You never need to create it first and write to it later.






share|improve this answer



















  • 7




    Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
    – Random832
    Dec 7 at 17:42










  • I know that and the edit by terdon has clearly stated this!
    – George Udosen
    Dec 7 at 17:44






  • 5




    The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
    – Random832
    Dec 7 at 17:45






  • 1




    Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
    – George Udosen
    Dec 7 at 17:53










  • @GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
    – kasperd
    Dec 8 at 0:10















up vote
15
down vote













Simply use the date command to redirect into file and it will be created:



date > example.txt


A prefered way would using the >> append operator:



date >> example.txt


Both the redirection operator (>) and the append operator (>>) will create the target file if it doesn't exist. You never need to create it first and write to it later.






share|improve this answer



















  • 7




    Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
    – Random832
    Dec 7 at 17:42










  • I know that and the edit by terdon has clearly stated this!
    – George Udosen
    Dec 7 at 17:44






  • 5




    The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
    – Random832
    Dec 7 at 17:45






  • 1




    Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
    – George Udosen
    Dec 7 at 17:53










  • @GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
    – kasperd
    Dec 8 at 0:10













up vote
15
down vote










up vote
15
down vote









Simply use the date command to redirect into file and it will be created:



date > example.txt


A prefered way would using the >> append operator:



date >> example.txt


Both the redirection operator (>) and the append operator (>>) will create the target file if it doesn't exist. You never need to create it first and write to it later.






share|improve this answer














Simply use the date command to redirect into file and it will be created:



date > example.txt


A prefered way would using the >> append operator:



date >> example.txt


Both the redirection operator (>) and the append operator (>>) will create the target file if it doesn't exist. You never need to create it first and write to it later.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 8 at 5:41

























answered Dec 7 at 16:55









George Udosen

19.1k94266




19.1k94266








  • 7




    Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
    – Random832
    Dec 7 at 17:42










  • I know that and the edit by terdon has clearly stated this!
    – George Udosen
    Dec 7 at 17:44






  • 5




    The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
    – Random832
    Dec 7 at 17:45






  • 1




    Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
    – George Udosen
    Dec 7 at 17:53










  • @GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
    – kasperd
    Dec 8 at 0:10














  • 7




    Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
    – Random832
    Dec 7 at 17:42










  • I know that and the edit by terdon has clearly stated this!
    – George Udosen
    Dec 7 at 17:44






  • 5




    The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
    – Random832
    Dec 7 at 17:45






  • 1




    Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
    – George Udosen
    Dec 7 at 17:53










  • @GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
    – kasperd
    Dec 8 at 0:10








7




7




Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
– Random832
Dec 7 at 17:42




Note that > will clear the file if it does exist, whereas >> as in the OP's original sequence will add the date to the end of the file if it already exists.
– Random832
Dec 7 at 17:42












I know that and the edit by terdon has clearly stated this!
– George Udosen
Dec 7 at 17:44




I know that and the edit by terdon has clearly stated this!
– George Udosen
Dec 7 at 17:44




5




5




The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
– Random832
Dec 7 at 17:45




The edit does not seem clear in that aspect to me. I just thought it's worth mentioning because even though the OP asks about creating a new file, their existing sequence will not destroy the data in an existing file if something goes wrong.
– Random832
Dec 7 at 17:45




1




1




Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
– George Udosen
Dec 7 at 17:53




Note that OP said "create file at the same time" so it's a new file that needs a date entry before use so > should be enough!
– George Udosen
Dec 7 at 17:53












@GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
– kasperd
Dec 8 at 0:10




@GeorgeUdosen I prefer >> when I know the file is not supposed to exist. It's easier to recover from a mistaken use of >> than to recover from a mistaken use of >.
– kasperd
Dec 8 at 0:10












up vote
0
down vote













It's worth noting that, if the problem with




I must create the file with the date in it the moment I created the new file




is due to race conditions (e.g. there's a process periodically scanning for a file with that name, and expects to find a date in there), even doing



date > example.txt


isn't correct, as there's still a very small window between when the shell opens the file and when date actually writes its stuff (which may also be written non-atomically as well).



In that case, the solution is to write to a separate file and then perform a mv to the correct file name:



date > example.txt.tmp
mv example.txt.tmp example.txt


A move on the same file system is guaranteed to be atomic, so when example.txt appears, it already contains the expected content.





If instead the question is just about typing a single command, the original solution is of course the correct and most straightforward one.






share|improve this answer





















  • The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
    – ckujau
    Dec 11 at 0:13










  • @ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
    – Matteo Italia
    Dec 11 at 0:23















up vote
0
down vote













It's worth noting that, if the problem with




I must create the file with the date in it the moment I created the new file




is due to race conditions (e.g. there's a process periodically scanning for a file with that name, and expects to find a date in there), even doing



date > example.txt


isn't correct, as there's still a very small window between when the shell opens the file and when date actually writes its stuff (which may also be written non-atomically as well).



In that case, the solution is to write to a separate file and then perform a mv to the correct file name:



date > example.txt.tmp
mv example.txt.tmp example.txt


A move on the same file system is guaranteed to be atomic, so when example.txt appears, it already contains the expected content.





If instead the question is just about typing a single command, the original solution is of course the correct and most straightforward one.






share|improve this answer





















  • The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
    – ckujau
    Dec 11 at 0:13










  • @ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
    – Matteo Italia
    Dec 11 at 0:23













up vote
0
down vote










up vote
0
down vote









It's worth noting that, if the problem with




I must create the file with the date in it the moment I created the new file




is due to race conditions (e.g. there's a process periodically scanning for a file with that name, and expects to find a date in there), even doing



date > example.txt


isn't correct, as there's still a very small window between when the shell opens the file and when date actually writes its stuff (which may also be written non-atomically as well).



In that case, the solution is to write to a separate file and then perform a mv to the correct file name:



date > example.txt.tmp
mv example.txt.tmp example.txt


A move on the same file system is guaranteed to be atomic, so when example.txt appears, it already contains the expected content.





If instead the question is just about typing a single command, the original solution is of course the correct and most straightforward one.






share|improve this answer












It's worth noting that, if the problem with




I must create the file with the date in it the moment I created the new file




is due to race conditions (e.g. there's a process periodically scanning for a file with that name, and expects to find a date in there), even doing



date > example.txt


isn't correct, as there's still a very small window between when the shell opens the file and when date actually writes its stuff (which may also be written non-atomically as well).



In that case, the solution is to write to a separate file and then perform a mv to the correct file name:



date > example.txt.tmp
mv example.txt.tmp example.txt


A move on the same file system is guaranteed to be atomic, so when example.txt appears, it already contains the expected content.





If instead the question is just about typing a single command, the original solution is of course the correct and most straightforward one.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 7 at 22:18









Matteo Italia

153118




153118












  • The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
    – ckujau
    Dec 11 at 0:13










  • @ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
    – Matteo Italia
    Dec 11 at 0:23


















  • The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
    – ckujau
    Dec 11 at 0:13










  • @ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
    – Matteo Italia
    Dec 11 at 0:23
















The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
– ckujau
Dec 11 at 0:13




The date > ... still isn't atomic, so that additional mv step still doesn't help the race condition. Example: date +%Y-%m-%d %H:%M:%S.%N > foo && stat foo && cat foo.
– ckujau
Dec 11 at 0:13












@ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
– Matteo Italia
Dec 11 at 0:23




@ckujau: I don't see what's your point... of course date > ... isn't atomic - I even said that ("which may also be written non-atomically as well"). The rename of course is effective only if the other program looks specifically for the final filename, not just any file in the directory.
– Matteo Italia
Dec 11 at 0:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1099220%2fhow-to-make-new-file-with-date-in-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido