计算R中目录的大小

时间:2016-09-28 16:39:28

标签: r directory size

我想计算R中目录的大小。我试图使用list.info函数,不幸的是跟随符号链接,所以我的结果有偏见:

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

如何计算目录的文件大小,以便它给出与Linux相同的结果,例如:以du -s为例?

由于

4 个答案:

答案 0 :(得分:4)

我终于使用了这个:

system('du -s')

答案 1 :(得分:3)

#presets for varibles 
nCount = 0
total = 0
average = 0.0
Numbers = []
ValidInt = False

#start of string
nCount = (input("How many numbers? "))
print(nCount)
while not ValidInt:
    try:
        int(nCount)
        ValidInt = True
    except:
        nCount = input("Please Enter An Interger Number")
#validation loops untill an interger is entered
for x in range (int(nCount)):
    Numbers.append(input("Please Enter The Next Number"))

参考文献:

  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
  5. 如果你拥有它,你也可以利用cygwin;这使您可以使用Linux命令并获得可比较的结果。此外,在我上面给出的最后一个链接中使用Sysinternals是一个不错的解决方案。

答案 2 :(得分:2)

“ file.size”返回实际大小,磁盘上的大小是磁盘上实际占用的空间量。 检查一下以了解区别。 https://superuser.com/questions/66825/what-is-the-difference-between-size-and-size-on-disk 试试看所有文件的大小:

 files<-list.files(path_of_directory,full.names = T)
 vect_size <- sapply(files, file.size)
 size_files <- sum(vect_size)

答案 3 :(得分:0)

健康的解决方案,可能对检查包裹大小非常有用。

dir_size <- function(path, recursive = TRUE) {
  stopifnot(is.character(path))
  files <- list.files(path, full.names = T, recursive = recursive)
  vect_size <- sapply(files, function(x) file.size(x))
  size_files <- sum(vect_size)
  size_files
}

cat(dir_size(find.package("Rcpp"))/10**6, "Mb")
#> 14.81649 Mb

reprex package (v2.0.0) 于 2021 年 6 月 26 日创建