Tuesday, January 31, 2006

Shell Script - Appending a Timestamp to a Filename

This case is most useful for a somewhat simple script that will be invoked once at an arbitrary time and complete promptly. If the script will keep running (in the background, say) make sure the variable declaration is inside any loops.

Define the variable ${NOW}:
NOW=`date +"%Y%m%d-%H%M%S"`
If we invoke this as in this example:
netstat -an >> /logs/netstat.out.${NOW}

It will create a datestamp that looks like the following:
netstat.out.20051029-124116

No comments: