We would like to automate the disk cleanup process by using a shell script on an AIX 7. We need to write a shell script to clean disk according to disk percentage
Sample output of df -Pg
user:host:/:>df -Pg
Filesystem GB blocks Used Available Capacity Mounted on
/dev/hd4 5.00 0.05 4.95 2% /
/dev/hd2 6.00 3.63 2.37 61% /usr
/dev/work1lv 1074.50 377.46 697.04 36% /work1
... some lines are ommitted and it continues
We want to check only capacity percentage by using cut command We couldnt list only 5th column that is given as parameter to cut command -f. If we succeeded displaying properly, we will continue for cleaning part using shell script.
df -Pg | cut -f5 -d' '
The output of the command above was not what we expect.
The cut
command considers each and every delimiter to begin a new field; if you told it to delimit the output based on spaces and tabs, every space creates a new field. That's why you got unexpected output.
Printing field 5 could be done more directly with awk
, which combines any number of sequential spaces into one delimiter: df -Pg | awk '{print $5}'
. You still have two problems to solve, though: skipping the header and considering filesystems that have a space in their mount-point. You can skip the header fairly easily, since it's the first line of output: df -Pg | awk 'NR > 1 {print $5}'
, but dealing with the remaining fields takes more work.
I would propose that you use different options for the df
command to request colon-separated output with specific fields: df -c -F %z %m
. The -F
option automatically includes the filesystem device and block fields, so you'd want to skip those. We still want to skip the header, so we end up with:
df -c -F %z %m | sed 1d | cut -d: -f3,4
... to delete the first line, then output only the third and fourth colon-delimited fields. That should give your monitoring program the relevant information. Depending on what actions you're taking, you may want to include field 1 (the device) if you need to check the free volume group space. Be aware that the df
output adds a space to the beginning of the mount-point field (which may be a side effect of requesting colon-separated output and its parsing of the -F argument). Be aware also that you may see network-mounted filesystems; if you only want to see local filesystems, add the -T local
option:
df -T local -c -F %z %m | sed 1d | cut -d: -f3,4