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.
Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes.
df (abbreviation for disk free) is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access.
1. Using head or dd but in a single pass. This is the more efficient, as your file is read only 1 time, the first 10 byte goes to head_part and the rest goes to tail_part . Note: second redirection >tail_part could be place outside of whole list ( { ...;} ) as well...
Another method we can use to grab the size of a file in a bash script is the wc command. The wc command returns the number of words, size, and the size of a file in bytes.
You may use awk,
df | awk '$1=="/dev/sda"{print $4}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With