Skip to content

Instantly share code, notes, and snippets.

@btupper
Last active November 30, 2016 15:37
Show Gist options
  • Save btupper/73406990a704a45d448c223275cd9bf4 to your computer and use it in GitHub Desktop.
Save btupper/73406990a704a45d448c223275cd9bf4 to your computer and use it in GitHub Desktop.
A function to audit disk usage by subdirectory
#' A function to audit disk usage by subdirectory
#'
#' @param path the path to audit
#' @param extra character extra arguments for linux `du` command
#' @param app character the `du` executable path
#' @return data frame or NULL
#' \itemize{
#' \item{size numeric in megabytes}
#' \item{name character}
#' }
#' @examples
#' \dontrun{
#' du_subdirectories(path = "/opt/data")
#' # size name
#' # 1 926 /opt/data/bathy
#' # 2 275 /opt/data/cpcugb
#' # 3 332 /opt/data/jellycast
#' # 4 1 /opt/data/misc
#' # 5 18516 /opt/data/narr
#' # 6 37825 /opt/data/natmap
#' # 7 1132 /opt/data/natural_earth
#' # 8 1 /opt/data/ncep
#' # 9 1 /opt/data/salpcast
#' # 10 4193 /opt/data/sat
#' # 11 4526 /opt/data/tickcast
#' # 12 144 /opt/data/wx
#' }
du_subdirectories <- function(path = "/opt/data",
extra = "-ms",
app = Sys.which("du") ){
# btupper@ecocast ~ $ du -hs /opt/data/jellycast
# 332M /opt/data/jellycast
dd <- list.dirs(path[1],
recursive = FALSE, full.names = TRUE)
if (length(dd) <= 0) {
x <- data.frame(size = numeric(), name = character(),
stringsAsFactors = FALSE)
return(x)
}
CMD <- sprintf("%s %s %s", app, extra, dd)
xx <- strsplit(sapply(CMD, system, intern = TRUE), '\t')
x <- do.call(rbind, xx)
x <- data.frame(size = as.numeric(x[,1]),
name = x[,2], stringsAsFactors = FALSE)
rownames(x) <- NULL
x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment