Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

dir_ls

List files


Description

dir_ls() is equivalent to the ls command. It returns filenames as a named fs_path character vector. The names are equivalent to the values, which is useful for passing onto functions like purrr::map_dfr().

dir_info() is equivalent to ls -l and a shortcut for file_info(dir_ls()).

dir_map() applies a function fun() to each entry in the path and returns the result in a list.

dir_walk() calls fun for its side-effect and returns the input path.

Usage

dir_ls(
  path = ".",
  all = FALSE,
  recurse = FALSE,
  type = "any",
  glob = NULL,
  regexp = NULL,
  invert = FALSE,
  fail = TRUE,
  ...,
  recursive
)

dir_map(
  path = ".",
  fun,
  all = FALSE,
  recurse = FALSE,
  type = "any",
  fail = TRUE
)

dir_walk(
  path = ".",
  fun,
  all = FALSE,
  recurse = FALSE,
  type = "any",
  fail = TRUE
)

dir_info(
  path = ".",
  all = FALSE,
  recurse = FALSE,
  type = "any",
  regexp = NULL,
  glob = NULL,
  fail = TRUE,
  ...
)

Arguments

path

A character vector of one or more paths.

all

If TRUE hidden files are also returned.

recurse

If TRUE recurse fully, if a positive number the number of levels to recurse.

type

File type(s) to return, one or more of "any", "file", "directory", "symlink", "FIFO", "socket", "character_device" or "block_device".

glob

A wildcard aka globbing pattern (e.g. *.csv) passed on to grep() to filter paths.

regexp

A regular expression (e.g. [.]csv$) passed on to grep() to filter paths.

invert

If TRUE return files which do not match

fail

Should the call fail (the default) or warn if a file cannot be accessed.

...

Additional arguments passed to grep.

recursive

(Deprecated) If TRUE recurse fully.

fun

A function, taking one parameter, the current path entry.

Examples

dir_ls(R.home("share"), type = "directory")

# Create a shorter link
link_create(system.file(package = "base"), "base")

dir_ls("base", recurse = TRUE, glob = "*.R")

dir_map("base", identity)

dir_walk("base", str)

dir_info("base")

# Cleanup
link_delete("base")

fs

Cross-Platform File System Operations Based on 'libuv'

v1.5.0
GPL-3
Authors
Jim Hester [aut, cre], Hadley Wickham [aut], libuv project contributors [cph] (libuv library), Joyent, Inc. and other Node contributors [cph] (libuv library), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.