程序員必備的面試技巧
“程序員必備的面試技巧,就像是編寫一段完美的代碼一樣重要。在面試戰(zhàn)場(chǎng)上,我們需要像忍者一樣靈活,像偵探一樣聰明,還要像無敵鐵金剛一樣堅(jiān)定。只有掌握了這些技巧,我們才能在面試的舞臺(tái)上閃耀光芒,成為那個(gè)令HR們心動(dòng)的程序猿!”
目錄
- 0?前言
- 1?DOS、Windows、Linux中的文件存儲(chǔ)
-
- 1.1?扇區(qū)
- 1.2?DOS、Windows:簇(Cluster)
- 1.3 Linux:塊(block)和索引節(jié)點(diǎn)(inode)
- 2 stat命令的功能、格式和選項(xiàng)說明
-
- 2.1 stat命令的功能
- 2.2 stat命令的格式
- 2.3 stat命令的選項(xiàng)
- 3 stat命令使用實(shí)例
-
- 3.1 stat?文件或目錄:查看文件或目錄信息
- 3.2 stat -t?文件或目錄:以簡(jiǎn)潔的形式顯示文件或目錄狀態(tài)
- 3.3 stat -f?文件或目錄:顯示文件或目錄的系統(tǒng)狀態(tài)而不是文件或目錄狀態(tài)
- 3.4 stat -L 文件?或 stat --dereference 文件:取消對(duì)引用(跟隨)符號(hào)鏈接顯示并顯示該符號(hào)鏈接指向的文件的信息
- 3.5 stat?-c "格式" 文件?或 stat --format="格式"?文件?或?stat --printf="格式":自定義輸出
- 4?其它查看Inodes的命令
0?前言
在
4?其它查看Inodes的命令
中,我們提到??df -i或--inodes的功能是?列出索引節(jié)點(diǎn)而不是塊使用情況信息。
那么什么inode(索引節(jié)點(diǎn))呢?我們一起來探討看看……
1?DOS、Windows、Linux中的文件存儲(chǔ)
1.1?扇區(qū)
文件一般是存儲(chǔ)在磁盤上的,磁盤的最小存儲(chǔ)單位叫做扇區(qū)(sector),每個(gè)扇區(qū)大小為512字節(jié)。
扇區(qū)是磁盤的最小存儲(chǔ)單元。
1.2?DOS、Windows:簇(Cluster)
為了提高磁盤讀取效率,操作系統(tǒng)通常會(huì)一次讀取多個(gè)扇區(qū)的內(nèi)容。
在微軟的DOS、WINDOWS等操作系統(tǒng)中,把一次讀取多個(gè)扇區(qū)稱為簇(Cluster),簇是DOS、WINDOWS等操作系統(tǒng)中磁盤文件存儲(chǔ)管理的最小單位。1個(gè)長(zhǎng)度非0的文件,至少占用1簇的存儲(chǔ)空間。
1.3 Linux:塊(block)和索引節(jié)點(diǎn)(inode)
在Linux操作系統(tǒng)中,把一次讀取多個(gè)扇區(qū)稱為塊(block),最常見的是4KB,即連續(xù)八個(gè)扇區(qū)組成一個(gè)塊。文件數(shù)據(jù)存儲(chǔ)在塊中,塊是Linux操作系統(tǒng)中文件存取的最小單位。
索引節(jié)點(diǎn)(inode)是文件元信息的存儲(chǔ)區(qū)域,文件元信息包括以下信息,但不包含文件名,文件名存放在目錄當(dāng)中。
- 文件的字節(jié)數(shù)
- 文件擁有者的 User ID
- 文件的 Group ID
- 文件的讀、寫、執(zhí)行權(quán)限
- 文件的時(shí)間戳,共有三個(gè):ctime 指 inode 上一次變動(dòng)的時(shí)間,mtime 指文件內(nèi)容上一次變動(dòng)的時(shí)間,atime 指文件上一次打開的時(shí)間。
- 鏈接數(shù),即有多少文件名指向這個(gè) inode
- 文件數(shù)據(jù) block 的位置
Linux系統(tǒng)內(nèi)部不使用文件名,而是使用索引節(jié)點(diǎn)號(hào)碼識(shí)別文件。對(duì)于系統(tǒng)來說文件名只是為便于識(shí)別索引節(jié)點(diǎn)號(hào)碼而使用的別稱。
在Linux系統(tǒng)中,一個(gè)文件必須占用一個(gè)索引節(jié)點(diǎn)(inode),占用至少一個(gè)塊(block)。
要查看索引節(jié)點(diǎn)(inode)的信息,除了使用df命令,我們還可以使用stat命令。
2 stat命令的功能、格式和選項(xiàng)說明
我們可以使用 stat --help命令查看 stat 命令的幫助信息。
purpleEndurer @ bash $ stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.Mandatory arguments to long options are mandatory for short options too.
? -L, --dereference ? ? follow links
? -f, --file-system ? ? display file system status instead of file status
? -c ?--format=FORMAT ? use the specified FORMAT instead of the default;
? ? ? ? ? ? ? ? ? ? ? ? ? output a newline after each use of FORMAT
? ? ? --printf=FORMAT ? like --format, but interpret backslash escapes,
? ? ? ? ? ? ? ? ? ? ? ? ? and do not output a mandatory trailing newline;
? ? ? ? ? ? ? ? ? ? ? ? ? if you want a newline, include \n in FORMAT
? -t, --terse ? ? ? ? ? print the information in terse form
? ? ? --help ? ? display this help and exit
? ? ? --version ?output version information and exitThe valid format sequences for files (without --file-system):
? %a ? access rights in octal
? %A ? access rights in human readable form
? %b ? number of blocks allocated (see %B)
? %B ? the size in bytes of each block reported by %b
? %C ? SELinux security context string
? %d ? device number in decimal
? %D ? device number in hex
? %f ? raw mode in hex
? %F ? file type
? %g ? group ID of owner
? %G ? group name of owner
? %h ? number of hard links
? %i ? inode number
? %m ? mount point
? %n ? file name
? %N ? quoted file name with dereference if symbolic link
? %o ? optimal I/O transfer size hint
? %s ? total size, in bytes
? %t ? major device type in hex, for character/block device special files
? %T ? minor device type in hex, for character/block device special files
? %u ? user ID of owner
? %U ? user name of owner
? %w ? time of file birth, human-readable; - if unknown
? %W ? time of file birth, seconds since Epoch; 0 if unknown
? %x ? time of last access, human-readable
? %X ? time of last access, seconds since Epoch
? %y ? time of last modification, human-readable
? %Y ? time of last modification, seconds since Epoch
? %z ? time of last change, human-readable
? %Z ? time of last change, seconds since EpochValid format sequences for file systems:
? %a ? free blocks available to non-superuser
? %b ? total data blocks in file system
? %c ? total file nodes in file system
? %d ? free file nodes in file system
? %f ? free blocks in file system
? %i ? file system ID in hex
? %l ? maximum length of filenames
? %n ? file name
? %s ? block size (for faster transfers)
? %S ? fundamental block size (for block counts)
? %t ? file system type in hex
? %T ? file system type in human readable formNOTE: your shell may have its own version of stat, which usually supersedes
the version described here. ?Please refer to your shell's documentation
for details about the options it supports.GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report stat translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'stat invocation'
purpleEndurer @ bash $?
2.1 stat命令的功能
顯示文件或文件系統(tǒng)狀態(tài)。
2.2 stat命令的格式
stat [選項(xiàng)]...?文件...
注意:
1.使用stat命令必須指定要顯示信息的文件
purpleEndurer @ bash $ stat
stat: missing operand
Try 'stat --help' for more information.
purpleEndurer @ bash $?
?
2.不同的Linux shell 可能會(huì)有自己的 stat 版本,請(qǐng)參閱自己所用的 shell 的文檔了解有關(guān)其支持的選項(xiàng)的詳細(xì)信息。?
2.3 stat命令的選項(xiàng)
選項(xiàng) | 功能 |
---|---|
-L 或 --dereference |
取消對(duì)引用(跟隨)符號(hào)鏈接顯示并顯示該符號(hào)鏈接指向的文件的信息 |
-f 或 --file-system |
顯示文件系統(tǒng)狀態(tài)而不是文件狀態(tài) |
-c FORMAT 或 --format=FORMAT |
使用指定的 FORMAT 而不是默認(rèn)值; 每次使用 FORMAT 后輸出換行符 |
--printf=FORMAT | 與 --format 類似,但解釋反斜杠轉(zhuǎn)義,并且不輸出強(qiáng)制性的尾隨換行符; 如果需要換行符,請(qǐng)?jiān)?FORMAT 中包含 n |
-t 或 --terse |
以簡(jiǎn)潔的形式打印信息 |
--help | 顯示此幫助并退出 |
--version | 輸出版本信息并退出 |
3 stat命令使用實(shí)例
3.1 stat?文件或目錄:查看文件或目錄信息
例3.1.1:在當(dāng)前目錄下用mkdir命令創(chuàng)建test目錄并用stat查看test目錄的信息
purpleEndurer @ bash $ mkdir test
purpleEndurer @ bash $ stat test
? File: ‘test’
? Size: 4096 ? ? ? ? ? ?Blocks: 8 ? ? ? ? ?IO Block: 4096 ? directory
Device: 4ch/76d Inode: 1319986 ? ? Links: 2
Access: (0775/drwxrwxr-x) ?Uid: ( 1000/ ? ?csdn) ? Gid: ( 1000/ ? ?csdn)
Access: 2024-01-17 19:23:39.555845513 +0800
Modify: 2024-01-17 19:23:39.555845513 +0800
Change: 2024-01-17 19:23:39.555845513 +0800
?Birth: -
purpleEndurer @ bash $?
?
?命令返回了如下信息:
-
File
:文件名 -
Size
:文件大小,以字節(jié)表示 -
Blocks
:文件占用(已分配)的塊數(shù)。 -
IO Block
:文件系統(tǒng)塊大小 -
directory
:文件類型(普通文件、目錄、文件系統(tǒng)) -
Device
:文件所在的設(shè)備(十六進(jìn)制和十進(jìn)制) -
Inode
:文件所在的 Inode 號(hào) -
Links
:與文件關(guān)聯(lián)的硬鏈接數(shù) -
Access?
(0775/drwxrwxr-x) ?:數(shù)字/符號(hào)格式的文件權(quán)限 -
UID:
所有者的用戶ID和用戶名 -
GID:
所有者的群組ID和組名 -
Access?
2024-01-17 19:23:39.555845513 +0800:
上次訪問文件的時(shí)間 -
Modify?
2024-01-17 19:23:39.555845513 +0800:上次修改文件內(nèi)容的時(shí)間 -
Change?
2024-01-17 19:23:39.555845513 +0800:上次更改文件文件元數(shù)據(jù)的時(shí)間 -
Birth
:文件狀態(tài)被更改的的時(shí)間戳,Linux不支持
例3.1.2?在當(dāng)前目錄創(chuàng)建文件test.txt并用并用stat查看test.txt文件的信息
purpleEndurer @ bash $ ls > test.txt
purpleEndurer @ bash $ stat test.txt
? File: ‘test.txt’
? Size: 27 ? ? ? ? ? ? ?Blocks: 8 ? ? ? ? ?IO Block: 4096 ? regular file
Device: 4ch/76d Inode: 1319971 ? ? Links: 1
Access: (0664/-rw-rw-r--) ?Uid: ( 1000/ ? ?csdn) ? Gid: ( 1000/ ? ?csdn)
Access: 2024-01-17 19:40:48.612393492 +0800
Modify: 2024-01-17 19:40:48.613393535 +0800
Change: 2024-01-17 19:40:48.613393535 +0800
?Birth: -
purpleEndurer @ bash $?
?
對(duì)于文件,stat顯示的信息不同之處在于:
-
directory
:普通文件
3.2 stat -t?文件或目錄:以簡(jiǎn)潔的形式顯示文件或目錄狀態(tài)
例3.2?分別以stat -t?和 stat?命令查看文件test.txt?的狀態(tài)?
purpleEndurer @ bash $ ls > test.txt
purpleEndurer @ bash $ stat -t test.txt
test.txt 14 8 81b4 1000 1000 4c 1319933 1 0 0 1705553539 1705553539 1705553539 0 4096purpleEndurer @ bash $ stat test.txt
? File: ‘test.txt’
? Size: 14 ? ? ? ? ? ? ?Blocks: 8 ? ? ? ? ?IO Block: 4096 ? regular file
Device: 4ch/76d Inode: 1319933 ? ? Links: 1
Access: (0664/-rw-rw-r--) ?Uid: ( 1000/ ? ?csdn) ? Gid: ( 1000/ ? ?csdn)
Access: 2024-01-18 12:52:19.515288704 +0800
Modify: 2024-01-18 12:52:19.516288743 +0800
Change: 2024-01-18 12:52:19.516288743 +0800
?Birth: -
purpleEndurer @ bash $?
?
3.3 stat -f?文件或目錄:顯示文件或目錄的系統(tǒng)狀態(tài)而不是文件或目錄狀態(tài)
例3.3.1?查看test目錄的文件系統(tǒng)狀態(tài)
purpleEndurer @ bash $ stat -f test
? File: "test"
? ? ID: d52c0294601b7508 Namelen: 255 ? ? Type: overlayfs
Block size: 4096 ? ? ? Fundamental block size: 4096
Blocks: Total: 7584002 ? ?Free: 6796649 ? ?Available: 6441294
Inodes: Total: 1933312 ? ?Free: 1870694
purpleEndurer @ bash $?
??
命令返回信息包括以下內(nèi)容:?
- File:文件名。
- ID:十六進(jìn)制文件系統(tǒng)ID。
- Namelen:?文件名稱最大的長(zhǎng)度。
- Fundamental block size:?文件系統(tǒng)上每個(gè)塊大小。
- Blocks:塊信息,包括以下內(nèi)容:
- Total:文件系統(tǒng)中的總塊數(shù)。
- Free:文件系統(tǒng)中可用的塊數(shù)。
- Available:非root用戶可用的可用塊數(shù)。
- Inodes:索引節(jié)點(diǎn)信息,包括以下內(nèi)容:
- Total :文件系統(tǒng)中的總索引節(jié)點(diǎn)數(shù)。
- Free:文件系統(tǒng)中空閑索引節(jié)點(diǎn)的數(shù)量。
例3.3.2?查看文件test.txt的文件系統(tǒng)狀態(tài)
purpleEndurer @ bash $ stat -f test.txt
? File: "test.txt"
? ? ID: d52c0294601b7508 Namelen: 255 ? ? Type: overlayfs
Block size: 4096 ? ? ? Fundamental block size: 4096
Blocks: Total: 7584002 ? ?Free: 6796654 ? ?Available: 6441299
Inodes: Total: 1933312 ? ?Free: 1870694
purpleEndurer @ bash $?
?
3.4 stat -L 文件?或 stat --dereference 文件:取消對(duì)引用(跟隨)符號(hào)鏈接顯示并顯示該符號(hào)鏈接指向的文件的信息
例 3.4.1?用stat命令查看文件 /usr/share/zoneinfo/America/Cayman?的信息
purpleEndurer?@?bash?$?stat?/usr/share/zoneinfo/America/Cayman
??文件:'/usr/share/zoneinfo/America/Cayman'?->?'Panama'
??大?。?????????? 塊:0??????????IO?塊:4096???符號(hào)鏈接
設(shè)備:802h/2050d Inode:1986380?????硬鏈接:1
權(quán)限:(0777/lrwxrwxrwx)??Uid:(????0/????root)???Gid:(????0/????root)
最近訪問:2024-01-18?12:30:25.329503131?+0800
最近更改:2021-05-25?16:19:07.819859046?+0800
最近改動(dòng):2021-05-25?16:19:07.819859046?+0800
創(chuàng)建時(shí)間:-
?
從命令返回信息可以看到,/usr/share/zoneinfo/America/Cayman?的文件類型是符號(hào)鏈接,文件大小只有6個(gè)字節(jié),指向的文件是?Panama?
要想獲取/usr/share/zoneinfo/America/Cayman? 指向的文件? Panama?的信息,可以使用 -L選項(xiàng)。
例 3.4.2?用stat -L 命令查看文件 ?/usr/share/zoneinfo/America/Cayman?的信息
purpleEndurer?@?bash?$?stat?-L?/usr/share/zoneinfo/America/Cayman
??文件:'/usr/share/zoneinfo/America/Cayman'
??大小:203??????? 塊:8??????????IO?塊:4096???普通文件
設(shè)備:802h/2050d Inode:1986451?????硬鏈接:1
權(quán)限:(0644/-rw-r--r--)??Uid:(????0/????root)???Gid:(????0/????root)
最近訪問:2023-05-06?12:37:04.230304278?+0800
最近更改:2019-10-13?09:36:01.000000000?+0800
最近改動(dòng):2021-05-25?16:19:07.835859046?+0800
創(chuàng)建時(shí)間:-
?返回信息表示這個(gè)文件的大小是203字節(jié),文件類型是普通文件,那么這是不是文件? Panama?的信息呢?
例 3.4.3?用stat命令查看文件 /usr/share/zoneinfo/America/Panama??的信息
purpleEndurer?@?bash?$?stat?-L?/usr/share/zoneinfo/America/Panama
??文件:'/usr/share/zoneinfo/America/Panama'
??大小:203??????? 塊:8??????????IO?塊:4096???普通文件
設(shè)備:802h/2050d Inode:1986451?????硬鏈接:1
權(quán)限:(0644/-rw-r--r--)??Uid:(????0/????root)???Gid:(????0/????root)
最近訪問:2023-05-06?12:37:04.230304278?+0800
最近更改:2019-10-13?09:36:01.000000000?+0800
最近改動(dòng):2021-05-25?16:19:07.835859046?+0800
創(chuàng)建時(shí)間:-
?
命令返回的文件信息與例 3.4.2中的信息是一致的。
?3.5 stat?-c "格式" 文件?或 stat --format="格式"?文件?或?stat --printf="格式":自定義輸出
stat
命令有兩個(gè)選項(xiàng),可讓我們根據(jù)需要自定義輸出:-c "格式" (--format="格式")和?--printf="格式"
。
其中的格式?可以使用許多文件和文件系統(tǒng)的格式指令:
%a 以八進(jìn)制格式顯示訪問權(quán)限。
%A 以易于閱讀的格式顯示訪問權(quán)限。
%b 這是分配的塊數(shù)(請(qǐng)參見%B)。
%B 由%b報(bào)告的每個(gè)塊的字節(jié)大小。
%C 顯示SELinux安全上下文字符串。
%d 以十進(jìn)制格式顯示設(shè)備編號(hào)。
%D 十六進(jìn)制格式的設(shè)備號(hào)。
%f 以十六進(jìn)制顯示原始模式。
%F 顯示文件類型。
%g 打印所有者的組ID。
%G 打印所有者的組名。
%h 顯示硬鏈接數(shù)。
%i 打印出索引節(jié)點(diǎn)號(hào)。
%m 打印安裝點(diǎn)。
%n 顯示文件的文件名
%N 顯示帶符號(hào)引用的文件名,如果使用符號(hào)鏈接則取消引用
%o 打印最佳I/O傳輸大小提示。
%s 總大?。ㄒ宰止?jié)為單位)。
%t 主要設(shè)備類型(十六進(jìn)制),用于字符/塊設(shè)備特殊文件
%T 次要設(shè)備類型(十六進(jìn)制),用于字符/塊設(shè)備特殊文件
%u 顯示所有者的用戶ID。
%U 打印所有者的用戶名。
%w 顯示文件的產(chǎn)生時(shí)間,易于閱讀;–如果未知。
%W 打印文件誕生的時(shí)間,距離紀(jì)元后的秒數(shù);如果未知,則為0。
%x 上次訪問的時(shí)間,易于我們理解的格式。
%X 上次訪問的時(shí)間,距離紀(jì)元以來的秒數(shù)。
%y 顯示上次修改的最后時(shí)間,便于閱讀。
%Y 打印上次修改的時(shí)間,距離紀(jì)元以來的秒數(shù)。
%z 這是上次更改的時(shí)間,人類可以理解。
%Z 最后一次更改的時(shí)間,距離紀(jì)元以來的秒數(shù)。
這兩個(gè)選項(xiàng)的區(qū)別在于:
- 當(dāng)兩個(gè)或多個(gè)文件用作操作符時(shí),
--format
在每個(gè)操作數(shù)的輸出之后自動(dòng)添加換行符。? -
--printf
解釋反斜杠轉(zhuǎn)義字符。
例 3.5.1?用stat -c命令顯示Code目錄和test.txt?的文件類型、所有者的組ID和組名信息。
purpleEndurer @ bash $ stat -c "%F,%g,%G" Code test.txt
directory,1000,csdn
regular file,1000,csdn
purpleEndurer @ bash $?
?
例3.5.2?用stat --printf命令顯示Code目錄和test.txt?的文件類型、所有者的組ID和組名信息。
purpleEndurer @ bash $ stat --printf "%F,%g,%G" Code test.txt
directory,1000,csdnregular file,1000,csdnpurpleEndurer @ bash $?
??
可見,返回的目錄和文件信息在同一行,不容易區(qū)分,因此我們可以改進(jìn)一下,在格式串末尾加上換行符\n:
purpleEndurer @ bash $ stat --printf "%F,%g,%G\n" Code test.txt
directory,1000,csdn
regular file,1000,csdn
purpleEndurer @ bash $?
?
這樣看起來就清晰多了。
4?其它查看Inodes的命令
除了上節(jié)介紹的df -i或--inodes命令,以及本節(jié)介紹的 stat命令,我們還可以使用ls -i命令。
例如,查看當(dāng)目前下的目錄和文件的inodes:
purpleEndurer @ bash $ ls -i
1051744 Code ?1319965 test.txt
purpleEndurer @ bash $?文章來源:http://www.zghlxwxcb.cn/news/detail-804266.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-804266.html
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記40:stat命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!