4

I would like to simplify this one liner that will print only if a partition is greater than or equal to 60% utilization. I would also like to append a percent sign on to the utilization value.

df -h | sed 's/%//g' | awk '{print $6,$5 }' | awk '{ if($2 >=60 ) print}' | column -t

I am using a BSD variant and I do not have the common GNU tools installed.

  • Sorry about the title, first time user here. Both commands worked great thank you !!!! – Steve Dec 29 '16 at 17:36
1

Since this is tagged awk let us work on that. The first awk is just being used to move columns 6 and 5 to 1 and 2, so we can get rid of that and just use 6 and 5 in the second awk.

df -h | sed 's/%//g' | awk '{ if($5 >=60 ) print $6, $5 }' | column -t

We can add in the percent sign as well

df -h | sed 's/%//g' | awk '{ if($5 >=60 ) print $6, $5 "%"}' | column -t

We can remove the sed and do that within the awk

df -h | awk '{gsub(/%/,""); if($5 >=60) print $6, $5 "%"}' | column -t

We could remove the column -t by letting awk store the values in an array, and then in an END statement loop over the array to figure out the longest name and print accordingly, but this is no longer a one liner in my opinion.

  • EXCELLENT THANKS !!! I am new with sed and awk so I really appreciate this. the last command failed but then I only have bourne shell and T shell. YOU ROCK ! – Steve Dec 29 '16 at 17:33
  • @Steve how does the last command fail? It works for me. In general this script is a bit fragile as the output of df -h changes if you have remote filesystems with long names, which I didn't addess. – icarus Dec 29 '16 at 17:57
  • Filesystem Size Used Avail Capacity Mounted on /dev/da0s4d 248M 10K 228M 0% /tmp /dev/da0s2d 989M 293M 617M 32% /usr /dev/da0s4e 16G 57M 15G 0% /var /dev/da0s4f 55G 123M 50G 0% /var/log /dev/da0s4g 43G 122M 40G 0% /var/spool /dev/da0s1a 2.9G 845M 1.8G 31% /vcdrom Shakezula:Admn {7} % df -h | awk '{sub(/%/,"",$5); if($5 >=60) print $6, $5 "%"}' | column -t Mounted Capacity% it doesnt have any output. Your other suggestions worked perfectly though, – Steve Dec 29 '16 at 19:04
  • df -h | awk '{sub(/%/,"",$5); if($5 >=60) print $6, $5 "%"}' | column -t Mounted Capacity% – Steve Dec 29 '16 at 19:04
  • This is all the output stated Mounted Capacity% – Steve Dec 29 '16 at 19:06
2

A bit easier with simple grep. Only the two columns won't be swapped.

$ df -h|egrep -o "(100|[6-9].)% /.*$"
65% /
93% /home
  • df -h | egrep -o "(100|[6-9].)% /.*$\" Illegal variable name. – Steve Dec 29 '16 at 18:59
  • I ran it but it errored out df -h | egrep -o "(100|[6-9].)% /.*$\" Illegal variable name. – Steve Dec 29 '16 at 19:00
  • @Steve: which shell do you use? – Ipor Sircer Dec 29 '16 at 19:15
  • I use TCSH and bourne shell – Steve Dec 29 '16 at 19:46
  • egrep -o is a GNU extension. In general you should always use single quotes rather than double quotes unless you want the substitutions to happen. – icarus Dec 29 '16 at 19:46
1

You may replace the sed with tr -d "%" and combine the two invocations of awk into a single one:

$ df -h | tr -d "%" | awk '$5 >= 60 { print $6, $5 }' | column -t

To get the percentage sign in:

$ df -h | tr -d "%" | awk '$5 >= 60 { printf("%s %s%s\n", $6, $5, (NR>1?"%":"")) }' | column -t

The second version of the Awk script conditionally adds a % to the second output column depending on whether NR is greater than one or not. If NR is one, we don't add a % since that's the header line.

1

GNU df command has --output switch , which allows you to display only specific columns. Knowing that, and combining with knowledge that we can replace text in awk, the job becomes slightly easier:

$ df --output=source,pcent | awk '{gsub(/\%/," ");if($2 > 60) print}'                                                        
Filesystem     Use 
/dev/sda1       63 

Python can do all that as well. As one-liner:

df --output=source,pcent | python -c "import sys;lines=sys.stdin.readlines();lines.pop(0);print [l.strip() for l in lines if int(l.replace('%',' ').split()[1]) > 60]"

Or as script for more readability:

bash-4.3$ cat filter_df.py 
#!/usr/bin/env python
import sys;
for index,line in enumerate(sys.stdin):
    if index == 0: continue
    if int(line.replace('%',' ').split()[1]) > 60 :
        print line
bash-4.3$ df --output=source,pcent | ./filter_df.py 
/dev/sda1       63%
  • df --output=source,pcent | awk '{gsub(/\%/," ");if($2 > 60) print}' df: illegal option -- - usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...] – Steve Dec 29 '16 at 20:20
  • @Steve what system are you using ? Notice that I mentioned GNU df. What is your df --version ? – Sergiy Kolodyazhnyy Dec 29 '16 at 20:21
  • Apparently an old one. I am new to AWK for the most part. df --version df: illegal option -- - usage: df [-b | -g | -H | -h | -k | -m | -P] [-acilnT] [-t type] [file | filesystem ...] – Steve Dec 29 '16 at 20:24
  • @Steve well . . . that's peculiar. Does df --help say anything about the version ? By the way, welcome to the ranks of AWK users ;) – Sergiy Kolodyazhnyy Dec 29 '16 at 20:26
  • 1
    yes sir, BSD I am on – Steve Dec 29 '16 at 20:58
0

If we can use Perl:

df | perl -nE 'say "$2\t$1"   if /(\d+)% (.*)/ and $1>60'

or

df | perl -nE '/(\d+)% (.*)/ and $1>60 and  say "$2\t$1"'
  • Unrecognized switch: -E (-h will show valid options). I ran it but using bourne shell, not BASH, so that is likly my prob – Steve Dec 29 '16 at 19:02
  • @Steve, you probably have a very old Perl version... – JJoao Dec 29 '16 at 19:14
  • 1
    And it will not be a bash vs sh issue. – icarus Dec 29 '16 at 19:41
0

Show any partitions with more than 60% usage (either size or inodes):

df -h | grep '[67890].%'

it is that simple. (0 is there to catch the ones with 100℅)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.