How to Append Date and Time to Log File in Linux

If we want to get detailed log report, we can store multiple log files with date and time information on the file name 🙂

We can do this command in Linux :

your_command_here > /path_1/path_2/your_log_$(date +"%Y-%m-%d_%H-%M-%S").txt

For example :

echo abcdefg > /path_1/path_2/your_log_$(date +"%Y-%m-%d_%H-%M-%S").txt

And you will get a log file with date and time information within the file name 🙂

If you want to use the command within cron job, you must escape every % with backslash.
So the cron command would be :

echo abcdefg > /path_1/path_2/your_log_$(date +"\%Y-\%m-\%d_\%H-\%M-\%S").txt

Leave a comment

Your email address will not be published.