Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude list of files from find

Tags:

linux

find

shell

If I have a list of filenames in a text file that I want to exclude when I run find, how can I do that? For example, I want to do something like:

find /dir -name "*.gz" -exclude_from skip_files 

and get all the .gz files in /dir except for the files listed in skip_files. But find has no -exclude_from flag. How can I skip all the files in skip_files?

like image 598
Thomas Johnson Avatar asked Mar 21 '14 12:03

Thomas Johnson


People also ask

How do you exclude from find?

We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!

How do I exclude a file in Linux?

To do so, create a text file with the name of the files and directories you want to exclude. Then, pass the name of the file to the --exlude-from option.

How do I find and exclude a directory?

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 .


2 Answers

I don't think find has an option like this, you could build a command using printf and your exclude list:

find /dir -name "*.gz" $(printf "! -name %s " $(cat skip_files)) 

Which is the same as doing:

find /dir -name "*.gz" ! -name first_skip ! -name second_skip .... etc 

Alternatively you can pipe from find into grep:

find /dir -name "*.gz" | grep -vFf skip_files 
like image 103
Josh Jolly Avatar answered Sep 17 '22 21:09

Josh Jolly


This is what i usually do to remove some files from the result (In this case i looked for all text files but wasn't interested in a bunch of valgrind memcheck reports we have here and there):

find . -type f -name '*.txt' ! -name '*mem*.txt' 

It seems to be working.

like image 31
Martin G Avatar answered Sep 21 '22 21:09

Martin G