5

I am creating an Electron app that helps me manage my disk space. However, I would like it to work on Linux/UNIX too.

I wrote the following code, which works great for Windows, but not Linux/UNIX systems.

window.onload = function(){
  const cp = require('child_process')
  cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
    let drives = stdout.trim()split('\r\r\n')
      .map(value => value.trim().split(/\s{2,0}/))
      .slice(1)
  })
}

The output looks like this.

[
  ["560232439808",  "C:", "999526756352",  "System"  ]
  ["999369699328",  "D:", "999558213632",  "SSD"     ]
  ["1511570386944", "E:", "8001545039872", "Get"     ]
  ["4620751712256", "F:", "8001545039872", "BR"      ]
  ["788449492992",  "G:", "4000650883072", "Seen"    ]
  ["2296009408512", "H:", "4000768323584", "Seen 2"  ]
  ["3594248679424", "I:", "8001545039872", "2160"    ]
  ["3507750227968", "J:", "8001545039872", "1080"    ]
  ["945300619264",  "K:", "999625322496",  "Trailer" ]
]

Since I am unfamiliar with Linux/UNIX, I am wondering how I can achieve the same output for Linux/UNIX too?

oldboy
  • 5,729
  • 6
  • 38
  • 86
  • 1
    You should look at something like `drivelist` and `diskusage` packages if you want to get more cross-platform. The `wmic` command you have now is Windows-specific; there are similar versions in Linux and other UNIXes (and thus MacOS) (see `df` for example) but not all the fields you have have exact parallels (e.g. a drive could be mounted "inside" the path of another drive) – Joe Dec 31 '19 at 20:11
  • There is `df` on *NIX OSes but the nature of the filesystems means that the output is not at all equivalent to the above. You can have a tree on C:, D:, E:, etc. However with *NIX you'd have start at `/`, yet you can *mount* a different partition anywhere in that tree. For example, a common one is to have `/home` (equivalent to the Windows `C:\Users\` directory) on a different partition. So you might end up with sizes of, say, `20 GB` for `/` but `100 GB` for `/home` yet from FS navigation perspective they might as well be the same partition. – VLAZ Dec 31 '19 at 20:13
  • @Joe so is `drivelist` and `diskusage` only Windows specific too? or does `df` simply allow you to execute `wmic`-like commands on Linux and UNIX? – oldboy Dec 31 '19 at 21:11
  • @VLAZ thanks for the info. so what do you think the best approach is? – oldboy Dec 31 '19 at 21:12

1 Answers1

5

There probably isn't a command that works across all platforms.

But what you can do is get the current platform with process.platform and run a different command on each platform.

For example:

const cp = require('child_process');

if (process.platform == 'win32') { // Run wmic for Windows.
    cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
      let drives = stdout.trim()split('\r\r\n')
        .map(value => value.trim().split(/\s{2,0}/))
        .slice(1)
    });
} else if (process.platform == 'linux') { // Run df for Linux.
    cp.exec('df', (error, stdout)=>{
        // Do your magic here.
    });
} else {
    // Run something for a mac.
}

You can read about process.platform here.

Joshua
  • 5,032
  • 2
  • 29
  • 45
  • sweet thanks. hey i just realized something else. for some reason `wmic logicaldisk get size` returns the "total" size of the disk without accounting for reserved space. you know any way i can account for the reserved space on each disk on Windows?? – oldboy Jan 01 '20 at 00:27
  • If there is a command that could get the total reserved space then you could run that command separately, then factor the result into your calculation. I've taken a look and I couldn't find a command that could do this. If I were to find one though I couldn't be sure because I'm on Ubuntu not Windows. – Joshua Jan 01 '20 at 00:39
  • i have no clue. i havent used command line in a minute lol. i tried looking thru the help settings on `wmic logicaldisk get` and couldnt see anything related to reserved space :( ill have to keep looking i guess. appreciate your help!! – oldboy Jan 01 '20 at 00:41
  • found a way, although im not sure it will work with node ?? `diskpart` and then `list disk` seems to accurately display the total minus reserved space. – oldboy Jan 01 '20 at 01:02
  • It seems like that will work, my computer isn't Windows so I can't check if it'll work or not. The only way to find out is to try it :) – Joshua Jan 01 '20 at 01:26
  • im not sure how to do two consecutive asynchronous commands like that. the problem is that when i try `cp.exec('diskpart' ...)` it launches an actual instance of command prompt (the windows CL tool) :( – oldboy Jan 01 '20 at 01:28
  • 1
    I see, try using [`cp.spawn(`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) instead of `cp.exec(`. Also try running both commands at once like: `cp.exec('diskpart; list disk'`. – Joshua Jan 01 '20 at 01:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205176/discussion-between-joshua-and-oldboy). – Joshua Jan 01 '20 at 01:44
  • same issue unfortunately. it also launches an actual instance of command prompt – oldboy Jan 01 '20 at 03:33
  • figured out how to get the info. `exec('get-volume', {'shell': 'powershell.exe'} ... )`. beautiful. – oldboy Jan 01 '20 at 03:47
  • oh jesus i just realized when the size it displays from `get-volume` is different from the value of `size` from `get-volume | format-list driveletter, size`. absolutely retarded lol – oldboy Jan 01 '20 at 03:57
  • Great, so do you have everything you need then? – Joshua Jan 01 '20 at 04:28
  • for this question yes for the most part. thanks again!! [here is my new question](https://stackoverflow.com/q/59549998/7543162) if youre interested – oldboy Jan 01 '20 at 04:33