Search code examples
rdirectorysize

Compute the size of directory in R


I want to compute the size of a directory in R. I tried to use the list.info function, by unfortunably that follows the symbolic links so my results are biased:

# return wrong size, with duplicate counts for symlinks
sum(file.info(list.files(path = '/my/directory/', recursive = T, full.names = T))$size)

How do I compute the file size of a directory, so that it gives me the same result as on Linux, e.g. with du -s for example?

Thanks


Solution

  • system('powershell -noprofile -command "ls -r|measure -s Length"')
    

    References:

    1. https://technet.microsoft.com/en-us/library/ff730945.aspx
    2. Get Folder Size from Windows Command Line
    3. https://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html
    4. https://superuser.com/questions/217773/how-can-i-check-the-actual-size-used-in-an-ntfs-directory-with-many-hardlinks

    You can also leverage cygwin if you have it; this lets you use Linux commands and get comparable results. Further there's a nice solution using Sysinternals in the last link I gave above.