I need to create a log file using the following format:
myapp_monday.log
myapp_tuesday.log
....
...
myapp_sunday.log
How do I create log filename with day of the week in it under Linux or Unix operating systems?
You can use the date command to display current date and time. You can format a date provide a string beginning with + to get day of the week as follows:
$ date +"%A"
Where,
- +"%A" - Get weekday in full format i.e. as Tuesday
- +"%a" - Get weekday in abbreviated format i.e. as Tue
- +"%u" - Get day of week starting with Monday (1), i.e. mtwtfss
- +"%w" - Get day of week starting with Sunday (0), i.e. smtwtfs
Use the following syntax to store weekday into a shell variable:
_dow="$(date +'%A')" echo "$_dow" ## Get day of week starting with Monday (1), i.e. mtwtfss (see above for syntax) ## _dow="$(date +'%u')" echo "$_dow"
To create a filename with day of the week in it:
#!/bin/bash _dow="$(date +'%A')" _log="myapp_${_dow}.log" echo "My log filename: ${_log}"
Sample outputs:
My log filename: myapp_Wednesday.log
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 0 comments… add one now }