I have this to get the timestamp of a directory:
stat -c %Y directory_name
What can I pipe this to, to get the date formatted like this?
20220201
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThat stat -c %Y
is specific to the GNU implementation of stat
. If have GNU stat
on your Solaris machine, it's likely you'll also have GNU find
where you can just do:
gfind dir -prune -printf '%TY%Tm%Td\n'
You'll probably also have GNU date
, with which you can do:
gdate -r dir +%Y%m%d
(though beware that if dir
is a symlink, it will give you the modification time of its target, not the symlink itself).
Solaris also comes with zsh
which has its own stat
builtin (and has had long before a stat
utility was added to GNU coreutils) where you can do:
zmodload zsh/stat
stat -LF %Y%m%d +mtime dir
date --date=@$(stat -c %Y directory_name) +%Y%m%d
You can embed it into a date command, e.g.:
date -d "@$(stat -c %Y directory_name)" +%Y%m%d
But beware stat -c
is a GNU extension, doesn't work elsewhere...
Try this with printf
:
$ printf '%(%Y%m%d)T\n' "#$(stat -c %Y dir)"
20220127
printf
builtin of bash
. bash
copied that %T
from ksh93, but in ksh93 (which is the /bin/sh
of Solaris since Solaris 11), You'd need printf '%(%Y%m%d)T\n' "#$(stat -c %Y dir)"
. Also note that dir
is not the same as dir/
when dir
is a symlink.
Feb 4, 2022 at 17:44