I know a workaround, where one calculates sizes of all files and then you subtract this from your default space, but is there any direct way to do it? Simplicity and speed are important.

There's the way to calculate free RAM with external call

ReadList["!typeperf \"\\Memory\\Available Bytes\" -sc 1", Word,
RecordLists -> True, WordSeparators -> {","}] // 
ToExpression@Part[#, 2, 2] &

Is there any way to do the same but for disk space?


ReadList["!dir c:\ ", Word, RecordLists -> True, 
WordSeparators -> {" ", ","}][[-1, 3 ;; -3]]] // ToExpression

gives the amount of free memory on disk C in bytes)

  • $\begingroup$ Yes... you can use the same approach, noting that dir gives this information in its output. $\endgroup$ Dec 22 '12 at 1:26
  • 1
    $\begingroup$ There's no built-in way in Mathematica for this so you will need to call an external command or system API function. .NETLink might be useful if you're on Windows (check e.g. this). I'm not on Windows now so I can't test a solution. $\endgroup$
    – Szabolcs
    Dec 22 '12 at 1:31
  • 1
    $\begingroup$ something along the lines of Select[ ReadList["!df", Word, RecordLists -> True, WordSeparators -> {",", " "}], First@# == "/dev/disk1" & ] should do the trick (on OSX and I guess linux) $\endgroup$
    – acl
    Dec 22 '12 at 1:40
  • 1
    $\begingroup$ The Windows performance counter that gives this information is \LogicalDisk(*)\Free Megabytes, so you can use typeperf to get the information if you like. However, this only works if your user account has the "profile system performance" privilege assigned. By default this is only granted to Administrators! $\endgroup$ Dec 22 '12 at 1:44
  • 1
    $\begingroup$ @acl For your df call, I'd display all disks: TableForm[Transpose[{##2}], TableHeadings -> {#, Automatic}] & @@ ReadList["!df -l -k", Word, RecordLists -> True, WordSeparators -> {",", " "}] $\endgroup$
    – Jens
    Dec 22 '12 at 5:45

We can use JLink for this:


JavaBlock[JavaNew["java.io.File", "C:\\"]@getFreeSpace[]]
(* 159352233984 *)

We can also find all file system roots and report the total, free and usable disk space:


  {#@getPath[], #@getTotalSpace[], #@getFreeSpace[], #@getUsableSpace[]}& /@
] // TableForm[#, TableHeadings->{None, {"Root","Total", "Free", "Usable"}}]&

table showing disk space

The zeroes here are dismounted drives on my system. The usable space is a concept that varies between operating systems and may take account of disk quotas, permissions, etc.


The package JVMTools (www.jvmtools.net) has the functions FreeDiskSpace[] and TotalDiskSpace[] in its utility functions section, among many other functions. www.jvmtools.net/utility.html

Works on all systems.


Here is a version that passes Directory[] to df so you know how much space there is in Mathematica's current working directory.

I have tried it on Linux but I see no reason it shouldn't work on OSX and other unixy systems that have df.

Options[freeSpace] = {"Path" :> Directory[], "Property" -> 4};
freeSpace[OptionsPattern[]] := 
 Module[{out = 
    ReadList["!df '" <> ToString[OptionValue["Path"]] <> "'"
     ,RecordLists -> True]
    ,i ,val},
  val = out[[-1, OptionValue["Property"]]];
    val, FromDigits::nlst
    ], FromDigits::nlst]

You can do similarly with @WReach's solution, which would be better since it will work on all systems.

JavaBlock[JavaNew["java.io.File", Directory[]]@getFreeSpace[]]

You can also check how much free space is on another disk, say you store your stuff elsewhere:

freeSpace[]                 (* 8898584 *)
freeSpace["Path"-> "/tmp"]  (* 20958736 *)

You can also extract other information that df gives:

freeSpace["Path" -> "/tmp", "Property" -> 1] (* "/dev/sda5" *)

The different properties are:

    1            2     3     4    5     6
Filesystem      Size  Used Avail Use% Mounted on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.