Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX stat time format

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
like image 471
scott77777 Avatar asked Nov 15 '10 05:11

scott77777


People also ask

What does stat do in Unix?

On Unix-like operating systems, the stat command displays the detailed status of a particular file or a file system.

What does the command stat do?

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.

Does LS use stat?

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.


1 Answers

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

like image 157
Dennis Williamson Avatar answered Sep 30 '22 14:09

Dennis Williamson