stat

display file or filesystem status

Basis of ls, stat permits field seletion

stat [option] file...

Reference: File System attributes

--format= or -c
formatting codes for files
%n File name %N Quoted Filename
maybe embedded spaces, …
with dereference if symbolic link
%h Number of hard links  
%A Access rights %a octal
%s Total size, in bytes%bblocks allocated

 seconds
 since
 Epoch

%xlast access
ex: 2009-02-08 12:53:27.000000000 -0500
See /etc/fstab and mount -o [no]atime
%X
%y modification %Y
%z change %Z
file creation (born) date.
file systems ext2, ext3 do not support creation date, See Also File System for much more info regarding filesystems than you want to know.
%U User name of owner %u UID
%G Group name of owner%g GID
%F File type %f raw mode in hex
%i Inode number
%o IO block size
%d Device number in decimal %D hex
%t Major device type in hex %T Minor

-f or --file-system

for file systems -c
%n File name
%f Free blocks in file system
%a available to non-superuser%b Total
%c Total file nodes in file system%d Free
%i File System id in hex
%l Maximum length of filenames
%s Optimal transfer block size
%T Type in human readable form%t hex
 > stat -f /bin
  File: "/bin"
  ID: 0        Namelen: 255     Type: ext2/ext3
  Block size: 4096       Fundamental block size: 4096
  Blocks: Total: 2510895  Free: 1663998  Available: 1534394
  Inodes: Total: 2592691  Free: 2543983


 > stat /bin
   File: `/bin'
   Size: 4096  Blocks: 16   IO Block: 4096   directory
   Device: 802h/2050d    Inode: 16433473    Links: 2
   Access: (0755/drwxr-xr-x)  Uid:(0/root) Gid:(0/root)
   Access: 2009-02-08 12:53:27.000000000 -0500
   Modify: 2009-01-09 05:46:10.000000000 -0500
   Change: 2009-01-09 05:46:10.000000000 -0500
 
Sample script to make stat output pretty
echo "                yy    access             modification              change             I am $0"
export ccyymmdd=`date +"%Y-%m-%d"`
export ccyy=`date +"%Y-"`
/usr/bin/stat -c"%n %x   %y   %z" $* |sed "s/ /\\t/; s/$ccyymmdd/__-__-__/g;  s/200/0/g; s/00000000 -0.00//g;"|column -t


Mac OS

stat [-FLnq] [-f format | -l | -r | -s | -x] [-t timefmt] [fileList]

displays the target of a symbolic link or if file is not a symbolic link displays nothing exits with an error code of 1.

-l output in ls -lT format.
-rw-r--r-- 1 dgerman dgerman 13986 Apr 19 16:49:39 2008 stat.1.html
‑F after each pathname display
/ if it is a directory
* permission iincludes executable
@ symbolic links,
% whiteouts,
= sockets, and
| FIFOs. -F implies -l.
-L Use stat instead of lstat.
The information refers to the target of file, if file is a symbolic link
-q Quite, Suppress access failure messages( usage or syntax messages are output )
-r for all the fields in the stat structure, display the raw, numerical value
(permissions in octal, times in seconds since the epoch, …
234881028 1272436 0100644 1 501 501 0 14847 1208647713 1208647713 1208647713 1208647713 4096 32 0 stat.1.html
-s use shell output for setting variables.
st_dev=234881028 st_ino=1273272 st_mode=0100644 st_nlink=1 st_uid=501 st_gid=501 st_rdev=0 st_size=15306 st_atime=1208652461 st_mtime=1208652459 st_ctime=1208652459 st_birthtime=1208652459 st_blksize=4096 st_blocks=32 st_flags=0
-x verbose (no, not -v)

 File: "stat.1.html"
  Size: 14119        FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/ dgerman)  Gid: (  501/ dgerman)
Device: 14,4   Inode: 1271740    Links: 1
Access: Sat Apr 19 16:52:50 2008
Modify: Sat Apr 19 16:52:49 2008
Change: Sat Apr 19 16:52:49 2008

-t timefmt see date.
stat -t"%F-%H-%M-%S" stat.1.html         notice quotes inserted by stat, not formatting
234881028 1280986 -rw-r--r-- 1 dgerman admin 0 17127 "2008-04-20-16-04-46" "2008-04-20-16-04-44" "2008-04-20-16-04-44" "2008-04-20-16-04-44" 4096 40 0 stat.1.html
-n no newline after each file's data
-f format Display information using the specified format.
Formatting: %n newline , %t tab, %% percent, %@ current file number in fileList

%formattingCharacters fieldSpecifier

Characters not included as part of formattingCharacters or fieldSpecifiction are displayed.
Only the % and the fieldSpecifier are required.
formattingCharacters
- Align left
+ sign will be output
0 0 for left padding
space Reserves a space at the front of non-negative signed output fields.
+ overrides a space
# use alternate output form for octal and hexadecimal .
octal output will have a leading 0 ,
hexadecimal output leading 0x
Then
size minimum field width.
precision . and a decimal digit string .
maximum string length: the number of digits to appear after the decimal point in floating point , or
the minimum number of digits to appear in numeric .
fmt S, U, X, O, D, F.
String, unsignedDecimal , hexadecimal, octal, signeddecimal, floatingPoint
Floating point output only applies to timespec fields a, m, and c
. .

S is used with:

a, m, c date in strftime format.
d, r actual device name.
g, u group or user name.
p permissions (mode) of file as in ls -lTd.
N name of file.
T type of file.
Y Insert a -> into the output. Note that the default output format for Y is a string, but if specified explicitly, these four characters are prepended.
sub sub field specifier (H, M, L). applies to p, d, r, and T
H High
  • specifies the major number for devices from r or d,
  • the user bits for permissions from the string form of p,
  • the file type bits from the numeric forms of p, and the long output form of T.
  • L Low
  • specifies the minor number for devices from r or d,
  • the other bits for permissions from the string form of p,
  • the user, group, and other bits from the numeric forms of p,
  • and the ls -F style output character for file type when used with T (the use of L for this is optional).
  • M Middle
  • specifies the group bits for permissions from the string output form of p or
  • the suid, sgid, and sticky bits for the numeric forms of p.
  • darwin:
    234881033 176990 -rw-r--r-- 1 dgerman dgerman 0 7326645 "Mar 15 01:13:24 2008" "Jun 26 14:32:26 2007" "Jun 27 19:18:55 2007" "Jun 26 14:32:26 2007" 4096 14312 0 0aks.gz
    fieldSpecifier
    p File type and permissions : stat -f "%Op %Xp %#p %#Xp %Sp"
    100644 81a4 0100644 0x81a4 -rw-r--r--
    l Number of hard links to file.
    u
    g
    User ID or
    group ID of file's owner.
    stat -f "%u %g %Su %Sg"
    501 80 dgerman admin

    a
    m
    c
    B
    time file was last
    accessed or
    modified or
    when the inode was last changed
    or the Birth time of the inode.
    stat -f " a=%Sa %n m=%Sm %n c=%Sc %n B=%SB"
    a=Apr 20 12:02:15 2008
    m=Apr 20 12:02:13 2008
    c=Apr 20 12:02:13 2008
    B=Apr 20 12:02:13 2008
    z size file in bytes. > hexdump -C 3f
    00 77 0a 0a |w..|
    03
    > /usr/bin/stat -f "%z %b " 3f
    3 8 ### 3 byte file takes 8 blocks !
    b blocks allocated for file.
    k Optimal file system I/O operation block size. stat -f "%k"
    4096
    d Device upon which file resides: stat -f"%Sd"
    disk0s5
    i file's inode number stat -f"%i"
    1278008
    v Inode generation number.
    f User defined flags for file.
    ( Unable to find any use of this field 4/18/08 ed.)
    r Device number for character and block device special files.
    field specifiers not from data in struct stat
    N name of the file.
    T file type, either as in ls -F example: / @ or
    descriptive with H is given.
    stat -n -f "%T %HT" /dev/console stat.1.html
    Character Device Regular File
    Y target of symbolic link.
    Z major, minor from the rdev for character or block special devices
    size for all others.

    Most formattingCharacters default to U
    p (permissions) defaults to Octal
    a, m, and c default to D and
    Y, T, and N (symbolicTarget, fileType and Name), default to S.

    Exit 0 on success, and >0 if an error occurs.

    EXAMPLES

    Given a symbolic link foo that points from /tmp/foo to /

               > stat -F /tmp/foo
               lrwxrwxrwx 1 user grp 1 Apr 24 16:37:28 2002 /tmp/foo@ -> /
    
               > stat -LF /tmp/foo
               drwxr-xr-x 16 root wheel 512 Apr 19 10:57:54 2002 /tmp/foo/ 
    To initialize shell variables, use -s :
               > csh                                     > sh
               % eval set `stat -s .cshrc`               $ eval $(stat -s .profile)
               % echo $st_size $st_mtimespec             $ echo $st_size $st_mtimespec
               1148 1015432481                           1148 1015432481 
    to get a list of the kind of files including files pointed to if the file is a symbolic link:
               $ stat -f "%N: %HT%SY" /tmp/*
               /tmp/bar: Symbolic Link -> /tmp/foo
               /tmp/output25568: Regular File
               /tmp/blah: Directory
               /tmp/foo: Symbolic Link -> / 
    to get a list of the devices, types and the major and minor device numbers,
    formatted with tabs and linebreaks,
     stat -f "Name: %N%n%tType: %HT%n%tMajor: %Hr%n%tMinor: %Lr%n%n" /dev/*
               [...]
               Name: /dev/wt8
                       Type: Block Device
                       Major: 3
                       Minor: 8
    
               Name: /dev/zero
                       Type: Character Device
                       Major: 2
                       Minor: 12
    
    
    Display the permissions set on a file separately:
     > stat -f "%Sp -> owner=%SHp group=%SMp other=%SLp" .
               drwxr-xr-x -> owner=rwx group=r-x other=r-x
    
    To determine the files that have been modified most recently, use:
    (sort -n treates the modify time in decimal as a number not a string)
        > stat -f "%m%t%Sm %N" /tmp/* | sort -n |cut -f2- (cut discards first field, the modify time in decimal )
        Apr 25 11:47:00 2002 /tmp/blah
        Apr 25 10:36:34 2002 /tmp/bar
        Apr 24 16:47:35 2002 /tmp/foo 
    
    Read, write or execute permissions of fileList are not required,
    but directories listed in the path name leading to the file must be searchable.

    If no fileList is given, information is show for STDIN.

    38294852 58542596 crw--w---- 1 dgerman tty 268435456 0 
            "Apr 19 20:53:01 2008" "Apr 19 20:53:01 2008" "Apr 19 20:53:01 2008" "Apr 19 20:53:01 2008" 131072 0 0 (stdin)
    
    useful :
    export ccyymmdd=`date +"%x"`
    export yymmdd=`echo $ccyymmdd|sed "s/\/20/\//"`
    /usr/bin/stat -t "%x %X " -f"%N %Sa %Sm %Sc" $* | sed "s'$yymmdd'__/__/__'g; s/\"//g" | column -t
    traceroute __/__/__ 19:39:44 06/05/05 20:59:00 03/19/09 19:49:09

    see file, ls, lstat, readlink ,stat, printf, strftime

    FSF linux version darwin
    -L
    --dereference
    follow links
    -t
    --terse
    --help --version