linux系统df和du命令的区别

时间:2022-05-30 17:11:45
发现一台用户的电脑,df检查出来的/磁盘空间占用了16G,比用du查看得到的磁盘空间大的多,du查看/下所有程序目录加起来还不到5G。这是什么原因呢? 即便是有隐藏文件,查了也很小啊。
因为df和du的统计机制不一样。 du会把指定目录下所有文件、目录、目录下的文件都统计。是建立在文件系统能看到的的确确是有这样一些文件的基础上的。也就是说我们能在文件系统里面看到的文件才会被du统计。
df(df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。): 这要从程序写文件的方式来谈起,一个进程要向一个文件里面写东西,那么会得到一个pointer, 然后向指针指向的文件(磁盘区域)写入。假如由于某些原因(也可能人为)被指向的文件被删除了,但是( writecall)并不知道文件是否还在,就还会继续向里面写,不管文件是否存在,磁盘块还是被写操作使用。 于是因为文件没有了,所以du统计不到写入的这些磁盘块,但是因为写的进程还在,磁盘空间没有被释放,所以df却能统计到,所以df统计的结果比du大很多。 如何解决呢? 1.停止系统上的程序。 2.如果还是不行,unmount一下文件系统看看 3.重启系统
原文:Whenyou open a file, you get apointer.  Subsequent writes tothis file references this filepointer.  The write call does notcheck to see if the file
is there or not.  It just writesto the specified number of characters starting
at a predeterminedlocation.  Regardless of whetherthe file exist or not, disk
blocks are used by the write operation.


下面是整个原文: This article explains how reporting disk usage du and reportingfree disk space
on file systems df may show different numbers.

du
--

The du user command gives the number of kilobytes contained in allfiles and,
recursively, directories within each specified directory or file(filename).
If filename is missing, `.' (the current directory) isused.  A filewhich
has multiple links to it is only counted once.

EXAMPLE:

  system % du

    ./jokes
  33  ./squash
  44  ./tech.papers/lpr.document
  217  ./tech.papers/new.manager
  401  ./tech.papers
  144  ./memos
  80  ./letters
  388  ./window
  93  ./messages
  15  ./useful.news
  1211 .

Note that the last number, 1211 is the grand total (in kilobytes)for the
directory.

df
--

The df user command displays the followinginformation:

  amount of disk space occupiedby currently mounted file systems
  the amount of used andavailable space
  how much of the file system'stotal capacity has been used

Used without arguments, df reports on all mounted filesystems.

EXAMPLE:

  system % df

  Filesystem  kbytes  used  avail  capacity  Mountedon
  /dev/ip0a   7445    47141986   70%     /
  /dev/ip0g  42277   352912758   93%     /usr

Note: used plus avail is less than the amount of space in the filesystem
(kilobytes) because the system reserves a fraction of the space inthe file
system to allow its allocation routines to workwell.  The amount reservedis
typically about 10%.  (This maybe adjusted using the tunefscommand.  Referto
the man pages on tunefs(linux系统df和du命令的区别 formore information.)  When all thespace on a file
system, except for this reserve, is in use, only the super-user canallocate
new files and data blocks to existingfiles.  This, however, may causethe file
system to be overallocated.  When a file system isover allocated in this way,
df may report that the file system is more than 100%utilized.

If arguments to df are disk partitions (for example, /dev/ip0as orpath names),
df produces a report on the file system containing the namedfile.  Thus, df
shows the amount of space on the file system containing the currentdirectory.

Problem Definition
------- ----------

This section gives the technical explanation of why du and dfsometimes report
different totals of disk space usage.

When a program that is running in the background writes to a filewhile the
process is running, the file to which this process is writing isdeleted.
Running df and du shows a discrepancy in the amount of disk spaceusage.  The
df command shows a higher value.

Explanation Summary
----------- -------

When you open a file, you get apointer.  Subsequent writes tothis file
references this filepointer.  The write call does notcheck to see if the file
is there or not.  It just writesto the specified number of characters starting
at a predeterminedlocation.  Regardless of whetherthe file exist or not, disk
blocks are used by the write operation.

The df command reports the number of disk blocks used while du goesthrough the
file structure and and reports the number of blocks used by eachdirectory.  As
far as du is concerned, the file used by the process does notexist, so it does
not report blocks used by this phantomfile.  But df keeps track of diskblocks
used, and it reports the blocks used by this phantomfile.