how to calculate file system utilization system i.e. free and used space in all drives. If any drive is exceeding a threshold limit it should print top 10 files or directory as per space usage. The limit can be like %used memory.please help me to write a bash script for Linux/AIX.
how to find file system utilization in Linux and if its exceeding a certain limit then some action?
linux;bash;aix
Checking the IBM Knowledge Center for differences in the commands needed, I think I've got it worked out. Be advised, there is no way for me to test this in AIX, so it's based on what I can learn of AIX's differences from the above site. In standard GNU/Linux tools, prior to coversion of some of the options, it tests as good. YMMV.To start with, we can get the list of file systems, and what percentage is used on each, along with the file system size (which we can't turn off), and the mount point, which we may need for later processing. Using df -F %z %m returns something like /dev/sda1 1545476 78% /home, meaning 78 percent of the 1545476 blocks on device /dev/sda1, mounted on /home are in use.Since we only need to know the file system and mount point, to check it out further, and the percent used, without the % sign, we modify the output using sed and awk. While doing that, we might as well put the uasge first and the name second, strip out the lines that don't represent real file systems, and get the mount point for each file system. This'll make it easier to process later. Lastly we can sort it in descending percentage used so that the most important ones are first. This gives us the command:df -F %z %m | grep -e '^/dev/[sh]d' | sed 's/%//g' | awk '{print $3 $1 \$4\}' | sort -rnFinal output for a line is like this 78 /dev/sda1 /home.Now we can use this as the input to a loop that reads each line and processes it as we want, based on a preset action_limit, here set at 75. Save it as a shell script, with execute permissions.#!/bin/bashaction_limit=75;df -F %z %m \    | grep -e '^/dev/[sh]d' \    | sed 's/%//g' \    | awk '{print $3 $1 $4}' \    | sort -r -n \    | while read -r pct dsk mnt; do        if test $action_limit -le $pct; then            echo $mnt for $dsk is using ${pct}%;        fi;    doneThe line with echo needs to be replaced with the command of what you want to do with those file systems, now that you know which ones qualify. The echo command shows where the other command needs to be called, and shows what information is available to pass to that script. Not everything needs to be used. Most likely only the mount point, $mnt, will be needed. Be advised that $mnt contains a directory path, which may contain spaces or other dangerous shell characters, and should be enclosed in quotes when used as a parameter to another script or command.To merely list the top files/directories by size on the quaifying file systems, we don't need to create a whole new script to call. We can simply add one command to the above, giving the modified shell script:#!/bin/bashaction_limit=75;top_count=10;df -F %z %m \    | grep -e '^/dev/[sh]d' \    | sed 's/%//g' \    | awk '{print $3 $1 $4}' \    | sort -r -n \    | while read -r pct dsk mnt; do        if test $action_limit -le $pct; then            echo $mnt for $dsk is using $pct%;            du -s -x -m $mnt/* | sort -rn | head -$top_count| awk '{SZ=$1;$1=;print \tSZ\t$0}';        fi;    doneThe output will be, in descending order, the file systems that meet or exceed the set limit, action_limit, followed by the files or directories using the most space on that filesystem. How many files or directories are listed is set by the new variable top_count. These are indented under their mount points to keep things neat and readable, and include the total size of each file or directory in megabytes. (The -m controlls that, and can be changed to -k for kilobytes, or -g for gigabytes. Megabytes seemed like a good compromise.)Lastly, you used the /bash tag, so all testing was done in bash, and if there are any bash-specific tests or syntax used, I didn't catch them. Therefore, if you have some other shell, test it carefully before trusting it.