Stat命令详解,及示例

来源:互联网 发布:java程序设计试卷 编辑:程序博客网 时间:2024/05/16 16:24

写这个博客的目的是,给自己学透stat命令。也希望能帮助大家进一步了解此命令。详细介绍乃本人对manual的渣翻译,如有错,请指正。

     一·Stat简要介绍:

     

      1. stat函数以一个文件所在的路径为参数,返回此文件的结构

        2.展示文件状态。


二·Stat详细介绍(DESCRIPTION):

1. stat展示了被文件指向的文件的信息。

        2.不要求被指定的文件有读,写或执行权限,但是在路径名内的所有目录必须是可搜索的<-意思是这目录必须可见或必须有?

    3.如果没给定参数,stat会展示标准输入的file descriptor。


例如:

stat
crw--w---- 1 Dala tty 268435456 0 "May  4 18:51:10 2017" "May  4 18:51:10 2017" "May  4 18:51:10 2017" "Jan  1 08:00:00 1970" 131072 0 0 (stdin)

   4.用readlink调用时,只有软连接的目标文件被打印出来。如果给定的参数不是软链接,readlink会什么都不打印,因错误推、退出!

例如:

readlink 文件名
当文件名不是一个软链接时,返回为空!

   5.展示的信息是从用给定的参数调用lstat函数以及计算返回结构得到的。

  三·使用选项:(不要嫌看它们烦,存在即合理!<-说给自己听的!)


     -F      

     使用-F得到的结果,若后面为‘/’表明此路径名为一个目录;

     ‘*’表明这是可执行的;

     ‘@’表明这是一个软链接;

     ‘%’表明这是一个white-out(这里的white-out指什么?欢迎补充。);

     ‘=’表明套接字;

     ‘|’表明这是一个先进先出。

示例:

stat -F ~/Documents


Documents为路径

结果:

drwx------ 15 dala staff 510 May  3 18:24:30 2017 /Users/dala/Documents/
故documents后有‘/’


     -f format

     用一个指定的格式来显示信息,更多请看格式FORMATS部分(就在下一部分哦!)。


     -L      

     使用stat而不是lstat。stat报告的信息会是文件的目标文件(如果这个文件是一个软链接的话),而不是这个文件本身!

     -l     

     使用 ls -lT格式显示输出。

     -n      

     不会空对每一个输出另外空起一行。

     -q      

             如果stat或lstat命令失败能压制错误信息(就是不打印出错误信息啦!),但使用readlink时,万一错了,错误信息是不会打印的哦!

     -r      

     显示出原始的,数字的信息。

示例:

使用前:

stat ~/Documents/
结果

16777220 622483 drwx------ 15 data staff 0 510 "May  4 18:04:36 2017" "May  3 18:24:30 2017" "May  3 18:24:30 2017" "Jul  9 06:33:08 2015" 4096 0 0 /Users/dala/Documents/

使用后:

stat -r ~/Documents/

结果:

16777220 622483 040700 15 501 20 0 510 1493892276 1493807070 1493807070 1436394788 4096 0 0 /Users/dala/Documents/


     -s      

用shell output 的方式显示信息,适用于初始化变量。

示例:

stat -s ~/Documents/

结果:

st_dev=16777220 st_ino=622483 st_mode=040700 st_nlink=15 st_uid=501 st_gid=20 st_rdev=0 st_size=510 st_atime=1493892276 st_mtime=1493807070 st_ctime=1493807070 st_birthtime=1436394788 st_blksize=4096 st_blocks=0 st_flags=0

     -t timefmt

用指定的格式来显示时间戳。

     -x      

用更啰嗦的方法来显示信息。

示例:

stat -x ~/Documents/

结果:

  File: "/Users/dala/Documents/"  Size: 510          FileType: Directory  Mode: (0700/drwx------)         Uid: (  501/dala)  Gid: (   20/   staff)Device: 1,4   Inode: 622483    Links: 15Access: Thu May  4 18:04:36 2017Modify: Wed May  3 18:24:30 2017Change: Wed May  3 18:24:30 2017


 四·格式 Formats

     格式串与printf格式相似,在于他们开始与%,之后跟着格式的字符,以选择了stat结构体中被格式的字段(fields)的字符而结束。

     %n表示另起一行;

     %t表示tab;

     %%表示百分比字符;

     %@表示当前文件数量。


     有以下可选择的表示标示:


     #       

     为十进制与十六进制选择其他输出格式。非0的十进制输出前会有一个0,非0的十六进制数输出前会有0x。

     +       

     使打印出来的数字带符号。


     -       

     使向左对齐。

     0       

     在左边填0而不是空白。


     space   

     在负符号的数值前空一格,+覆盖space,如果+和space一起用的话。

     

     有以下的字段:


     size   

     一个表明的字段长度的可选用的十进制字符串。


     prec    

     用例子解释 10.6,10表示字符串的最大长度,6表示小数点后的位数。

以下的有时间再翻译。


     fmt     An optional output format specifier

             which is one of D, O,U, X, F, or S.

             These represent signed decimal output,

             octal output, unsigned decimal output,

             hexadecimal output, floating point

             output, and string output, respec-

             tively.  Some output formats do not

             apply to all fields.  Floating point

             output only applies to timespec fields

             (the a, m, and c fields).


             The special output specifier S may be

             used to indicate that the output, if

             applicable, should be in string for-

             mat.  May be used in combination with:


             amc     Display date in strftime(3)

                     format.


             dr      Display actual device name.


             gu      Display group or user name.

  p       Display the mode of file as in

                     ls -lTd.


             N       Displays the name of file.


             T       Displays the 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 charac-

                     ters are prepended.


     sub     An optional sub field specifier (high,

             middle, low).  Only applies to the p,

             d, r, and T output formats.  It can be

             one of the following:


         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 per-

                     missions 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.


     datum   A required field specifier, being one

             of the following:


             d       Device upon which file

                     resides.


             i       file's inode number.


             p       File type and permissions.


             l       Number of hard links to file.


             u, g    User ID and group ID offile's

                     owner.


             r       Device number for character

                     and block device special

                     files.


             a, m, c,B

                     The time file was last

                     accessed or modified, of when

                     the inode was last changed, or

                     the birth time of the inode.


             z       The size of file in bytes.

           b       Number of blocks allocated for

                     file.


             k       Optimal file system I/O opera-

                     tion block size.


             f       User defined flags for file.


             v       Inode generation number.


             The following four field specifiers

             are not drawn directly from the data

             in structstat, but are:


             N       The name of the file.


             T       The file type, either as in ls

                     -F or in a more descriptive

                     form if the sub field speci-

                     fier H is given.


             Y       The target of a symbolic link.


             Z       Expands to ``major,minor''

                     from the rdev field for char-

                     acter or block special devices

                     and gives size output for all

                     others.


     Only the % and the field specifier are

     required.  Most field specifiers default to U

     as an output form, with the exception of p

     which defaults to O, a, m, and c which default

     to D, and Y, T, andN which default to S.


     五·退出状态 EXIT STATUS

     The stat and readlink utilities exit 0 on suc-

     cess, and >0 if an error occurs.

  

   六·示例 EXAMPLES

     Given a symbolic link foo that points from

     /tmp/foo to/, you would usestat as follows:


           > stat -F /tmp/foo

           lrwxrwxrwx 1 jschauma cs 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 some shell variables, you could

     use the -s flag as follows:


           > csh

           % eval set `stat -s .cshrc`

           % echo $st_size $st_mtimespec

           1148 1015432481


           > sh

           $ eval $(stat -s .profile)

           $ echo $st_size $st_mtimespec

           1148 1015432481


     In order to get a list of the kind of files

     including files pointed to if the file is a

     symbolic link, you could use the following

     format:


           $ stat -f "%N: %HT%SY" /tmp/*

           /tmp/bar: Symbolic Link -> /tmp/foo

           /tmp/output25568: Regular File

           /tmp/blah: Directory

           /tmp/foo: Symbolic Link -> /


     In order to get a list of the devices, their

     types and the major and minor device numbers,

     formatted with tabs and linebreaks, you could

     use the following format:


           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


     In order to determine the permissions set on a

     file separately, you could use the following

     format:


           > stat -f "%Sp -> owner=%SHp group=%SMp other=%SLp" .

           drwxr-xr-x -> owner=rwx group=r-x other=r-x


     In order to determine the three files that

     have been modified most recently, you could

     use the following format:


           > stat -f "%m%t%Sm %N" /tmp/* | sort -rn | head -3 | cut -f2-

           Apr 25 11:47:00 2002 /tmp/blah

           Apr 25 10:36:34 2002 /tmp/bar

           Apr 24 16:47:35 2002 /tmp/foo




0 0
原创粉丝点击