find files not in a list

不羁岁月 提交于 2019-12-20 11:35:39

问题


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

Example:

file.lst:

a
b
c

Execute:

find -mtime +7 -print > found.lst

found.lst:

a
d
e

so what I expect is:

d
e

回答1:


Pipe your find command through 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.



回答2:


Pipe the find-command to grep using the -v and -f switches

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

grep options:

-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


来源:https://stackoverflow.com/questions/7306971/find-files-not-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!