linux命令總結之state命令


ls 命令及其許多參數提供了一些非常有用的文件信息。另一個不太為人所熟知的命令 stat 提供了一些更為有用的信息。

  1 [root@Gin scripts]# man stat
  2 STAT(1)                          User Commands                         STAT(1)
  3 
  4 NAME
  5        stat - display file or file system status
  6 
  7 SYNOPSIS
  8        stat [OPTION]... FILE...
  9 
 10 DESCRIPTION
 11        Display file or file system status.
 12 
 13        -L, --dereference
 14               follow links
 15 
 16 :
 17 STAT(1)                          User Commands                         STAT(1)
 18 
 19 NAME
 20        stat - display file or file system status
 21 
 22 SYNOPSIS
 23        stat [OPTION]... FILE...
 24 
 25 DESCRIPTION
 26        Display file or file system status.
 27 
 28        -L, --dereference
 29               follow links
 30 
 31        -Z, --context
 32               print the SELinux security context
 33 
 34        -f, --file-system
 35               display file system status instead of file status
 36 
 37        -c  --format=FORMAT
 38               use  the  specified FORMAT instead of the default; output a newline
 39               after each use of FORMAT
 40 
 41        --printf=FORMAT
 42               like --format, but interpret backslash escapes, and do not output a
 43               mandatory  trailing  newline.  If you want a newline, include \n in
 44               FORMAT.
 45 
 46        -t, --terse
 47               print the information in terse form
 48 
 49        --help display this help and exit
 50 
 51        --version
 52               output version information and exit
 53 
 54        The valid format sequences for files (without --file-system):
 55 
 56        %a     Access rights in octal
 57 
 58        %A     Access rights in human readable form
 59 
 60        %b     Number of blocks allocated (see %B)
 61 
 62        %B     The size in bytes of each block reported by %b
 63 
 64        %C     SELinux security context string
 65 
 66        %d     Device number in decimal
 67 
 68        %D     Device number in hex
 69 
 70        %f     Raw mode in hex
 71 
 72        %F     File type
 73 
 74        %g     Group ID of owner
 75 
 76        %G     Group name of owner
 77 
 78        %h     Number of hard links
 79 
 80        %i     Inode number
 81 
 82        %n     File name
 83 
 84        %N     Quoted file name with dereference if symbolic link
 85 
 86        %o     I/O block size
 87 
 88        %s     Total size, in bytes
 89 
 90        %t     Major device type in hex
 91 
 92        %T     Minor device type in hex
 93 
 94        %u     User ID of owner
 95 
 96        %U     User name of owner
 97 
 98        %x     Time of last access
 99 
100        %X     Time of last access as seconds since Epoch
101 
102        %y     Time of last modification
103 
104        %Y     Time of last modification as seconds since Epoch
105 
106        %z     Time of last change
107 
108        %Z     Time of last change as seconds since Epoch
109 
110        Valid format sequences for file systems:
111 
112        %a     Free blocks available to non-superuser
113 
114        %b     Total data blocks in file system
115 
116        %c     Total file nodes in file system
117 
118        %d     Free file nodes in file system
119 
120        %f     Free blocks in file system
121 
122        %C     SELinux security context string
123 
124        %i     File System ID in hex
125 
126        %l     Maximum length of filenames
127 
128        %n     File name
129 
130        %s     Block size (for faster transfers)
131 
132        %S     Fundamental block size (for block counts)
133 
134        %t     Type in hex
135 
136        %T     Type in human readable form
137 
138        NOTE: your shell may have its own version of stat,  which  usually  super-
139        sedes the version described here.  Please refer to your shell’s documenta-
140        tion for details about the options it supports.
141 
142 AUTHOR
143        Written by Michael Meskes.
144 
145 REPORTING BUGS
146        Report stat bugs to bug-coreutils@gnu.org
147        GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
148        General help using GNU software: <http://www.gnu.org/gethelp/>
149        Report stat translation bugs to <http://translationproject.org/team/>
150 
151 COPYRIGHT
152        Copyright © 2010 Free Software Foundation, Inc.  License GPLv3+:  GNU  GPL
153        version 3 or later <http://gnu.org/licenses/gpl.html>.
154        This  is free software: you are free to change and redistribute it.  There
155        is NO WARRANTY, to the extent permitted by law.
156 
157 SEE ALSO
158        stat(2)
159 
160        The full documentation for stat is maintained as a Texinfo manual.  If the
161        info and stat programs are properly installed at your site, the command
162 
163               info coreutils 'stat invocation'
164 
165        should give you access to the complete manual.
166 
167 GNU coreutils 8.4                October 2014                          STAT(1)
man stat

下面演示了stat 不帶參數顯示的信息:

[root@Gin scripts]# stat date.txt
  File: `date.txt'
  Size: 40              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 261790      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-01-29 10:49:25.119790781 +0800
Modify: 2017-01-29 10:49:11.961790098 +0800
Change: 2017-01-29 10:49:11.961790098 +0800

注意使用該命令獲得的信息:除了通常的文件大小(也可以使用 ls -l 命令獲得)以外,您還獲得了該文件占用的塊數。通常的 Linux 塊大小為 512 字節,因此一個大小為 93,300,148 字節的文件將占用 (93300148/512=) 182226.85 個塊。由於塊都是完整占用,因此該文件使用了一些整數個數的塊。無需猜測就可以獲得確切的塊數。

  您還可以從以上輸出中獲得文件所有權的 GID 和 UID,以及權限的八進制表示形式 (6751)。如果要將文件恢復到它現在具有的相同權限,可以使用 chmod 6751 oracle,而不是顯式拼寫這些權限。

  以上輸出最有用的部分是文件訪問時間戳信息。該輸出顯示,該文件被訪問的時間是 2006-08-04 04:30:52(顯示在“Access:”的旁邊),即 2006 年 8 月 4 日上午 4:30:52。這是某個人開始使用數據庫的時間。該文件的修改時間是 2005-11-02 11:49:47(顯示在“Modify:”的旁邊)。最后,“Change:”旁邊的時間戳顯示文件狀態更改的時間。

  stat 命令的修改符 -f 顯示了有關文件系統(而非文件)的信息:

[root@Gin scripts]# stat -f date.txt 
  File: "date.txt"
    ID: ff0d2532e41897c0 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 1851748    Free: 1286127    Available: 1190396
Inodes: Total: 479552     Free: 409340

另一個選項 -t 顯示了完全相同的信息,只不過是在一行中顯示的:

[root@Gin scripts]# stat -t date.txt 
date.txt 40 8 81a4 0 0 803 261790 1 0 0 1485658165 1485658151 1485658151 4096

這對 shell 腳本非常有用,在 shell 腳本中可以使用一個簡單的 cut 命令獲得值以進行進一步處理。

應用案例:

如何取得stat /poe命令后文件的權限對應的數字內容,如-rw-r--r--644,要求使用命令取得6440644這樣的數字。

 

[root@Gin scripts]# stat date.txt 
  File: `date.txt'
  Size: 40              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 261790      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-01-29 10:49:25.119790781 +0800
Modify: 2017-01-29 10:49:11.961790098 +0800
Change: 2017-01-29 10:49:11.961790098 +0800
[root@Gin scripts]# stat -c %a date.txt 
644

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM