罗列文件夹中的文件 | List the Files in a Directory/Folder


罗列文件夹中的文件 | List the Files in a Directory/Folder

关键函数

list.files

list.files(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

       dir(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

list.dirs

list.dirs(path = ".", full.names = TRUE, recursive = TRUE)

关键参数

# path  
相对路径或绝对路径
# pattern
Only file names which match the regular expression will be returned.
# all.files
布尔型,FALSE,相当于linux的lsTRUE,相当于linux的ls -a
# full.names    
布尔型,TRUE,返回相对路径+文件名,FALSE,返回文件名不返回路径
# recursive 
布尔型,是否递归遍历该path下所有文件夹
# ignore.case   
布尔型, pattern参数的匹配是否需要区分字母大小写
# include.dirs  
布尔型, recursive参数递归时,子目录名本身是否包含在返回结果中,默认不包含在返回列表中

no..    
logical. Should both "." and ".." be excluded also from non-recursive listings?

示例

测试路径tree

tree bmat
bmat
├── WBC
│   ├── PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
│   ├── PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
│   ├── PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
│   ├── PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
│   ├── PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
│   ├── PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
│   ├── PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
│   ├── PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
│   ├── PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
│   ├── PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
│   ├── PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
│   ├── PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
│   └── md5
└── cfDNA
    ├── PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
    ├── PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
    ├── PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
    ├── PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz
    ├── PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz
    ├── PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz
    └── md5

list.file

all.files

list.files(path = "./bmat", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
[1] "cfDNA" "WBC"  
list.files(path = "./bmat", pattern = NULL, all.files = TRUE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
[1] "."                  ".."                 ".DS_Store"          ".ipynb_checkpoints" "cfDNA"              "WBC"

full.names

list.files(path = "./bmat", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
[1] "cfDNA" "WBC"  
list.files(path = "./bmat", pattern = NULL, all.files = FALSE,
           full.names = TRUE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
[1] "./bmat/cfDNA" "./bmat/WBC"

recursive

list.files(path = "./bmat", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
[1] "cfDNA" "WBC"  
list.files(path = "./bmat", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = TRUE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
 [1] "cfDNA/md5"                                                                                           
 [2] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [3] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [4] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [5] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [6] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [7] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [8] "WBC/md5"                                                                                             
 [9] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[10] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[11] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[12] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[13] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[14] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[15] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[16] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[17] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[18] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[19] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[20] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"

ignore.case

> list.files(path = "./bmat", pattern = "cf")
[1] "cfDNA"
> list.files(path = "./bmat", pattern = "CF", ignore.case = TRUE)
[1] "cfDNA"
> list.files(path = "./bmat", pattern = "CF", ignore.case = FALSE)
character(0)

include.dirs

> list.files("./bmat", recursive = TRUE, include.dirs = FALSE)
 [1] "cfDNA/md5"                                                                                           
 [2] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [3] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [4] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [5] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [6] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [7] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [8] "WBC/md5"                                                                                             
 [9] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[10] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[11] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[12] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[13] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[14] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[15] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[16] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[17] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[18] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[19] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[20] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"   


> list.files("./bmat", recursive = TRUE, include.dirs = TRUE)
 [1] "cfDNA"                                                                                               
 [2] "cfDNA/md5"                                                                                           
 [3] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [4] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [5] "cfDNA/PTAYM11189_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [6] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"          
 [7] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz" 
 [8] "cfDNA/PTAYM12148_plasma_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"
 [9] "WBC"                                                                                                 
[10] "WBC/md5"                                                                                             
[11] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[12] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[13] "WBC/PTAYH11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[14] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[15] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[16] "WBC/PTAYH12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[17] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[18] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[19] "WBC/PTAYM11189W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"        
[20] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.bmat.gz"                  
[21] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.hbfilter.bmat.gz"         
[22] "WBC/PTAYM12148W_partall_Aligned.out.fix_RG.sortp.mapped_F1804_MAPQ20.rmdup.zhnfilter.bmat.gz"