Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 997
Ryan Avatar asked Oct 21 '15 01:10

Ryan


People also ask

How do I check a bytes file?

Using the ls Command–l – displays a list of files and directories in long format and shows the sizes in bytes.

Which command is used to display the amount of available disk space for file systems on which the invoking user has appropriate read access?

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.

How do you read first 10 bytes of a binary file?

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...

How do I find the size of a file in bash?

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.


1 Answers

You may use awk,

df | awk '$1=="/dev/sda"{print $4}'
like image 51
Avinash Raj Avatar answered Oct 21 '22 05:10

Avinash Raj