Getting the creation date of a file on Cygwin - shell

I'm trying to get the creation date of a file using console commands. I've managed to use this:
stat -c "%w" file
which outputs the date and time:
2015-05-01 09:33:22.503525000 -0400
But I'd like to extract only the date (without the time) from the result. Is there a way to do this using only stat, or do I have to separate the result using another command? And what might that command be?

Try this:
stat -c "%w" file | cut -d" " -f1
The output of stat will be piped into cut and the string will be cut at the first concurrence of a white space.

Assuming you are using bash:
date -d "#$(stat -c %W file)" +%F
Will do the trick. %W on stat returns the epoch date, date +%F converts it to a YYYY-MM-DD format.

Related

(MacOS Terminal). How to produce a readable Epoch time from terminal

I am trying to give a command to a friend who has been having account passwords changed. He is trying to determine a way to figure out when the password was last changed. I was able to give him this command, that is working in MacOS terminal, but I want to make it a bit nicer by providing a readable answer. Here is the command:
dscl . read /Users/username accountPolicyData | grep -A1 SetTime
which results in something like this:
<key>passwordLastSetTime</key>
<real>1670348364.4110398</real>
This command is pulling the reset date and time great, but him having to search out an epoch time calculator may be a bit over his head. My question:
Do any of you have any idea how I could strip out the bracketed text and convert the epoch time from the commandline? I'm happy to drop the title line if that helps if doing so would allow a numerical conversion that is readable.
Thanks for any suggestion you may have.
You can use defaults to read plist :
#!/bin/bash
file=$(mktemp /tmp/XXXXXXXX.plist)
dscl . read /Users/username accountPolicyData | tail -n +2 > $file
c=$(defaults read $file passwordLastSetTime)
date -r ${c%.*} '+%Y-%m-%d %H:%M:%S'
rm $file
You can use the following command to extract the epoch time and convert it to a human-readable date and time:
dscl . read /Users/username accountPolicyData | grep -A1 passwordLastSetTime | awk '/real/ {print strftime("%c",$2)}'
Explanation:
dscl . read /Users/username accountPolicyData is the same as before, it retrieves the account policy data for the specified username.
grep -A1 passwordLastSetTime filters the output to show only the line containing passwordLastSetTime and the next line.
awk '/real/ {print strftime("%c",$2)}' is an awk script that processes the filtered output. The script matches the line that contains real, extracts the second field ($2), and converts it from an epoch time to a human-readable date and time using the strftime function. The %c format string for strftime specifies a locale-specific date and time format.

How do I convert a date string in format MMDDYYYY to YYYY-MM-DD at the Unix command line?

From the Unix command line, how do I convert a date string in the format MMDDYYYY to the format YYYY-MM-DD? For instance, how do I convert 02032017 to 2017-02-03? Thanks!
I have tried this, but I'm afraid it did not work.
date -d "02032017"
This produces this error message.
date: invalid date ‘02032017’
String manipulation with variable expansions
input_date=02032017
output_date=${input_date:4}-${input_date::2}-${input_date:2:2}
This should work,
DATE="02032017"; date -d"$DATE" +%Y-%m-%d
Error in the above answer.
Try this one
month=$(cut -c 1-2)
date=$(cut -c 3-4)
year=$(cut -c 5-8)
date=$year-$month-$date
echo $date
You can use a date library that allows you to specify the incoming date's format, for example perl's Time::Piece:
input_date=02032017
perl -MTime::Piece -sE 'say Time::Piece->strptime($date, "%m%d%Y")->ymd' -- -date="$input_date"
2017-02-03

convert date string with hour and minutes to seconds in bash

I have a sample string that contains YYYYMMDDHHMM format.
I am trying to convert it into seconds but getting below error.
[vagrant#CentOS-Seven ~]$ export SAMPLE_DATE=201812051147
[vagrant#CentOS-Seven ~]$ echo $SAMPLE_DATE
201812051147
[vagrant#CentOS-Seven ~]$ date -d $SAMPLE_DATE
date: invalid date ‘201812051147’
[vagrant#CentOS-Seven ~]$
According to this answer date has not option to specify the input format. Therefore you have to convert your input into a format that is accepted by date. The sed command in the following command converts 201812051147 to 2018-12-05 11:47.
To convert a date to seconds, use the output format +%s.
$ input=201812051147
$ date -d "$(sed -E 's/(....)(..)(..)(..)(..)/\1-\2-\3 \4:\5/' <<< "$input")" +%s
1544010420
Please not that the output depends on your systems timezone. You can change the timezone by setting the TZ environment variable.
By the way: You don't have to export your variable in this case. Also, the convention for naming variables in bash is to use lowercase letters. By convention only special variables are written in all caps. Using variable names written in all caps could lead to a name collisions with these variables.
Apparently separating date and time parts with a space is enough, so:
$ echo 201812051147 |
sed -E 's/(.{8})(.{4})/\1 \2/' |
date -f - +"%s"
Output:
1544003220
You can use the "touch" command to assign that date to a file and then print it using "date", try this:
touch -t 201812051147 tmpfile ; date -r tmpfile +%s ; rm -f tmpfile

Grep a time stamp in the H:MM:SS format

Working on the file an need to grep the line with a time stamp in the H:MM:SS format. I tried the following egrep '[0-9]\:[0-9]\:[0-9]'. Didn't work for me. What am i doing wrong in regex?
$ date -u | egrep '\d{1,2}:\d{1,2}:\d{1,2}'
Fri May 2 00:59:47 UTC 2014
Try a site like http://regexpal.com/
Here is the fix:
grep '[0-9]:[0-9][0-9]:[0-9][0-9]'
If you need get timestamp only, and your grep is gnu grep.
grep -o '[0-9]:[0-9][0-9]:[0-9][0-9]'
and if you work more harder, limit on time format only:
grep '[0-2][0-9]:[0-5][0-9]:[0-5][0-9]'
Simplest way that I know of:
grep -E '([0-9]{2}:){2}[0-9]{2}' file
If you need month and day also:
grep -E '.{3,4} .{,2} ([0-9]{2}:){2}[0-9]{2}' file

How to convert time format from "2010-10-08 00:00:01" to "1286467201" in awk

In awk, Is there a way to convert the time format from "2010-10-08 00:00:01" to 1286467201
as using the command "date"
$ date +%s -d '2010-10-08 00:00:01'
1286467201
GNU awk has a mktime function that can do the job. However, it's crucial to be aware of timezones. The string "2010-10-08 00:00:01" does not contain enough information to define a specific time. If you assume it is in UTC you can do:
$ echo 2010-10-08 00:00:01 | \
TZ=UTC gawk '{ tstr=$1" "$2; gsub(/[\-:]/, " ", tstr); print mktime(tstr); }'
1286496001
If you don't specify the TZ variable you end up with the server's time zone (which should be UTC anyway, but a lot of folks use local time on servers, so it's not a safe assumption).
You can get UTC output from your date command by altering it slightly:
$ date +%s -u -d '2010-10-08 00:00:01'

Resources