Saturday 29 October 2011

How to create Unix files with the date and time appended to the filename


touch filename-`date '+%Y-%m-%d'`  creates a file: filename-2009-05-06
touch filename-`date +"%Y%m%d%H%M%S"` creates a file:  filename-20090506142053
touch filename- `date +"%Y%m%d%H%M%S"` creates a file: filename-2009-05-06-152446
touch filename-`date +"%Y-%m-%d-%H:%M:%S"`  creates a file: filename-2009-05-06-15:36:36



You can also set an alias as follows in tcsh:
alias cdate 'date +"%Y-%m-%d-%H:%M:%S"'
Then you call it as follows:
touch filename-`cdate`
You can also set this to a Unix variable as follows:
setenv cdate `date +"%Y-%m-%d-%H:%M:%S"`
You can use it as follows:
touch filename-$cdate

No comments:

Post a Comment