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?

  • 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

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.

  • 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

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.

  • 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

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.