8

I want to do the equivalent of ls in R. Say I want ls /a/b/c/201*/*/d/e/f/*/sameNameFile.gz, this command take 5 seconds to run on a terminal, I tried to use list.files but it takes a path argument and need recursive=TRUE. It is taking ages...

Is there a function I could use (or an option to list.files) that would allow me to run this ls command (I know I can run the comamnd itself with system(cmd,intern=TRUE) but I want a R solution)

statquant
  • 13,672
  • 21
  • 91
  • 162
  • This is almost a duplicate of http://stackoverflow.com/q/21576944/134830 – Richie Cotton Feb 14 '14 at 13:56
  • 1
    If this is a command that you have to run regularly, you might want to consider copying the files you want to a common folder, or otherwise restructuring your filing system. – Richie Cotton Feb 14 '14 at 14:26

1 Answers1

9

The result from the related question

Fast test if directory is empty

was that on some systems, system("ls -f -R", intern = TRUE) is faster than list.files. Your performance may vary.

The -R switch means recursive; the -f switch means don't sort alphabetically, which is where the performance gain comes from.

Community
  • 1
  • 1
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • performance in my case is like 1 to 100 ... Also I am not using `-R` here I just specify a `regex` with the whole path... I should not have to use `recursive` in `list.files` – statquant Feb 14 '14 at 14:04
  • 4
    @statquant then don't use it. The gist of this answer is that `list.files` is slow, and something like `system(sprintf('ls %s', ls_args), intern = TRUE)` is just plain faster. – shadowtalker Oct 06 '14 at 03:26