How do I calculate free disk space on Linux in JavaScript?

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

Answers:

Answer

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.

Answer

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.

Answer

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.

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.