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时想要排除的文本文件中有文件名列表,我该怎么办?例如,我想做类似的事情:
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
?
并获取/ dir中的所有.gz文件,但skip_files中列出的文件除外。但是find没有-exclude_from标志。如何跳过skip_files中的所有文件?
4 个解决方案
#1
33
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
我不认为find有这样的选项,你可以使用printf和你的排除列表构建一个命令:
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到grep管道:
find /dir -name "*.gz" | grep -vFf skip_files
#2
7
I think you can try like
我想你可以试试
find /dir \( -name "*.gz" ! -name skip_file1 ! -name skip_file2 ...so on \)
#3
5
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):
这就是我通常从结果中删除一些文件的方法(在这种情况下,我查找了所有文本文件,但对我们在这里和那里的一堆valgrind memcheck报告不感兴趣):
find . -type f -name '*.txt' ! -name '*mem*.txt'
It seems to be working.
它似乎有效。
#4
2
find /var/www/test/ -type f \( -iname "*.*" ! -iname "*.php" ! -iname "*.jpg" ! -iname "*.png" \)
The above command gives list of all files excluding files with .php, .jpg ang .png extension. This command works for me in putty.
上面的命令给出了所有文件的列表,不包括扩展名为.php,.jpg ang .png的文件。这个命令适用于我的腻子。
#1
33
I don't think find
has an option like this, you could build a command using printf
and your exclude list:
我不认为find有这样的选项,你可以使用printf和你的排除列表构建一个命令:
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到grep管道:
find /dir -name "*.gz" | grep -vFf skip_files
#2
7
I think you can try like
我想你可以试试
find /dir \( -name "*.gz" ! -name skip_file1 ! -name skip_file2 ...so on \)
#3
5
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):
这就是我通常从结果中删除一些文件的方法(在这种情况下,我查找了所有文本文件,但对我们在这里和那里的一堆valgrind memcheck报告不感兴趣):
find . -type f -name '*.txt' ! -name '*mem*.txt'
It seems to be working.
它似乎有效。
#4
2
find /var/www/test/ -type f \( -iname "*.*" ! -iname "*.php" ! -iname "*.jpg" ! -iname "*.png" \)
The above command gives list of all files excluding files with .php, .jpg ang .png extension. This command works for me in putty.
上面的命令给出了所有文件的列表,不包括扩展名为.php,.jpg ang .png的文件。这个命令适用于我的腻子。