LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-06-2020, 02:27 PM   #1
Tonybe
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Rep: Reputation: Disabled
Printing Disk Space Less Than %80


Hi! I'm trying to write a script that prints the % of disk space if it's under %80:
Example:
/var/log %50 # it prints
/opt %81 # doesn't print

Script results:
/var/log %50

I'm using the df -Ph command but can't find out how to print it if it's under %80. df's printing everything. Any ideas? Googling's not helping. Thanks!
 
Old 03-06-2020, 02:31 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
It’d be easier to help with your script if you posted it. Hard to advise what’s wrong when we don’t know what you’re doing.
 
Old 03-06-2020, 02:44 PM   #3
Tonybe
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Doh! Sorry!

#!/bin/bash

SERVER_LIST=$1

if [ -z "$1" ]; then
echo -e "\nNo arguments supplied!\n"
echo -e "\nPlease call '$0 <argument>' to run this command!\n"
exit 1
fi



echo " "
for HOST in `cat $SERVER_LIST`
do
echo "Servername : $HOST"
echo -e "Disk Space for /opt: "; rosh -l root -n $HOST -t "df /opt/"
echo "------------------------------"
done
echo " "
 
Old 03-06-2020, 03:54 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by Tonybe View Post
Hi! I'm trying to write a script that prints the % of disk space if it's under %80:
Example:
/var/log %50 # it prints
/opt %81 # doesn't print

Script results:
/var/log %50

I'm using the df -Ph command but can't find out how to print it if it's under %80. df's printing everything. Any ideas? Googling's not helping. Thanks!
Quote:
Originally Posted by Tonybe View Post
Doh! Sorry!

Code:
#!/bin/bash

SERVER_LIST=$1

if [ -z "$1" ]; then
    echo -e "\nNo arguments supplied!\n"
    echo -e "\nPlease call '$0 <argument>' to run this command!\n"
    exit 1
fi



echo " "
for HOST in `cat $SERVER_LIST`
do
    echo "Servername : $HOST"
    echo -e "Disk Space for /opt: "; rosh -l root -n $HOST -t "df /opt/"
    echo "------------------------------"
done
echo " "
Please use [code] tags when posting code or output. See the link in my signature.

The code in the script doesn't match what you said in the OP, so we're still not clear on what you're doing.

df won't filter by size, you'd need to apply additional filters to the output of the df command. I'd suggest grep or awk.

As you say, df prints everything, so first you'd need to filter out (exclude) the lines that are not "real", those with Filesystem tmpfs (df -x), or include those that are (df -t) See man df

Then, you'd need to use grep or awk to select the lines you want to see, based on the Use% column. See their man pages.

I am curious why you want to only see partitions where the Use% is below 80%...I'd think the ones that are fuller -- higher than 80%, would be of more concern.
 
1 members found this post helpful.
Old 03-06-2020, 03:59 PM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
What is rosh?

I would use awk.
Code:
df|awk '$3/$2<0.8 {print}'
It takes the output of df, divides column 3 by column 2 and prints the line if the result is less than 0.8.
 
2 members found this post helpful.
Old 03-06-2020, 04:23 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by berndbausch View Post
What is rosh?

I would use awk.
Code:
df|awk '$3/$2<0.8 {print}'
It takes the output of df, divides column 3 by column 2 and prints the line if the result is less than 0.8.
Just learning awk: Couldn't one just select by column 5? Something like
Code:
df|awk '$5<80 {print}'
 
Old 03-06-2020, 04:36 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,770

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Code:
df   | awk '0+$5 < 80 {print}'
Without the 0+$5 it treats the value as text and not a number I do believe...
 
1 members found this post helpful.
Old 03-06-2020, 05:04 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Could be. I had to use $5<20 to get any filtering at all, but it seemed to work...would text compare as well? (I’d think not)
 
Old 03-06-2020, 05:23 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,770

Rep: Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933Reputation: 5933
Sure alphanumeric sorting.

df | awk '$5 > 10 {print}'
Code:
Filesystem                      1K-blocks      Used Available Use% Mounted on
tmpfs                             3953068     58104   3894964   2% /dev/shm
 
Old 03-07-2020, 11:09 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,819

Rep: Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212Reputation: 1212
And nothing speaks against the -P option that was initially suggested.
Certain versions of df break a long in two even if the output device is not a terminal - the -P prevents that.
 
Old 03-09-2020, 08:24 AM   #11
Tonybe
LQ Newbie
 
Registered: Nov 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks everyone!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find a file that's modified more than 2 days ago but less than 5 days BudiKusasi Linux - Newbie 1 02-09-2018 07:25 PM
Free disk space is less than 20% on volume /z/x/y Jacob-1 Linux - Server 1 01-22-2018 10:12 AM
Disk Space Used is Less Than Size Still Available Disk Space is Zero devUnix Linux - Server 10 10-04-2013 08:56 AM
Free disk space on / is less than 50MB qwertyjjj Linux - Server 1 10-13-2011 08:23 AM
In search of a distro for older PC (w/ less RAM & less HD space) fc6_user Linux - Distributions 5 04-10-2007 09:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration