How can I *only* get the number of bytes available on a disk in bash?

Ryan

df does a great job for an overview. But what if I want to set a variable in a shell script to the number of bytes available on a disk?

Example:

$ df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/sda             1111111111  2222222  33333333  10% /
tmpfs                  44444444      555  66666666   1% /dev/shm

But I just want to return 33333333 (bytes available on /), not the whole df output.

Gilles 'SO- stop being evil'

Portably:

df -P /dev/sda1 | awk 'NR==2 {print $4}'

The -P option ensures that df will print output in the expected format, and will in particular not break the line after the device name even if it's long. Passing the device name as an argument to df removes any danger from parsing, such as getting information for /dev/sda10 when you're querying /dev/sda1. df -P just prints two lines, the header line (which you ignore) and the one data line where you print the desired column.

There is a risk that df will display a device name containing spaces, for example if the volume is mounted by name and the name contain spaces, or for an NFS volume whose remote mount point contains spaces. In this case, there's no fully portable way to parse the output of df. If you're confident that df will display the exact device name you pass to it (this isn't always the case), you can strip it:

df -P -- "$device" | awk -vn=${#device} 'NR==2 {$0 = substr($0, n+1); print $3}'

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I get the amount of available memory portably across distributions?

来自分类Dev

How can I get the Mercurial Changeset ID from a revision number?

来自分类Dev

How can I repair the disk by command?

来自分类Dev

How can I zero just the padding bytes of a class?

来自分类Dev

why can I only read 2048 bytes at a time from an okhttp.Response InputStream?

来自分类Dev

How can I get the application path in C?

来自分类Dev

How can I get this Django template to render?

来自分类Dev

how can i solve ldconfig create link with version number

来自分类Dev

How can I display unique words contained in a Bash string?

来自分类Dev

How can I print contents of file given filename as stdin in bash?

来自分类Dev

How to get a disk usage map on linux?

来自分类Dev

How to get a disk usage map on linux?

来自分类Dev

How to get all available models in Magento?

来自分类Dev

How can I get a list of PHP errors occurred in a page?

来自分类Dev

How can I get path to current twig template dynamically?

来自分类Dev

How can I get the current weekday in Rust using the Chrono crate?

来自分类Dev

How can i get the fileinfo of all files in a folder with GetFile()?

来自分类Dev

How can I get the original resource in ngResource with a responseError interceptor?

来自分类Dev

How can i get a single widget(li) JSON in gridster?

来自分类Dev

How can I get around this annoying WiX issue?

来自分类Dev

How can I get last inserted id using Hibernate

来自分类Dev

How can I get a UTC timestamp in SQL400 on iSeries?

来自分类Dev

How can I get list of users of a specific domain?

来自分类Dev

How can I get 3D object dimensions in ThreeJS?

来自分类Dev

How can i get the value of a variable through the Super keyword?

来自分类Dev

How can I get a detailed log of django csrf failure?

来自分类Dev

How to replace all matches with an incrementing number in BASH?

来自分类Dev

How can i increase the number of ticks, a controller goes through, in a staggered animation (Flutter)

来自分类Dev

i can not compare two expressions in Bash

Related 相关文章

  1. 1

    How can I get the amount of available memory portably across distributions?

  2. 2

    How can I get the Mercurial Changeset ID from a revision number?

  3. 3

    How can I repair the disk by command?

  4. 4

    How can I zero just the padding bytes of a class?

  5. 5

    why can I only read 2048 bytes at a time from an okhttp.Response InputStream?

  6. 6

    How can I get the application path in C?

  7. 7

    How can I get this Django template to render?

  8. 8

    how can i solve ldconfig create link with version number

  9. 9

    How can I display unique words contained in a Bash string?

  10. 10

    How can I print contents of file given filename as stdin in bash?

  11. 11

    How to get a disk usage map on linux?

  12. 12

    How to get a disk usage map on linux?

  13. 13

    How to get all available models in Magento?

  14. 14

    How can I get a list of PHP errors occurred in a page?

  15. 15

    How can I get path to current twig template dynamically?

  16. 16

    How can I get the current weekday in Rust using the Chrono crate?

  17. 17

    How can i get the fileinfo of all files in a folder with GetFile()?

  18. 18

    How can I get the original resource in ngResource with a responseError interceptor?

  19. 19

    How can i get a single widget(li) JSON in gridster?

  20. 20

    How can I get around this annoying WiX issue?

  21. 21

    How can I get last inserted id using Hibernate

  22. 22

    How can I get a UTC timestamp in SQL400 on iSeries?

  23. 23

    How can I get list of users of a specific domain?

  24. 24

    How can I get 3D object dimensions in ThreeJS?

  25. 25

    How can i get the value of a variable through the Super keyword?

  26. 26

    How can I get a detailed log of django csrf failure?

  27. 27

    How to replace all matches with an incrementing number in BASH?

  28. 28

    How can i increase the number of ticks, a controller goes through, in a staggered animation (Flutter)

  29. 29

    i can not compare two expressions in Bash

热门标签

归档