How to find the highest disk space usage mount? - linux

I'm looking for a command where only the highest disk space usage mount will be shown. So The maximum %usage mount will be shown.
Running df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vx/dsk/appdg/boom-vol
1.0G 19M 943M 2% /opt/blah99
500G 349G 152G 70% /opt/blah/data
/dev/vx/dsk/isdg/boom-shared-vol
50G 1.6G 46G 4% /opt/blah99/product/shared
/dev/vx/dsk/isdg/boom-bc-vol
150G 64G 81G 45% /opt/blah99/product/a_01
/dev/vx/dsk/isdg/boom-bt-vol
150G 47G 98G 33% /opt/blah99/product/a_02
Output should be -
500G 349G 152G 70% /opt/blah/data

What you are looking for is:
df -h | grep -vw "^\/dev" | sort -k 5 -n | tail -n 2 | head -n 1
Output of df -h | grep -vw "^\/dev":
Filesystem Size Used Avail Use% Mounted on
1.0G 19M 943M 2% /opt/blah99
500G 349G 152G 70% /opt/blah/data
50G 1.6G 46G 4% /opt/blah99/product/shared
150G 64G 81G 45% /opt/blah99/product/a_01
150G 47G 98G 33% /opt/blah99/product/a_02
Sorting by column 5 in numeric order: df -h | grep -vw "^\/dev" | sort -k 5 -n:
50G 1.6G 46G 4% /opt/blah99/product/shared
1.0G 19M 943M 2% /opt/blah99
150G 47G 98G 33% /opt/blah99/product/a_02
150G 64G 81G 45% /opt/blah99/product/a_01
500G 349G 152G 70% /opt/blah/data
Filesystem Size Used Avail Use% Mounted on
Getting second row from the end: df -h | grep -vw "^\/dev" | sort -k 5 -n | tail -n 2 | head -n 1:
500G 349G 152G 70% /opt/blah/data

Related

keep the header of file before sorting in csh

I have the below file.rpt:
Data Manhattan Wire Wire
Delay Distance Length Diff[%]
#############################
0.558 728.282 738.89 1%
0.481 255.258 285.887 12%
0.552 652.888 677.985 4%
0.431 420.896 477.73 14%
0.506 580.288 633.018 9%
0.388 350.22 463.05 32%
0.541 622.088 672.761 8%
0.462 384.064 586.98 53%
0.565 574.016 594.434 4%
0.470 339.268 450.927 33%
0.566 664.304 672.092 1%
0.441 436.428 534.146 22%
I'm trying to sort one of the column and keep the 3 lines header without any change.
How can I do it without manipulate to other file?
I tried something similar to the below:
head -3 file.rpt ; sort -nrk4,4 file.rpt | -
I suppose this doesn't really count as a csh answer, but:
sh -c '{ sed 3q file.rpt; sed 1,3d file.rpt | sort -nrk4,4; }'
In csh, you can do:
( sed 3q file.rpt; sed 1,3d file.rpt | sort -nrk4,4; )
You can do this:
head -3 file.rpt ; tail -n +4 file.rpt | sort -nrk4,4
-n: output the last X lines
+4: output starting with line 4. Hence skip the first 3 lines
use | to send the output of tail as the input to sort.
Result:
Data Manhattan Wire Wire
Delay Distance Length Diff[%]
#############################
0.462 384.064 586.98 53%
0.470 339.268 450.927 33%
0.388 350.22 463.05 32%
0.441 436.428 534.146 22%
0.431 420.896 477.73 14%
0.481 255.258 285.887 12%
0.506 580.288 633.018 9%
0.541 622.088 672.761 8%
0.565 574.016 594.434 4%
0.552 652.888 677.985 4%
0.566 664.304 672.092 1%
0.558 728.282 738.89 1%

Why fdisk and lsblk show different partition size?

If we see the size of sdb2 is 39.5GB in fdisk whereas 1k in the lsblk output. What can be the reason for this?
Disk /dev/sdb: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x674589c1
Device Boot Start End Sectors Size Id Type
/dev/sdb1 * 2048 999423 997376 487M 83 Linux
/dev/sdb2 1001470 83884031 82882562 39.5G 5 Extended
/dev/sdb5 1001472 83884031 82882560 39.5G 8e Linux LVM
root#ubuntu1604:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 300G 0 disk
sdb 8:16 0 300G 0 disk
├─sdb1 8:17 0 487M 0 part /boot
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 39.5G 0 part
├─ubuntu1604--vg-root 252:0 0 35.5G 0 lvm /
└─ubuntu1604--vg-swap_1 252:1 0 4G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
root#ubuntu1604:~#
lsblk shows size in IEC units (GiB, MiB, etc). A gibibyte (GiB) is larger than a gigabyte (GB)
fdisk shows size in SI (GB, MB, etc).

Add timestamp for each line on the df output command?

I would like to add the date for each line of the df output.
I tried:
df -m | awk '{print `date +%Y-%m`";"$1";"$2";"$3 }'
... but it doesn't work.
How can I add the date?
Here is an alternative:
df -m | awk '{print strftime("%Y-%m"), $0}'
And here is the output from the command above:
$ df -m | awk '{print strftime("%Y-%m"), $0}'
2019-10 Filesystem 1M-blocks Used Available Use% Mounted on
2019-10 devtmpfs 9852 0 9852 0% /dev
2019-10 tmpfs 9871 132 9740 2% /dev/shm
2019-10 tmpfs 9871 2 9869 1% /run
2019-10 /dev/mapper/fedora_canvas-root 50141 14731 32834 31% /
2019-10 tmpfs 9871 1 9871 1% /tmp
2019-10 /dev/sda5 976 243 667 27% /boot
2019-10 /dev/mapper/fedora_canvas-home 1277155 217435 994777 18% /home
2019-10 tmpfs 1975 63 1912 4% /run/user/1000
$
And here is an alternative version, printing just the 3 columns you wanted on the OP:
df -m | awk '{print strftime("%Y-%m"), $1, $2, $3}' | column -t
And the corresponding output:
$ df -m | awk '{print strftime("%Y-%m"), $1, $2, $3}' | column -t
2019-10 Filesystem 1M-blocks Used
2019-10 devtmpfs 9852 0
2019-10 tmpfs 9871 132
2019-10 tmpfs 9871 2
2019-10 /dev/mapper/fedora_canvas-root 50141 14731
2019-10 tmpfs 9871 1
2019-10 /dev/sda5 976 243
2019-10 /dev/mapper/fedora_canvas-home 1277155 217435
2019-10 tmpfs 1975 63
$
You may use this way:
df -m | awk -v dt=$(date "+%Y-%m") '{print dt "::", $0}'
We use -v dt=$(date "+%Y-%m") to execute date command in shell and pass it to awk in an argument dt.
If you want only first 3 columns from df command output then use:
df -m | awk -v dt=$(date "+%Y-%m") '{print dt, $1, $2, $3}'
A Perl solution.
df -m | perl -MPOSIX=strftime -alpe '$_ = strftime("%Y-%M ", localtime) . "#F[0..2]"'
Command line options:
-M : Load thestrftime() function from the POSIX module
-a : Autosplit the input records on whitespace into #F
-l : Remove newlines from input and add them to output
-p : Put each input record into $_, execute code and then print $_
-e : Run this code for each input record
The code updates $_ by concatenating the date (strftime("%Y-%M ", localtime)) with the first three columns (#F[0 .. 2]) of the input record.

How do I get grep to search for any number I put in the variable?

So at the beginning of my script, I am defining "threshold," which has a number that isn't going to exist in the next part, (2) and a number that will exist in the next part (6). I'm having the result of running df -h to a file called dffile. My question, is how do I get grep in line 7 to search all of the variable "threshold" for the number that will exist in the file? It works if I have the 6 before the 2 in the variable, so it seems as if it's only searching the first number in it. Thanks!
#!/bin/bash
threshold=("2%" "6%")
df -h > dffile
grep $threshold dffile >> thresh
cat thresh | awk '{print $6}' >> finding1
LINES=()
while IFS= read -r finding1
do
find $finding1 -xdev -size +40M -exec ls -lah {} \; | head -n 10
done < "finding1"
The output of df -h on my test server is:
root#tstd0001:~/scripts# df -h
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 0% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
As you can see above, "2" from my variable, does not exist, whereas "6" does. My goal is to make grep find any number that matches a number inside the variable.
Let's consider that cat output_df is the output of your df -h command:
$ cat output_df
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 2% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 6% /dev/shm
tmpfs 5.0M 0 5.0M 2% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
Then you change the first part of your script in the following way:
thresold="2%|6%"
cat output_df | awk -v VAR=$thresold '{if($5~VAR)print $6}'
of course you will have to replace cat output_df by df -h in the final script.
This will give the output:
/dev
/
/dev/shm
/run/lock
Explanations:
thresold="2%|6%" is a regex that matches 2% or 6% you can generalize it to "2%|6%|X%|Y%|...|Z%"
You pass it as a variable to awk via -v VAR=$thresold
Then you command awk to print the 6th field when the 5th field does match the regex that you have passed to it via '{if($5~VAR)print $6}'
Then you can regroup everything without using intermediate files:
thresold="2%|6%"
for f in `df -h | awk -v VAR=$thresold '{if($5~VAR)print $6}'`
do
find $f -xdev -size +40M -exec ls -lah {} \; 2>/dev/null | head -n10
done
On my box it gives the following output:
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/srcpkgcache.bin
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/pkgcache.bin
-rw-r--r-- 1 root root 62M 4月 28 02:27 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
-rw-r--r-- 1 root root 56M 2月 20 06:10 /usr/lib/libreoffice/program/libmergedlo.so
-rw-r--r-- 1 root root 73M 5月 22 19:31 /usr/lib/thunderbird/libxul.so
-rwxr-xr-x 1 root root 142M 5月 22 01:35 /usr/lib/chromium-browser/chromium-browser
-rw-r--r-- 1 root root 41M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.28.2
-rw-r--r-- 1 root root 54M 11月 17 2017 /usr/lib/x86_64-linux-gnu/libLLVM-5.0.so.1
-rw-r--r-- 1 root root 99M 3月 18 2017 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
-rwxr-xr-x 1 root root 42M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitPluginProcess2
...
Notes: 2>/dev/null this redirection is to remove all the permission errors by redirecting stderr to /dev/null (muting the stderr)
You initialize threshold as an indexed array,
threshold=("2%" "6%")
You then call grep with:
grep $threshold dffile
Since threshold is an array, to dereference all values in the array, you use the form:
${threshold[#]} ## which can be quoted to preserve whitespace in elements
When you deference the array as a normal variable, e.g. $threshold you return only the 1st element, e.g.
echo $threshold ## output '2%'
So before going further, you need to determine what you want to pass to grep, if you want to search for either 2% or 6%, then Allan has a nice explanation above. You can also construct the grep expression using printf -v, e.g.
printf -v gexp "%s\|%s" ${threshold[#]}
or to properly limit to the first 2-elements,
printf -v gexp "%s\|%s" "${threshold[0]}" "${threshold[1]}"
and then call grep with
grep "$gexp" dffile >> thresh
Let me know if you have further questions.

How to omit heading in df -k command of SunOs

Input: df -k
Output:
Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c0t0d0s0 10332220 443748 9785150 5% /
/devices 0 0 0 0% /devices
ctfs 0 0 0 0% /system/contract
proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
swap 45475864 1688 45474176 1% /etc/svc/volatile
objfs 0 0 0 0% /system/object
sharefs 0 0 0 0% /etc/dfs/sharetab
/dev/dsk/c0t0d0s3 10332220 3513927 6714971 35% /usr
I want to omit the 1st line Filesystem kbytes used avail capacity Mounted on from the output.
I used df -k | tail -n+2 in linux to get exactly what i wanted, but in SunOs I get
zenvo% df -k | tail -n+2
usage: tail [+/-[n][lbc][f]] [file]
tail [+/-[n][l][r|f]] [file]
How can i achieve the Required output:
/dev/dsk/c0t0d0s0 10332220 443748 9785150 5% /
/devices 0 0 0 0% /devices
ctfs 0 0 0 0% /system/contract
proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
swap 45475864 1688 45474176 1% /etc/svc/volatile
objfs 0 0 0 0% /system/object
sharefs 0 0 0 0% /etc/dfs/sharetab
/dev/dsk/c0t0d0s3 10332220 3513927 6714971 35% /usr
Note: No. of rows might change
I know it's an old thread, but the shortest and the clearest of all:
df -k | sed 1d
I haven't used SunOS but using sed you should be able to delete the first line like this:
df -k | sed -e /Filesystem/d
edit: But you would have to be careful that the word Filesystem doesn't show up elsewhere in the output. A better solution would be:
df -k | sed -e /^Filesystem/d
If you want to omit the first line of any result, you can use tail:
<command> | tail -n +2
So in your case:
df -k | tail -n +2
https://man7.org/linux/man-pages/man1/tail.1.html
What about:
df -k | tail -$((`df -k | wc -l`-1))

Resources