Write current date/time to a file using shell script
Use date >> //home/user/Desktop/Scripts/Date Logs/datelog.txt
.
Like i tried in my system :-
date > /tmp/date.txt
.
And file contains Wed Apr 5 09:27:37 IST 2017
.
[Edit] There are difference between >>(appending to the file) and >(Create the new file)
Edit:- As suggested by chepner, you can directly redirect the o/p of date command to file using date >> /tmp/date.txt
.
How to create a file with todays date in the filename
You should use double quotes and need to evaluate date +"%F"
using command substitution.
$ touch "test_$(date +%F)"
This will create an empty file test_2018-01-15
Double quote helps you create a single file where some options of date
command would include a space.
For example, touch test_$(date)
will create multiple files, where as touch "test_$(date)"
won't.
As pointed out by OP, one would need additional quotes "
around the format options, when multiple of them are used:
touch "test_$(date +"%F %T")"
How to name a file with current time?
Use command substitution to capture date
's output as a string.
echo 'hello' > "$(date +%T)".txt
Print a Date with some text in a new File using Shell Script
First of all you will want to format the output of date if you want to use it in the filename. If not you can use standard date command. You can find date format explanations here
Here is a sample block of code to output the current date and time to a text file :
#!/bin/bash
echo $(date +%F-%T) >> Output.txt
Here %F outputs the full date in the following format : YYYY-MM-DD
And %T outputs time in the following format : HH:MM:SS
If you want to add more text than just the date, you can use the "
delimiter, to create a string as follow :
echo "$(date +%F-%T) Log output" >> Output.txt
Putting all the text in the string makes sure bash doesn't interpret what's inside the string as operators and delimiters.
EDIT : Screenshot for bash version :
How to get the current date and time in specific format in Shell
You may use:
dt=$(date '+%Y-%m-%dT%T.%zZ')
echo "$dt"
2019-02-27T04:35:56.-0500Z
Also note use of --iso-8601=seconds
:
date --iso-8601=seconds
2019-02-27T04:37:29-05:00
YYYY-MM-DD format date in shell script
In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date
(usually GNU date).
As such:
# put current date as yyyy-mm-dd in $date
# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided
# -2 -> start time for shell
printf -v date '%(%Y-%m-%d)T\n' -1
# put current date as yyyy-mm-dd HH:MM:SS in $date
printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1
# to print directly remove -v flag, as such:
printf '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal
In bash (<4.2):
# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')
# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')
# print current date directly
echo $(date '+%Y-%m-%d')
Other available date formats can be viewed from the date man pages (for external non-bash specific command):
man date
Related Topics
How to Open Serial Port in Linux Without Changing Any Pin
Linux Shell Programming String Compare Syntax
What Is The Maximum Allowed Limit on The Length of a Process Name
Configure "-Prefix" Option for Cross Compiling
How to View Svn Diff in Vimdiff Style in Svn
Bash Will Not Auto-Complete (Tab) with Files
How to Connect to Server: Postgresql on Ubuntu in Windows Subsystem for Linux
Finding Latency Issues (Stalls) in Embedded Linux Systems
Programmatic Resource Monitoring Per Process in Linux
Unknown Symbol in While Loading a Kernel Module
Why Does Git Fail on Push/Fetch with "Too Many Open Files"
Recommended Irc Server (Ircd) for a Small Site
Linux: How to Enable Execute in Place (Xip) for Ramfs/Tmpfs
How to Find The Memory Consumption of a Particular Process in Linux for Every 5 Seconds
How to Get The Bash Date Script to Return a Day of The Week Relative to a Non-Current Time
How to Sort The Output of "Grep -L" Chronologically by Newest Modification Date Last