The file size calculated by du and ls under Linux is 10 times different?
事情是这样的,昨天开发让我给他倒个日志,由于历史原因吧,没有日志系统,直接上服务器看了下他要的日志大小
[root@xxxxx apps]# du -hs smartorder.log 9.0G smartorder.log
看了下,不小,我问开发,要整个日志吗,还是可以按日期给他切一下,他说要整个,我想着日志文件,通常压缩完也没多少,就压缩了一下,压缩完确实也不是太大
[root@xxxxx apps]# du -hs smartorder.log.tar.gz 744M smartorder.log.tar.gz
没多想,我就给他down下来发过去了
晚上回家,哥们找到我了

我说不可能啊,怎么可能100G,吓到我了,他还给我发了个截图

确实是100G,没办法,开电脑上服务器查看,通过ls指定–block-size查看大小
[root@xxxxx apps]# ls -l --block-size=G smartorder.log -rw-r--r-- 1 root root 103G Oct 21 09:00 smartorder.log
这。。。。。
后来想起来,du查找的时候是按照block大小计算的,计算的是实际占用磁盘空间的大小,但即便这样,按道理,和ls命令查出来的大小不会差太多,但是凡事有例外
linux中有一种文件叫做sparse file,它可以延迟分配磁盘空间,类似于我们用的虚拟机,在创建虚拟机的时候,可以分配20G的磁盘空间,但是你创建完后,去查看宿主机磁盘占用,确实际没有占用那么多
Sparse File专业名称叫稀疏文件,这是Unix类和NTFS等文件系统的一个特性
开始时,一个sparse file不包含数据,也没有分配到用来存储用户数据的磁盘空间。当数据被写入sparse file时,NTFS逐渐为其分配磁盘空间。
Sparse File以64KB为单位增量增长,所以磁盘上sparse file的大小总是64KB的倍数
Sparse File就是在文件中留有很多空余空间,留备将来插入数据使用。如果这些空余空间被ASCII码的NULL字符占据,并且这些空间相当大,那么,这个文件就被称为稀疏文件,而且,并不分配相应的磁盘块。
很显然,我上面遇到的就是一个Sparse File,那么这么大的一个sparse file,怎么处理?
其实cp命令有一个针对sparse文件拷贝优化的参数–spare=WHEN,WHEN的值为auto、always、never,默认为auto,如果设置为never则会自动填数据
同样支持sparse的命令还有tar、cpio、rsync,下面通过tar试下
[root@bibang-server apps]# tar cSf smartorder.log.tar smartorder.log [root@bibang-server apps]# ls -l --block-size=G smartorder.log.tar -rw-r--r-- 1 root root 10G Oct 21 09:57 smartorder.log.tar
如何查找系统上的sparse file,或确认文件是否是sparse file?
[root@xxxxx apps]# find ./smartorder.log -type f -printf "%S\t%p\n" 0.0886597 ./smartorder.log
如上,通过find命令,find命令通过%S输出的结果中,最左边一列显示的值是(BLOCK-SIZE*st_blocks/st_size),sparse file的大小通常是小于1.0的
如果要查找文件系统上所有稀疏文件,可以通过以下find命令
find / -type f -printf "%S\t%p\n" | gawk '$1
ok,今天的内容就到这里了,欢迎转发、在看、关注!
The above is the detailed content of The file size calculated by du and ls under Linux is 10 times different?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)
