im working with PHP on my Raspberry PI, and im trying to find a file on a USB drive, this will be in a cron job for every minute...
The file has a name that i will know every time (settings.txt) and i have the required output to figure out what drives i have.. Now i cant figure out how to sort them...
Im writing them all to a text file but they are not spaced with on every line and not with \t between them... how is it possible to get the value of "Mounted On" ?
Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 3648564 3479396 152784 96% /
devtmpfs 469756 0 469756 0% /dev
tmpfs 474060 452 473608 1% /dev/shm
tmpfs 474060 6928 467132 2% /run
tmpfs 5120 4 5116 1% /run/lock
tmpfs 474060 0 474060 0% /sys/fs/cgroup
/dev/mmcblk0p1 65480 20256 45224 31% /boot
tmpfs 94812 48 94764 1% /run/user/1000
/dev/sda1 15625744 5330752 10294992 35% /media/kristian/D81C-745F
Device info: OS: Ubuntu Mate 15.** PHP: 5.6
I'd use awk for this. It splits by space by default. For my Mac it was $9 and for one of my Centos boxes it's $6. Just depends on which column your value is. Looks like it's a $6.
df | awk '{print $6}'
Mounted
/
/tmp
/usr/local
/var
/dev
/Volumes/MobileBackups
/Volumes/AppCode
/Volumes/LaCie
I figured i could use this code after searching for hours:
<?php
$df = array();
echo "reading `df` command
";
exec("df -T -x tmpfs -x devtmpfs -P -B 1G",$df);
array_shift($df);
$Stats = array();
foreach($df as $disks){
$split = preg_split('/\s+/', $disks);
$Stats[] = array(
'disk' => $split[0],
'mount' => $split[6],
'type' => $split[1],
'mb_total' => $split[2],
'mb_used' => $split[3],
'mb_free' => $split[4],
'percent' => $split[5],
);
}
echo "Completed device scan...
";