Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatted modified date/time of file on mac bash?

In my bash script on mac (snow leopard) I have a path and filename, and I need to get the modified date/time of that file. I found I could do:
stat -f "%m" $MYFILE

However, that returns what I assume is epoch date/time. I need the date/time formatted: YYYYMMDDThhmmss. I've found all kinds of options (like date) that apparently depend on GNU, which on my mac I don't have.

What's the standard way to get a file's date/time modified in a user-specified format on mac (BSD?) bash. Or at least, a date/time formatting function that I can pass the result of my stat call above to.

like image 720
David Burson Avatar asked Aug 29 '12 01:08

David Burson


People also ask

How do I change the date modified on a file Mac?

To change the Modified timestamp to the current date and time, type "touch -m" in Terminal, followed by one space. Then drag the file from Finder into Terminal and press "Enter." Enter a space after the time and drag the file into the Terminal window. Press "Enter" to make the change.

How can I tell when a Mac file was created?

Answer: A: You can get the creation date & the last modified date in Finder using Get Info or selecting the file in column view or list view.

How do you find when was a file last modified Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.

How do I change the date format in bash?

Bash Date format YYYY-MM-DD To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.


2 Answers

It's actually pretty simple, but different enough from GNU date that it's nowhere near obvious:

date -r $TIMESTAMP +%Y%m%dT%H%M%S

To get stat to do the formatting:

stat -f "%Sm" -t "%Y%m%dT%H%M%S" FILE
like image 96
chepner Avatar answered Sep 19 '22 16:09

chepner


# how-to list all the files and dir in a dir sorted by their
# modified time in the different shells 
# usually mac os / Unix / FreeBSD stat 
stat -f "%Sm %N" -t "%Y-%m-%d %H:%M:%S" ~/opt/comp/proj/*|sort 

# STDOUT output:
# 2018-03-27 15:41:13 ~/opt/comp/proj/foo
# 2018-03-28 14:04:11 ~/opt/comp/proj/bar

# GNU Utils ( usually on Linux ) stat 
# STDOUT output:
stat -c "%y %n" ~/opt/comp/proj/*|sort

# 2018-03-29 09:15:18.297435000 +0300 ~/opt/comp/proj/bar
# 2018-03-29 09:15:18.297435000 +0300 ~/opt/comp/proj/foo
like image 27
Yordan Georgiev Avatar answered Sep 21 '22 16:09

Yordan Georgiev