bash script that computes used diskspace percentage for given partition - linux

I have a bash script that computes used diskspace percentage for given partition:
df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }
it doesn't work on a partition with long name because the name pushes the other columns to the next line, how do I fix this?
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup60-ROOT
2865493440 2222006740 497927968 82% /
tmpfs 30913808 0 30913808 0% /dev/shm
/dev/sda1 495844 103347 366897 22% /boot

Instead of parsing the entire "table" you could output the usage in percentage directly by using the --output parameter (see man 1 df for details):
$ df -k --output=pcent /dev/sda1
Use%
13%
That should be a lot easier to filter.
E.g. by creating an array with readarray in Bash 4:
$ readarray -t -s 1 percentage < <(df -k --output=pcent /dev/sda1)
$ echo "${percentage[0]// /}"
13%
Assigning the output of df to an array line by line:
$ percentage=($(df -k --output=pcent /dev/sda1))
$ echo "${percentage[1]}"
13%

The -P (portability) option for df use a output format compliant with posix and keeps everything in one line. You can also simplify the awk part using sel.

Related

converting the output of df -h into an array and then modified it's output

I want to converting the output of df -h into an array and then modified it's output with bash script.
For example the output of command is:
Filesystem Size Used Avail Use% Mounted on
/dev/sda4 28G 480M 26G 2% /var
/dev/sda2 28G 45M 26G 1% /tmp
/dev/sda5 275G 4.6G 256G 2% /home
tmpfs 790M 84K 789M 1% /run/user/1000
The output should be:
Filesystem Size Used Avail Use%
device4 28G 480M 26G 2%
device2 28G 45M 26G 1%
device5 275G 4.6G 256G 2%
Tmp 790M 84K 789M 1%
I know I should set IFS=$'\n' to have this output in an array but I have no idea how can I recognize device name and replace with the proper name.
Thank you for helping me to solve the problem.
This works for me:
#!/bin/bash
df -h | sed 's#\(.*\) \(.*\)$#\1#' | sed 's#Mounted##' | sed 's#/dev/sda\(.\)#device\1 #'
the first sed removes the last column
the second sed removes the word "Mounted". The last column header is "Mounted on", so the first sed only removes "on".
the last sed replaces /dev/sda by device.

Reading only two characters before a specific pattern in a linux file

I have a file like
Filesystem Size Used Avail Use% Mounted on
/abc/xyz/mnop
82G 7.7G 70G 10% /
hello 32G 922M 31G 3% /abc/asd
/abc/xyz 477M 118M 334M 27% /asd
/abc/xyz 50G 9.4G 38G 21% /ad
/abc/xyz 79G 27G 49G 36% /asd
/abc/xyz 30G 7.9G 21G 29% /sd
/abc/xyz 197G 2.4G 185G 2% /asd
xyz:/backups/abc
500G 18G 483G 4% /asdas
abc
1.9T 1.5T 405G 79% /media/Scratch
I want only the two characters before % i.e 10,3,27,21 and so on which i will compare one by one with a value 85 whether greater or less. But the first line use% should be skipped. Please suggest me how to get only those values in a variable one by one or in a file which i can compare with 85 using conditional statements.
i used grep -E -o ".{0,2}%" test.txt , but it is giving % with the values e.g 10% and also se% which i don't want.
Thanks
You can use awk:
df -h | awk 'NR>1{print $5+0}'
Or with a file:
awk 'NR>1{print $5+0}' test.txt
Using gnu grep:
grep -oP '\d+(?=%)' test.txt
Use below command it helps to get your desired results
cat text.txt | cut -d'%' -f1 | awk '{print $NF }'
Use perl: perl -ne 'print $1 . "," if /(\d{1,2})\%/' test.txt

Check only used space on CentOS server

I want to view only column named Use% i have tried to do like this:
df -h | awk '{print $%}'
But i have too long names in Filesystem colum and when i do that it show me mixed colum Use% and Mounted on.
How can I display only the Use% column
In a normal case, if the file-system column does NOT have whitespaces to worry about, e.g. for an output as
$ df -h
Filesystem Size Used Avail Use% Mounted on
C:/msys64 238G 132G 107G 56% /
doing df -h | awk 'NR>1{print $5}' would have been just fine to print the 5th column representing the usage. But for a case as complex as you said, when the file-system name is expected to have some spaces, looping through the columns to find the column matching % would be the right approach,
df -h | awk 'NR>1{for(i=1;i<=NF;i++) if (match($i,/\%$/)) { print $i; break } }'
56%

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.
You may get exact number of bytes with df:
df -B1 /
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/mapper/cl-root 32893632512 13080072192 18119061504 42% /
You may use awk,
df | awk '$1=="/dev/sda"{print $4}'
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}'
Only in Linux
df --output=avail
You can use an awk
df | grep sda | awk '{print $4}'
You can query disk status with stat as well. To query free blocks on filesystem mounted at /:
stat -f -c '%f' /
To get the result in bytes instead of blocks, you can use shell's arithmetic:
echo $((`stat -f -c '%f*%S' /`))
Very similar answers to the ones shown, but if you don't know the name of filesystem and are just looking for the total available space on the partition.
df -P --total | grep 'total' | awk '{print $4}'

Unix/Linux extract remaining available space on mount point from df output

I am writing a script (perl) and I would like to get remaining KB on some mount point. Command df -k return more information than I need.
~ df -k /var
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/bootdg/var
8267957 5749576 **2435702** 71% /var
Is there some way to cut result with AWK, to get just available space. But it give me same result I run it on Linux or Unix.
In Perl:
my %df = map { $_ = [ split ]; $_->[-1] => $_ } `df -P`;
print "Free space for /var: $df{'/var'}[3]\n";
man df:
-P, --portability
use the POSIX output format
Try :
df -h | awk '{print $4}' //$4 should be free file I think, you can change the variable according to your requirements

Resources