Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list.files - exclude folder

Tags:

regex

r

I want to use R's list.files function to find all text files in a folder and in its subfolders. However, I would like to exclude all files that are in one subfolder, let's say it contains unfinished work which is not ready for the things I use the other files for. Structure is like this:

- folder
 |- subfolder_1_good_stuff
 |- subfolder_2_good_stuff
 |- subfolder_3_good_stuff
 |- subfolder_4_unfinished_stuff

So "folder" would be my working directory.

I would now use:

list.files(path=".", pattern=".txt", recursive=TRUE)

But what should I add to "path" expression in order to exclude folder with unfinished stuff. This folder name will not be present in any filenames, if that makes some difference.

like image 480
nikopartanen Avatar asked Aug 22 '14 16:08

nikopartanen


People also ask

How do I exclude files from a folder?

Under Virus & threat protection settings, select Manage settings, and then under Exclusions, select Add or remove exclusions. Select Add an exclusion, and then select from files, folders, file types, or process. A folder exclusion will apply to all subfolders within the folder as well.

How do you exclude a directory in ls?

Adding -ls or -exec ls -l {} \; would make it like ls -l without directories.

How do I exclude a directory in Find command?

To exclude multiple directories, OR them between parentheses. And, to exclude directories with a specific name at any level, use the -name primary instead of -path .

How do you list only files not directories in Linux?

List Files Using “Grep” Command: First, we will use the grep command within the “ls” list command to list all the files residing in the particular folder. Try the below “ls” command along with the “-la” flag to list all the regular files, e.g., hidden or not.


2 Answers

Building upon @zx8754's answer, just with tidyverse approach using %>%:

library(tidyverse)

list.files(path=".", pattern=".txt", full.names = TRUE, recursive=TRUE) %>%
   stringr::str_subset(., "unfinished_stuff", negate = TRUE)
like image 160
Emman Avatar answered Sep 26 '22 12:09

Emman


Use regex - grepl to exclude:

# find all ".txt" files
myfiles <- list.files(path = ".", pattern = ".txt",
                      full.names = TRUE, recursive = TRUE)

# exclude unfinished stuff
myfilesfinished <- myfiles[ !grepl("unfinished_stuff", myfiles) ]
like image 27
zx8754 Avatar answered Sep 26 '22 12:09

zx8754