当前位置:网站首页>How much disk space does a new empty file take?
How much disk space does a new empty file take?
2020-11-06 21:04:00 【Zhang Yanfei Allen】
Today, let's think about a simple question . stay Linux You can use touch
Command to create an empty file :
touch empty_file.txt
After the operation is completed , Whether to consume some of our disk space ? If necessary , About how much ? Um. , Yes , This question is more simple than you think , But I don't know if you can give yourself a satisfactory answer .
My previous articles are all about the physical layer of disk , But it may not be enough to help understand the problems associated with the document . Starting today, let's move up the physical plane , To Linux Find the answer in the file system principle .
True knowledge comes from practice
I think it's possible to abandon the kernel principle first , It's more interesting to experiment directly by hand . You must know ls
This command allows you to view the file size , So let's take a look at it .
# touch abcdefghigklmn.txt
# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 17 17:49 empty.file
forehead ,ls
The command tells me that this empty file takes up 0. The size of the file is really 0, Because we haven't written anything to the file yet . But what we have to think about now is , Whether an empty file takes up disk space . So intuition tells us it's absolutely impossible , There is an extra file on the disk , How can it be that there is no space cost at all !
To solve this mystery , You need help df command . Input df –i
# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
......
/dev/sdb1 2147361984 12785019 2134576965 1% /search
This output helps us to show what's going on in our file system inode Usage situation . Be careful IUsed yes 12785019. We continue to create an empty file
# touch empty_file2.txt
df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
......
/dev/sdb1 2147361984 12785020 2134576964 1% /search
[@bjzw_46_76 temp]#
Now pay attention to IUsed Turned into 12785020.
ha-ha , One of our conclusions is that . Creating an empty file will take up a Inode.
Elaborate inode
that inode What kind of file related information is stored in it ? Let's take a look at the source code of the kernel . You can download one linux Source code . With ext2 File systems, for example , In my download of linux-2.6 Files in fs/ext2/ext2.h in , You can find the kernel for inode Definition of structure . The structure is more complex , It mainly stores some other data besides the contents of the file , Let's pick some of the more critical intercepts :
struct ext2_inode {
__le16 i_mode; # File permissions
__le16 i_uid; # File owner ID
__le32 i_size; # File Bytes size
__le32 i_atime; # The last time the file was accessed
__le32 i_ctime; # File creation time
__le32 i_mtime; # When the document was modified
__le32 i_dtime; # When the file was deleted
__le16 i_gid; # File group ID
__le16 i_links_count; # This document inode The number of times it was connected
__le32 i_blocks; # Of documents block Number
......
__le32 i_block[EXT2_N_BLOCKS]; # An array that points to blocks that store file data
......
You can see the user associated with the file 、 Visit time and so on exist inode Medium . In addition to include/linux/fs.h in , There's another. VFS Level of inode The definition of , We don't diverge here . Use stat Command to see the file directly inode Data in the .
# stat test
File: `test'
Size: 0 Blocks: 0 IO Block: 1024 regular empty file
Device: 801h/2049d Inode: 26 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-01 12:14:31.000000000 +0800
Modify: 2020-03-01 12:14:31.000000000 +0800
Change: 2020-03-01 12:14:31.000000000 +0800
Every inode How big is it ?dumpe2fs I can tell you (XFS Use xfs_info).
# dumpe2fs -h /dev/mapper/vgroot-lvroot
dumpe2fs 1.41.12 (17-May-2010)
......
Inode size: 256
Inode size Represent each Inode Size . On my machine , Every inode All are 256 byte . Two inode Is exactly the size of the disk sector 512 byte .
Where is the file name
inode We've seen all the structures , After working for a long time, I don't know if I have found a problem ,inode There is no file name stored in it !! that , Where is the file name ?
stay fs/ext2/ext2.h
in , I found the following folder related structures
struct ext2_dir_entry {
__le32 inode; /* Inode number */
__le16 rec_len; /* Directory entry length */
__le16 name_len; /* Name length */
char name[]; /* File name, up to EXT2_NAME_LEN */
};
This structure is our usual folder . you 're right , The file name exists in the folder data structure to which it belongs , It's one of them char name[]
Field . Along with the file name , The files in the folder were also recorded inode Etc .
Conclusion
-
- A new empty file needs to be consumed inode, To save users 、 Creation time and other metadata .
-
- To create an empty file, you need to consume all of its directories block A certain amount of space in , This space is used to hold the file name , jurisdiction 、 Time and other information
therefore , It looks like a new empty file , As long as you want to dig , Can really dig out a lot of knowledge . Finally, I'd like to share a fault encountered by my classmates in our team . One of our offline machines has stopped cooking directly , After restart, the reason is inode It's consumed . Tracing again found that a process created too many empty log files . Even though the files are empty , however inode It's wasted . Later, the students in charge changed the logic of creating log files , Deleted the extra empty files , The machine is back to normal .
Development of hard disk album of internal training :
- 1. Disk opening : Take off the hard coat of the mechanical hard disk !
- 2. Disk partitioning also implies technical skills
- 3. How can we solve the problem that mechanical hard disks are slow and easy to break down ?
- 4. Disassemble the SSD structure
- 5. How much disk space does a new empty file take ?
- 6. Only 1 How much disk space does a byte file actually take up
- 7. When there are too many documents ls Why is the command stuck ?
- 8. Understand the principle of formatting
- 9.read How much disk does a byte of file actually take place on IO?
- 10.write When to write to disk after one byte of file IO?
- 11. Mechanical hard disk random IO Slower than you think
- 12. How much faster is a server equipped with a SSD than a mechanical hard disk ?
My official account is 「 Develop internal skill and practice 」, I'm not just talking about technical theory here , It's not just about practical experience . It's about combining theory with practice , Deepen the understanding of theory with practice 、 Use theory to improve your technical practice ability . Welcome to my official account , Please also share with your friends ~~~
版权声明
本文为[Zhang Yanfei Allen]所创,转载请带上原文链接,感谢
边栏推荐
- 【ElasticSearch搜索引擎】
- WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
- Small program introduction to proficient (2): understand the four important files of small program development
- An article taught you to use HTML5 SVG tags
- mongo 用户权限 登录指令
- Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
- How about small and medium-sized enterprises choose shared office?
- Understanding formatting principles
- CloudQuery V1.2.0 版本发布
- The importance of big data application is reflected in all aspects
猜你喜欢
An article takes you to understand CSS gradient knowledge
Use modelarts quickly, zero base white can also play AI!
Share with Lianyun: is IPFs / filecoin worth investing in?
行为型模式之解释器模式
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
How to hide part of barcode text in barcode generation software
Small program introduction to proficient (2): understand the four important files of small program development
Tron smart wallet PHP development kit [zero TRX collection]
Zero basis to build a web search engine of its own
随机推荐
Gather in Beijing! The countdown to openi 2020
mongo 用户权限 登录指令
代码重构之法——方法重构分析
Multi robot market share solution
What is the tensor in tensorflow?
The legality of IPFs / filecoin: protecting personal privacy from disclosure
Filecoin has completed a major upgrade and achieved four major project progress!
Asp.Net Core learning notes: Introduction
Swagger 3.0 brushes the screen every day. Does it really smell good?
解决 WPF 绑定集合后数据变动界面却不更新的问题
hdu3974 Assign the task線段樹 dfs序
[efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
What are manufacturing and new automation technologies?
【應用程式見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路檢視
image operating system windows cannot be used on this platform
Junit测试出现 empty test suite
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
小游戏云开发入门
This project allows you to quickly learn about a programming language in a few minutes
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)