Is it possible to format the time output of stat? I am using
stat -c '%n %A %z' $filename
in a bash script, but its time format is not what I want. Is it possible to change this format in the command, or would I have to manually do it later?
An example output follows:
/lib drwxr-xr-x 2010-11-15 04:02:38.000000000 -0800
On Unix-like operating systems, the stat command displays the detailed status of a particular file or a file system.
The stat is a command which gives information about the file and filesystem. Stat command gives information such as the size of the file, access permissions and the user ID and group ID, birth time access time of the file. Stat command has another feature, by which it can also provide the file system information.
The ls command is probably one of the first commands that anyone using Unix learns, but it only shows a small portion of the information that is available with the stat command. The stat command pulls information from the file's inode.
You can simply strip of the decimal portion like this:
stat -c '%n %A %z' "$filename" | sed 's/\(:[0-9]\{2\}\)\.[0-9]* /\1 /'
Edit:
Here's another way to truncate the decimal portion:
stat -c '%n %A %.19z' "$filename"
This depends on the date being 19 characters long: 2010-11-15 04:02:38
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With