0

Ref:`stat` output format: truncate a date field

The way they have extracted date from stat command by using:

stat -c '%.10y' file

My final command looks something like this:

stat -c "%y %i %A %n" *

I want to modify '%y' such that I only get the time. Instead of '.11' I want to define a range from 12 to 20. Is there a way to do it, and what is this type of formatting called ?

by default the time is:

2021-06-19 13:46:30.201999800 +0530

but I want:

13:46:30

Final output:

13:46:29 11540474045615128 -rw-r--r-- __temp__
13:49:03 14918173767136212 -rw-r--r-- __TEM__.txt
13:46:29 9007199255101576 -rw-r--r-- bla.txt
13:49:58 1125899908323122 -rwxr-xr-x f.sh
13:46:30 1125899908323121 -rwxr-xr-x Q1 2.sh
0

3 Answers 3

2

You could always use zsh's stat builtin instead (which predates GNU stat; beware different systems have different and incompatible implementations of stat, it's not a standard command):

#! /bin/zsh -
zmodload zsh/stat
for f (*(N)) {
  stat -F%T -LH s -- $f &&
    print -r $s[mtime] $s[inode] $s[mode] $f
}
1

You can format the string to show the date and time only and then use cut to get rid of the first column:

stat -c '%.19y %i %A %n' * | cut -d' ' -f2-

Edit: Adding cut parameters description upon request.

-d' '   ; use space character as field delimiter
-f2-    ; print field number 2 and continue to end of line 
          (aka delete first field/column in this case)
3
  • Hi @ethcz, can you explain a little how is this working? How is cut command working and what is '--f2--' Jun 19, 2021 at 10:20
  • @VanshikhaDhar use man cut to read the documentation where you can find out what the -f flag does. You can also experiment - start by comparing the output after using cut to the value generated by stat without cut Jun 19, 2021 at 12:48
  • 2
    Note that it assumes that file names don't contain newline characters and don't start with -. Jun 19, 2021 at 13:40
0

I have used awk to get the desired out

 stat -c '%y %i %A %n' k |awk '{gsub(/^[0-9]*-[0-9]*-[0-9]* /,"",$0);$2="";gsub(/\..*/,"",$1);print }'

Tested and worked fine

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .