Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to figure out how to automatically insert a date and time of execution of a certain bash script file, into a txt file. Lets just say I have a bash script named backup.sh, and each time backup.sh executes, it would automatically create a txt file named Execution.txt, and within that txt file, the date and time of the execution of backup.sh would be written there. Does anyone have ideas to do this? I have Googled for some answers, but found nothing. Any help would be appreciated! Thanks in advance.

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

Simply put this inside your backup.sh file:

date > Execution.txt

This will create the file Execution.txt inside the current working directory. The file will contain the current date and time (if it already exists, it will be overwritten). Of course you may specify an absolute path to the file:

date > /absolute/path/to/Execution.txt

To append to the file (instead of overwriting it), use >> instead of >, i.e.,

date >> Execution.txt
share|improve this answer
I recommend against using absolute paths to commands. It is not more secure. setuid bit on scripts doesn't work. It is disabled. –  geirha Mar 24 at 16:56
Alright it worked, thanks! –  Jeremy Lai Wai Kit Mar 24 at 16:56
@geirha Actually I just thought of this myself; you are perfectly right. As far as Linux is concerned at least. On MacOS X for instance, things are different (setuid may be enabled for scripts). Yet, an absolute path does not make the script less secure either. And it still ensures that the intended command is indeed executed, even when the PATH is weird. Do you actually have any arguments why using an absolute path may be bad? –  Malte Skoruppa Mar 24 at 16:58
It makes the script harder to maintain. What if command foo is at /bin/foo on one system, but /usr/bin/foo at another? you'd have to go through the entire script changing it from one to the other. If you don't know what PATH is set to, set it yourself at the start of the script. –  geirha Mar 24 at 17:02
Hm, ok, fine. I guess you have a point. I reverted my last edit. Cheers and thanks for the comment. :) –  Malte Skoruppa Mar 24 at 17:05
add comment

You have to add the following line in your backup.sh script:

date >> /path/to/Execution.txt


  • date - print the system date and time
  • >> /path/to/Execution.txt redirect the output and appends it to the /path/toExecution.txt file.
share|improve this answer
It worked. Thanks for the help! –  Jeremy Lai Wai Kit Mar 24 at 16:56
add comment

If you want to record the date for one event in a script, it's likely you'll want to keep a running log. The date command can be pressed into service as a simple event logger:

date +'%c|Backup started' >> backup.log

The + option allows you to format the date anyway you chose. The details of date formatting are explained in man date. The above command will append a line to the log that looks something like:

Tue Mar 25 14:49:28 2014|Backup started

The pipe separator makes parsing the log a bit easier if you add different events:

date +"%c|Backup completed: $?" >> backup.log

$? is the return status of the last command to be executed, but you can use any variable that might be helpful for debugging later. (Note: you need to use double quotes if you want variables to be interpolated.) Looking at the log, you'd be able to discover a possible failure:

Tue Mar 25 14:49:28 2014|Backup started
Tue Mar 25 14:49:52 2014|Backup completed: 1
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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