I would like to use the nice formatting facilities that I find in the "date" command to format a file creation date. But I can't find any way to get output from "stat" that "date -d" will accept. Is there any way to do what I want within bash, or am I going to have to write C code?
What I tried:
pi@raspberrypi:~ $ stat --format %y foo.txt
2022-02-24 11:54:25.619999973 -0500
pi@raspberrypi:~ $ stat --format %Y foo.txt
1645721665
pi@raspberrypi:~ $ date -d `stat --format %Y foo.txt`
date: invalid date ‘1645721665’
pi@raspberrypi:~ $ date -d `stat --format %y foo.txt`
date: invalid option -- '0'
Try 'date --help' for more information.
What I would really like is to build a filename, using creation date as input:
% echo "$(date "%y%m%d_%H%M").log"
220318_0955.log
CodePudding user response:
try this date "%y%m%d_%H%M" -d "@$(stat --format %Y foo.txt)"
CodePudding user response:
With GNU stat if your underlying filesystem support the birth
time these formatting options can be rolled to give you what you want:
%w time of file birth, human-readable; - if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
Please don't confuse a creation date (better known as the birth date) with the ctime
or last status change time of a file!