Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
Yes... you can use the same approach, noting that dir gives this information in its output. – Oleksandr R. Dec 22 '12 at 1:26
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. – Szabolcs Dec 22 '12 at 1:31
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) – acl Dec 22 '12 at 1:40
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! – Oleksandr R. Dec 22 '12 at 1:44
@acl For your df call, I'd display all disks: TableForm[Transpose[{##2}], TableHeadings -> {#, Automatic}] & @@ ReadList["!df -l -k", Word, RecordLists -> True, WordSeparators -> {",", " "}] – Jens Dec 22 '12 at 5:45
up vote 7 down vote accepted

We can use JLink for this:


JavaBlock[JavaNew["", "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.

share|improve this answer

The package JVMTools ( has the functions FreeDiskSpace[] and TotalDiskSpace[] in its utility functions section, among many other functions.

Works on all systems.

share|improve this answer

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["", 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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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