Check only used space on CentOS server - linux

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%

Related

Why does piping into BASH command groups sometimes work?

I have used the following command for a while to keep headers on ps output.
ps aux | { head -1; grep root; }
The output will look something like the following.
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
root 142 0.0 0.0 1234567 2520 ?? Ss 3:14AM 0:08.03 /usr/sbin/notifyd
root 55 0.0 0.0 7890123 2460 ?? Ss 3:14AM 0:01.94 /usr/sbin/syslogd
...
However, when used with other command line programs the output is not as expected.
Take the following df example.
df -h
Outputs the following.
Filesystem Size Used Avail Use% Mounted on
/dev/disk1s1 466G 103G 362G 22% /
/dev/disk1s4 466G 1.1G 362G 1% /blah/blah/blah
Using df in a similar syntax as the above example with ps.
df -h | { head -1; grep disk1; }
Outputs the following.
Filesystem Size Used Avail Use% Mounted on
The expectation is that the output would look essentially the same as the straight df -h command.
Why does this differ from ps?
I feel that knowing these differences will help me understand BASH processing more completely.
Thank you!
It's because head is buffering its input. It reads into a large buffer from the pipe, then starts extracting lines from that buffer. After it has read and printed the first N lines, it exits. Then grep starts reading from the pipe. But anything that head already read into its buffer is not available.
The reason it seems to work with ps is because it produces lots of output, which doesn't fit into this buffer. grep is then able to process the rest of the output. But I think if you check carefully you'll see that the result is incomplete.
The output of df is much smaller, it all fits into the buffer that head uses, so there's nothing left for grep to process.
The buffer size is probably something like 4K characters.
You can do what you want with awk:
df -h | awk 'NR == 1 || /disk1/'
ps aux | awk 'NR == 1 || /root/'
NR is the line number, so this prints the line if it's the first line or it matches the regexp.
Sed(1) can also be used to filter output in this case:
ps aux | sed -n '1p; /root/p'
-n: Don't echo line of input to standard output after all commands have been applied to it.
1p; "address" of line1, with 'p' to print pattern space
/root/p; "address" of /regexp/ matching "root", with 'p' to print pattern space
Alternative:
ps aux | sed '1p; /root/p; d;'
Some systems may require ps -aux i.t. dash (-) to prefix options. Linux and *BSD systems do not (cannot be sure how macOS behaves, I don't have such a system to check out).

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

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}'

bash script that computes used diskspace percentage for given partition

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.

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