Use Df Command to Show Only the %Used

use df command to show only the %used

Shorter using python

statvfs will provide information on free or available blocks on a partition without need to call subprocess:

>>> import os
>>> st = os.statvfs('/')
>>> (st.f_blocks - st.f_bavail) / st.f_blocks
0.692345901307951

Shorter using df and awk

$ df -Ph /oracle/archive | awk 'NR == 2{print $5+0}'
69

Awk will treat $5 as a string. By adding zero to it, we force awk to convert it to a number and that eliminates the %.

Shorter using df and GNU grep

df -Ph /oracle/archive | grep -Po '\d+(?=%)'
69

-o tells grep to print only the matching string. \d+(?=%) looks for digits followed by % but does not include % in the match.

This requires the -P option to grep which requires GNU grep.

Given the directory name, how to find the Filesystem on which it resides in C?

df `pwd`

...Super simple, works, and also tells you how much space is there...

[stackuser@rhel62 ~]$ pwd
/home/stackuser
[stackuser@rhel62 ~]$ df `pwd`
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda7 250056240 196130640 41223408 83% /
[stackuser@rhel62 ~]$ cd isos
[stackuser@rhel62 isos]$ pwd
/home/stackuser/isos
[stackuser@rhel62 isos]$ df `pwd`
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 103216920 90417960 11750704 89% /mnt/sda5
[stackuser@rhel62 isos]$ df $(pwd)
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 103216920 90417960 11750704 89% /mnt/sda5

...which is the likely cause of the mount point query in the first place.

Note those are backticks, and the alternate (modern) method, providing further control over slashes and expansion is df $(pwd). Tested and traverses symlinks correctly on bash, dash, busybox, zsh. Note that tcsh won't like the $(...), so stick to the older backtick style in csh-variants.

There are also extra switches in pwd and df for further enjoyment.

Portable way of finding total disk size in Java (pre java 6)

Update:

I apologise for misreading the question, I recommend copying the FileSystemUtils approach, but modifying the commands it runs slightly.

In dos you can get the free and total bytes with the fsutil command:

fsutil volume diskfree [drive letter]

on my box this gives the following results:

Total # of free bytes        : 41707524096
Total # of bytes : 80023715840
Total # of avail free bytes : 41707524096

On Unix, the command is still "df -k", you're just interested in the "1024-blocks" column to the left of "Free" (example from Wikipedia below). You obviously need to multiply the result by 1024.

Filesystem    1024-blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4 32768 16016 52% 2271 14% /
/dev/hd2 4587520 1889420 59% 37791 4% /usr
/dev/hd9var 65536 12032 82% 518 4% /var
/dev/hd3 819200 637832 23% 1829 1% /tmp
/dev/hd1 524288 395848 25% 421 1% /home
/proc - - - - - /proc
/dev/hd10opt 65536 26004 61% 654 4% /opt

Assuming you copy FileSystemUtils to implement "totalSpaceKB()" to delegate to an equivalent OS-specific method. The implementation for Windows would be something like this (note the use of "Find" to trim the output from fsutil to just get the total size):

long totalSpaceWindows(String path) throws IOException {
path = FilenameUtils.normalize(path);
if (path.length() > 2 && path.charAt(1) == ':') {
path = path.substring(0, 2); // seems to make it work
}

// build and run the 'fsutil' command
String[] cmdAttribs = new String[] {
"cmd.exe",
"/C",
"fsutil volume diskfree " + path
+ " | Find \"Total # of bytes\"" };

// read in the output of the command to an ArrayList
List lines = performCommand(cmdAttribs, Integer.MAX_VALUE);

//because we used Find, we expect the first line to be "Total # of bytes",
//you might want to add some checking to be sure
if (lines.size() > 0) {
String line = (String) lines.get(0);
String bytes = line.split(":")[1].trim();
return Long.parseLong(bytes);
}
// all lines are blank
throw new IOException(
"Command line 'fsutil volume diskfree' did not return
+ "any info for path '" + path + "'");
}

The implementation for Unix would be the same as freeSpaceUnix(), but remove the two calls to tok.nextToken() at the end of the method

    /** comment these two lines so the token received is the total size */
tok.nextToken(); // Ignore 1K-blocks
tok.nextToken(); // Ignore Used
/** this will now be the total size */
String freeSpace = tok.nextToken();
return parseBytes(freeSpace, path);
}

The implementations for other platforms would be similar.

Hope this helps and apologies agian for misreading the problem.


Original answer (gets free bytes, not total).

Prior to Java 6 there isn't an elegant way to do this, (see the bug). Rather than rolling your own, I'd recommend using a library to do the platform-specific processing for you.

Apache commons-io has a FileSystemUtils type that provides a static method freeSpaceKb(). It works on Windows and and some Unix implementations (see quote from Javadoc below)

From the Javadoc:

public static long freeSpaceKb(String path)
throws IOException

Returns the free space on a drive or volume in kilobytes by invoking the command line.
FileSystemUtils.freeSpaceKb("C:"); // Windows
FileSystemUtils.freeSpaceKb("/volume"); // *nix

The free space is calculated via the command line. It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.
In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class. If your operating system isn't supported, please raise a JIRA call detailing the exact result from df -k and as much other detail as possible, thanks.

Shell script to delete files when disk is full

Just another proposal (comments within code):

FILESYSTEM=/dev/sda1 # or whatever filesystem to monitor
CAPACITY=95 # delete if FS is over 95% of usage
CACHEDIR=/home/user/lotsa_cache_files/

# Proceed if filesystem capacity is over than the value of CAPACITY (using df POSIX syntax)
# using [ instead of [[ for better error handling.
if [ $(df -P $FILESYSTEM | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }') -gt $CAPACITY ]
then
# lets do some secure removal (if $CACHEDIR is empty or is not a directory find will exit
# with error which is quite safe for missruns.):
find "$CACHEDIR" --maxdepth 1 --type f -exec rm -f {} \;
# remove "maxdepth and type" if you want to do a recursive removal of files and dirs
find "$CACHEDIR" -exec rm -f {} \;
fi

Call the script from crontab to do scheduled cleanings

How to identify the storage space left in a persistent volume claim?

If there's a running pod with mounted PV from the PVC,

kubectl -n <namespace> exec <pod-name> -- df -ah

...will list all file systems, including the mounted volumes, and their free disk space.

Increasing the size of a filesystem in AIX - chfs and df commands

I believe you messed up with copy-paste. I will explain how it works and you apply this to your environment.

When you're using chfs -a size=+1 the command increases your LV by one block but the thing is that you can only increase LV by some amount of PPs. For example if you have PP size configured to 64Mb you'd have your LV automatically increased by 64*1024=65536 blocks.

So issue a command "lsvg ", check "PP size" and count.

ceph df max available miscalculation

MAX AVAIL column represents the amount of data that can be used before the first OSD becomes full. It takes into account the projected distribution of data across disks from the CRUSH map and uses the 'first OSD to fill up' as the target. it does not seem to be a bug. If MAX AVAIL is not what you expect it to be, look at the data distribution using ceph osd tree and make sure you have a uniform distribution.

You can also check some helpful posts here that explains some of the miscalculations:

  • Using available space in a Ceph pool
  • ceph-displayed-size-calculation
  • max-avail-in-ceph-df-command-is-incorrec

As you have Erasure Coding involved please check this SO post:

  • ceph-df-octopus-shows-used-is-7-times-higher-than-stored-in-erasure-coded-pool


Related Topics



Leave a reply



Submit