1

I am using system command to find files on ubuntu, and trying to redirect the result on screen to a txt file, as following example.

# make file
system("touch a.txt")
system("touch b.txt")
system("touch c.txt")
system("touch d.txt")

sink("t.txt")

 c("a.txt", "b.txt")%>% lapply(function(f) {
  system(sprintf("find -name %s", f)) 
}) 
sink()

but the result turns out to be a list with O's inside. Please advise how I can achieve that. Thanks.

Grec001
  • 1,111
  • 6
  • 20
  • 1
    Does this answer your question? [How to save all console output to file in R?](https://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r) – Waldi Mar 16 '21 at 04:37
  • 1
    This [link](https://statisticsglobe.com/r-save-all-console-input-output-to-file) also seems relevant – Waldi Mar 16 '21 at 04:38
  • thanks. I'd checked this post earlier. unfortunately, I just got list with empty elements. – Grec001 Mar 16 '21 at 12:58

1 Answers1

2

You could use intern = TRUE option which returns system result directly into the R environment:

c("a.txt", "b.txt" , "test.txt") %>% lapply(function(f) {
  system(sprintf("find -name %s", f), intern = T)
}) 

[[1]]
[1] "./a.txt"

[[2]]
[1] "./b.txt"

[[3]]
character(0)


Waldi
  • 39,242
  • 6
  • 30
  • 78