Home > database >  How to get distant server free space
How to get distant server free space

Time:01-05

Hi I am currently trying to automate file transfer to a distant server using bash commands but I want to be able to know how much space is left on that server before I attempt to send multiple files.

For instance if the server has 5GB left I want to send 4.5GB. This is the code I came up with (form multiple other type of questions) but I cant use this data to automate the process

spaceLeft=`ssh -i /cygdrive/C/cygwin64/home/turco/IDs/fileTransfer -p 2222 root@localhost " df -h /var/mobile"`
echo $spaceLeft
IFS=' ' read -r -a array <<< "$spaceLeft"
for index in "${!array[@]}"
do
    echo "$index ${array[index]}"
done

when I echo spaceleft this appears

Filesystem     Size Used Avail Capacity iused  ifree     %iused Mounted on 
/dev/disk0s1s2 60Gi 45Gi 6.9Gi 87%      318900 624035540 0%     /private/var

when I echo all items in the array this appears

0 Filesystem
1 Size
2 Used
3 Avail
4 Capacity
5 iused
6 ifree
7 %iused
8 Mounted
9 on

All I want to know is the 6.9Gi space left but I cant find a way to get it

CodePudding user response:

I suggest with GNU df:

df -hP /var/mobile | awk 'NR==2{print $4}'

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

CodePudding user response:

Since you only want what's left on one drive you could tail -1 to skip the heading and then print the fourth field.

I also suggest that you set the blocksize to the smallest unit you want to handle and not use the -h (human readable) format.

gigsleft=$(ssh ... df -B 1G /var/mobile | tail -1 | awk '{ print $4 }')

gigsleft should now contain the number of gigs left, without a unit - so you don't need to parse it. If you want the space left in megs, use -B 1M instead.

  •  Tags:  
  • Related