
I need to create a shell script that appends a timestamp to existing file. I mainly use Mac OS X for development. Wanted to create the same on Mac Terminal.
Here are some basics on date
command.
1 2 3 4 5 6 7 8 9 |
NAME date -- display or set date and time SYNOPSIS date [-ju] [-r seconds] [-v [+|-]val[ymwdHMS]] ... [+output_fmt] date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss] date [-jnu] -f input_fmt new_date [+output_fmt] date [-d dst] [-t minutes_west] |
Samples:
1 2 3 4 |
bash-3.2$ date Wed Sep 26 19:29:10 PDT 2012 bash-3.2$ date +"%Y/%m/%d" 2012/09/26 |
Script to append date stamp to file:
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/bin/sh file_name=test_files.txt current_time=$(date "+%Y.%m.%d-%H.%M.%S") echo "Current Time : $current_time" new_fileName=$file_name.$current_time echo "New FileName: " "$new_fileName" cp $file_name $new_fileName echo "You should see new file generated with timestamp on it.." |
Please let me know if you have some ready to use shell script and want to share with Crunchify readers.
Happy coding and keep visiting!