Search code examples
linuxwindowsbashunixcygwin

Creating timestamps for "echo" outputs to file


I'm trying to create a timestamp for the information produced from the below .sh file when it is ran and the "echo"'s are performed. What is the best way to go about doing this?

echo "Target File" >> /cygdrive/c/FilePath/Log.txt
if [ -f //TargetDirectory/TargetFile ]
then
    echo //TargetDirectory/TargetFile is present. >> /cygdrive/c/FilePath/Log.txt
else
    echo //TargetDirectory/TargetFile is missing. >> /cygdrive/c/FilePath/Log.txt
fi
echo -e "\n" >> /cygdrive/c/FilePath/Log.txt

Solution

  • echo () {
        builtin echo "$(date +'[%m-%d %H:%M:%S]'):" "$@"
    }
    

    Just override echo.