# Is there a built-in function to get the size of a folder?

**URL:** <https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762>\
**Category:** General Usage\
**Tags:** filesystem\
**Created:** [February 19, 2022, 4:09pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762 "2022-02-19T16:09:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 19, 2022, 4:09pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/1 "2022-02-19T16:09:42Z")

</div>

I saw that there are `stat` and `filesize`, but I don’t see a recursive option.

Is there a small package or another function that can give me the cumulative size of all files in a folder?

Another thing I am not sure about is if `stat` works on windows?

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 19, 2022, 5:10pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/2 "2022-02-19T17:10:22Z")

</div>

I tried the code below and I think it works now.

```julia
using Logging
function totalsize(dirpath)
	total = 0
	for (root,dir,files) in walkdir(dirpath)
		total += filesize(root)
		@info filesize(root) root
		for f in files
			file = joinpath(root,f)
			size = filesize(file)
			total += size
			@info size file
		end
		print(" "^80,"\r")
		print(total," B","\r")
	end
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 19, 2022, 5:28pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/3 "2022-02-19T17:28:02Z")

</div>

```julia
julia> 0.668766975402832 * 1024^2
701253.0

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 19, 2022, 5:28pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/4 "2022-02-19T17:28:51Z")

</div>

Yeah that was a stupid mistake. Thanks

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [February 19, 2022, 5:57pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/5 "2022-02-19T17:57:18Z")

</div>

Here is a version that is less noisy.  
Is it bad practice to add methods to `Base`?

```julia
function Base.filesize(dirpath::String, recursive::Bool)
	if !recursive
		return filesize(dirpath)
	else
		total = 0
		for (root,dirs,files) in walkdir(dirpath)
			total += filesize(root)
			for f in files
				file = joinpath(root,f)
				size = filesize(file)
				total += size
			end
		end
		return total
	end
end

```

Example:

```julia
julia> map((x)->basename(x)=>filesize(x,true),
           readdir(joinpath(homedir(), ".julia"), join=true))
14-element Vector{Pair{String, Int64}}:
       "artifacts" => 2647756716
          "clones" => 4096
        "compiled" => 641285002
           "conda" => 9595941797
             "dev" => 3157598
    "environments" => 270825
            "logs" => 711615
    "makiegallery" => 8444804
           "noise" => 4434
        "packages" => 81462643
 "pluto_notebooks" => 17462
           "prefs" => 4113
      "registries" => 283915844
   "scratchspaces" => 37441

```

---

<div class="post-metadata">

**Author:** ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)\
**Post date:** [October 27, 2022, 4:00pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/6 "2022-10-27T16:00:14Z")

</div>

But what if there are many levels of folders in each folder?

---

<div class="post-metadata">

**Author:** ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)\
**Post date:** [October 27, 2022, 4:30pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/7 "2022-10-27T16:30:10Z")

</div>

In this case, this method definition is a type piracy.  
According to official documentation:

> “Type piracy” refers to the practice of extending or redefining methods in Base or other packages on types that you have not defined.

link: [Style Guide · The Julia Language](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy)

And yes, type piracy is considered bad practice.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [October 27, 2022, 5:28pm UTC](https://discourse.julialang.org/t/is-there-a-built-in-function-to-get-the-size-of-a-folder/76762/8 "2022-10-27T17:28:02Z")

</div>

The code should work in that case.  
`walkdir` walks through the full substructure of the folder.
