bash 查找不在列表中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7306971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:43:08  来源:igfitidea点击:

find files not in a list

linuxbashcommand-linefinddiff

提问by rurouni

I have a list of files in file.lst. Now I want to find all files in a directory dirwhich are older than 7 days, except those in the file.lstfile. How can I either modify the find command or remove all entries in file.lstfrom the result?

我有一个file.lst. 现在,我想在目录中查找dir超过 7 天的所有文件,file.lst文件中的文件除外。如何修改 find 命令或file.lst从结果中删除所有条目?

Example:

例子:

file.lst:

file.lst

a
b
c

Execute:

执行:

find -mtime +7 -print > found.lst

found.lst:

found.lst

a
d
e

so what I expect is:

所以我期望的是:

d
e

回答by dogbane

Pipe your findcommand through grep -Fxvf:

find通过管道传输您的命令grep -Fxvf

find -mtime +7 -print | grep -Fxvf file.lst

What the flags mean:

标志的含义:

-F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.    
-x, --line-regexp
              Select only those matches that exactly match the whole line.
-v, --invert-match
              Invert the sense of matching, to select non-matching lines.
-f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.

回答by Fredrik Pihl

Pipe the find-command to grepusing the -vand -fswitches

通过管道将 find-command 用于grep使用-v-f开关

find -mtime +7 -print | grep -vf file.lst > found.lst

grep options:

grep选项:

-v : invert the match
-f file: - obtains patterns from FILE, one per line

example:

例子:

$ ls
a  b  c  d  file.lst

$ cat file.lst 
a$
b$
c$


$ find . | grep -vf file.lst 
.
./file.lst
./d