2011-09-05 34 views
13

Mam listę plików w file.lst. Teraz chcę znaleźć wszystkie pliki w katalogu dir, które są starsze niż 7 dni, z wyjątkiem tych w pliku file.lst. Jak mogę zmodyfikować polecenie find lub usunąć wszystkie wpisy z file.lst z wyniku?znajdź pliki spoza listy

Przykład:

file.lst:

a 
b 
c 

Execute:

find -mtime +7 -print > found.lst 

found.lst:

a 
d 
e 

więc czego oczekuję to:

d 
e 

Odpowiedz

20

Rura swój find poleceń poprzez grep -Fxvf:

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

Co flagi na myśli:

-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. 
+0

Czy to zadziała? dane wyjściowe z 'find' dla np. plik 'a' w cwd to'./a', który spowoduje, że '-Fx' nie będzie pasował ... –

+0

Co jeśli potrzebuję dynamicznie tworzyć listę plików, np. z 'ssh xyz.com 'ls/var/backups/daily | ogon -10''? Zamiast listy plików w pliku file.lst zamiast tego będzie istnieć. Próbowałem wypróbować tutaj, ale nie mogę tego rozgryźć (mój piping-fu jest słaby). – Jacob

3

rur znalezisko-komenda grep pomocą -v i -f przełączniki

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

opcje grep:

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

przykład:

$ ls 
a b c d file.lst 

$ cat file.lst 
a$ 
b$ 
c$ 


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

myślę, że będzie pasował do większości cases.However, jeśli jest nazwa pliku jako „AA”, prawdopodobnie to zawiedzie – ajreal

+0

@ajreal, true, może użyć 'a $' w pliku.lst, aby zawęzić tę możliwość. –

+0

@Fredrik: powinieneś użyć stałego wzorca ciągu. 'grep -F' lub' fgrep' –

Powiązane problemy