List the files and folders in the current working directory or an alternative location on your computer. By default, the current directory is shown, but a path may be specified to list files elsewhere.
  • path – A character string listing a directory. This may be from a root directory (starting with / on a Mac). Or it may be a relative path from the current directory. Use a double-dot (..) to look into a parent folder.

Example. In my build of R, the session is initialized in the root directory of my account, which I verify via getwd. I list the folders available in this directory using list.files, navigate to the January 2012 folder for code using setwd, and list the files in the new current working directory. Finally, I list the current files in the February 2012 folder by moving up a directory and specifying the 1202 folder.
> getwd()
[1] "/Users/ddiez"
> list.files()
 [1] "Desktop"   "Documents" "Downloads" "Dropbox"  
 [5] "KindleGen" "Library"   "Movies"    "Music"    
 [9] "NMMAPS"    "Pictures"  "Public"    "Sites"    
> setwd("Dropbox/RFunction/code/1201")
> getwd()
[1] "/Users/ddiez/Dropbox/RFunction/code/1201"
> list.files()
 [1] "120103.R"           "120104.R"          
 [3] "120106.R"           "120107.R"          
 [5] "120108.R"           "120109.R"          
 [7] "120110.R"           "120111.R"          
 [9] "120112.R"           "120113.R"          
[11] "120114.R"           "120115.R"          
[13] "120116.R"           "120117.R"          
[15] "120118.R"           "120119.R"          
[17] "120120.R"           "120121.R"          
[19] "120122.R"           "120123.R"          
[21] "120124.R"           "120125.R"          
[23] "120126.R"           "120127.R"          
[25] "120128.R"           "120129.R"          
[27] "120130.R"           "120131.R"          
[29] "example.png"        "rug.png"           
[31] "sample.bmp"         "sample.jpg"        
[33] "sample.pdf"         "sample.png"        
[35] "sample.tiff"        "smoothScatter.png" 
[37] "smoothScatter1.png" "smoothScatter2.png"
[39] "TRUEFALSE.png"     
> list.files('../1202')
[1] "120201.R"    "120202.R"    "normTail.Rd"

Leave a Reply