I want calculate and display the free space of the home filesystem but there are 3-4 users, and all should be in javascript, how can we do ?
I know in linux shell , we can do :
df -h
But in javascript it's not
I want calculate and display the free space of the home filesystem but there are 3-4 users, and all should be in javascript, how can we do ?
I know in linux shell , we can do :
df -h
But in javascript it's not
Node diskusage will do this.
Copying their example code:
#!/usr/bin/env node
var disk = require('diskusage');
// get disk usage. Takes mount point as first parameter
disk.check('/', function(err, info) {
console.log(info.free);
console.log(info.total);
});
You'll need to install node (if you don't already have it) and fetch the node-diskfree package from npm of course.
Edit: switched to a cross platform package, that runs on all OSs and doesn't scrape command line tools.
What JavaScript environment are you using? NodeJS has a childprocess module that you can use to spawn a df command, see http://nodejs.org/api/child_process.html for more details.
I imagine that you're not attempting this in a browser based JavaScript sandbox.
shameless plug - https://www.npmjs.com/package/microstats
Can also be configured to alert the user when disk space crosses user defined threshold. works for linux, macOS and windows.
You can either use diskusage npm and child_process.exec('df / -h') to get this parameter. but diskusage npm is more reliable and easy to use. if you use cp.exec('...') you should process the returned string by yourself to retrieve the desired parameters.
You can do it by running the command df -h > df.txt which writes the df -h output to a file named df.txt. Then you could read the file and match a regex to it.
const { exec } = require('child_process');
const { readFileSync, unlinkSync } = require('fs')
var space;
exec('df -h > df.txt');
space = readFileSync('df.txt', (data, err) => {
if (err) {
throw err
}
}).toString().match(/[0-9]+\.[0-9]+?../)[0]
console.log(space)