使用df获取可用磁盘空间以显示kb中的可用空间?

时间:2022-10-24 17:14:08

I'm trying to output the amount of free disk space on the filesystem /example.

我正在尝试输出文件系统/示例中的可用磁盘空间量。

If I run the command df -k /example I can get good information about available disk space in kb but only by being human and actually looking at it.

如果我运行命令df -k / example我可以获得有关kb中可用磁盘空间的良好信息,但只能通过人工并实际查看它。

I need to take this data and use it somewhere else in my shell script. I initially thought about using cut but then my script wont be portable to other disks as free disk space will vary and cut will not produce accurate results.

我需要获取这些数据并在我的shell脚本中的其他地方使用它。我最初考虑使用剪切,但后来我的脚本不能移植到其他磁盘,因为可用磁盘空间会有所不同,切割不会产生准确的结果。

How can I get output of just the free disk-space of example in kb?

如何在kb中获得示例的可用磁盘空间的输出?

4 个解决方案

#1


45  

To get the output of df to display the data in kb you just need to use the -k flag:

要获得df的输出以kb显示数据,您只需使用-k标志:

df -k

Also, if you specify a filesystem to df, you will get the values for that specific, instead of all of them:

此外,如果您将文件系统指定为df,您将获得该特定的值,而不是所有值:

df -k /example

Regarding the body of your question: you want to extract the amount of free disk space on a given filesystem. This will require some processing.

关于问题的主体:您想要提取给定文件系统上的可用磁盘空间量。这将需要一些处理。

Given a normal df -k output:

给定正常的df -k输出:

$ df -k /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        7223800 4270396   2586456  63% /

You can get the Available (4th column) for example with awk or cut (previously piping to tr to squeeze-repeats (-s) for spaces):

您可以使用awk或cut获取可用(第4列)(之前使用tr管道以对空格进行挤压重复(-s)):

$ df -k /tmp | tail -1 | awk '{print $4}'
2586456
$ df -k /tmp | tail -1 | tr -s ' ' | cut -d' ' -f4
2586456

As always, if you want to store the result in a variable, use the var=$(command) syntax like this:

与往常一样,如果要将结果存储在变量中,请使用var = $(command)语法,如下所示:

$ myUsed=$(df -k /tmp | tail -1 | awk '{print $4}')
$ echo "$myUsed"
2586456

Also, from the comment by Tim Bunce you can handle long filesystem names using --direct to get a - instead, so that it does not print a line that breaks the engine:

此外,根据Tim Bunce的评论,您可以使用--direct来处理长文件系统名称,而不是打印一条打破引擎的行:

$ df -k --direct /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
-                7223800 4270396   2586456  63% /

#2


9  

You can use stat(2) command to display free blocks and also to find out how large each block is, e.g.

您可以使用stat(2)命令显示空闲块,还可以查看每个块的大小,例如:

stat -f --printf="%a %s\n" /

will display number of free blocks (%a) on a given file system (/) followed by a block size (%s). To get size in kB, you can use bc(1) command as in the following example:

将显示给定文件系统上的空闲块数(%a)(/),后跟块大小(%s)。要获得以kB为单位的大小,可以使用bc(1)命令,如以下示例所示:

stat -f --printf="%a * %s / 1024\n" / | bc

Finally, to put this into a variable is just a matter of using backtick substitution (or $() as in the first answer):

最后,将它放入变量只是使用反引号替换(或第一个答案中的$()):

SOMEVAR=`stat -f --printf="%a * %s / 1024\n" / | bc`

#3


7  

Show interesting columns only

仅显示有趣的列

 df /example --total -k -h  --output=source,avail
  • --total = grand total at the end
  • --total =最后的总计
  • -k = block size 1K
  • -k =块大小为1K
  • -h = human readable
  • -h =人类可读
  • --output=[FIELD_LIST] column list to show separated by ","
  • --output = [FIELD_LIST]列列表以“,”分隔

Not totally standard (I have seen --output just in Ubuntu man pages), in this case Awk and others just to remove columns are not necessary.

不完全标准(我在Ubuntu手册页中看到--output),在这种情况下,Awk和其他只是删除列是没有必要的。

#4


0  

This is another solution:

这是另一种解决方案:

df --output=avail -m /example | tail -1

output:

输出:

6415

6415

#1


45  

To get the output of df to display the data in kb you just need to use the -k flag:

要获得df的输出以kb显示数据,您只需使用-k标志:

df -k

Also, if you specify a filesystem to df, you will get the values for that specific, instead of all of them:

此外,如果您将文件系统指定为df,您将获得该特定的值,而不是所有值:

df -k /example

Regarding the body of your question: you want to extract the amount of free disk space on a given filesystem. This will require some processing.

关于问题的主体:您想要提取给定文件系统上的可用磁盘空间量。这将需要一些处理。

Given a normal df -k output:

给定正常的df -k输出:

$ df -k /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1        7223800 4270396   2586456  63% /

You can get the Available (4th column) for example with awk or cut (previously piping to tr to squeeze-repeats (-s) for spaces):

您可以使用awk或cut获取可用(第4列)(之前使用tr管道以对空格进行挤压重复(-s)):

$ df -k /tmp | tail -1 | awk '{print $4}'
2586456
$ df -k /tmp | tail -1 | tr -s ' ' | cut -d' ' -f4
2586456

As always, if you want to store the result in a variable, use the var=$(command) syntax like this:

与往常一样,如果要将结果存储在变量中,请使用var = $(command)语法,如下所示:

$ myUsed=$(df -k /tmp | tail -1 | awk '{print $4}')
$ echo "$myUsed"
2586456

Also, from the comment by Tim Bunce you can handle long filesystem names using --direct to get a - instead, so that it does not print a line that breaks the engine:

此外,根据Tim Bunce的评论,您可以使用--direct来处理长文件系统名称,而不是打印一条打破引擎的行:

$ df -k --direct /tmp
Filesystem     1K-blocks    Used Available Use% Mounted on
-                7223800 4270396   2586456  63% /

#2


9  

You can use stat(2) command to display free blocks and also to find out how large each block is, e.g.

您可以使用stat(2)命令显示空闲块,还可以查看每个块的大小,例如:

stat -f --printf="%a %s\n" /

will display number of free blocks (%a) on a given file system (/) followed by a block size (%s). To get size in kB, you can use bc(1) command as in the following example:

将显示给定文件系统上的空闲块数(%a)(/),后跟块大小(%s)。要获得以kB为单位的大小,可以使用bc(1)命令,如以下示例所示:

stat -f --printf="%a * %s / 1024\n" / | bc

Finally, to put this into a variable is just a matter of using backtick substitution (or $() as in the first answer):

最后,将它放入变量只是使用反引号替换(或第一个答案中的$()):

SOMEVAR=`stat -f --printf="%a * %s / 1024\n" / | bc`

#3


7  

Show interesting columns only

仅显示有趣的列

 df /example --total -k -h  --output=source,avail
  • --total = grand total at the end
  • --total =最后的总计
  • -k = block size 1K
  • -k =块大小为1K
  • -h = human readable
  • -h =人类可读
  • --output=[FIELD_LIST] column list to show separated by ","
  • --output = [FIELD_LIST]列列表以“,”分隔

Not totally standard (I have seen --output just in Ubuntu man pages), in this case Awk and others just to remove columns are not necessary.

不完全标准(我在Ubuntu手册页中看到--output),在这种情况下,Awk和其他只是删除列是没有必要的。

#4


0  

This is another solution:

这是另一种解决方案:

df --output=avail -m /example | tail -1

output:

输出:

6415

6415