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
You can specify the filter under "find" and the exclusion string under "grep -vwE". Use mtime under find if you need to filter on modified time too. This seems to show me all lines without the string, the OP asks for just the file names.
You can do it with grep alone (without find). grep -riL "somestring" . -L, --files-without-match each file processed. -R, -r, --recursive Recursively search subdirectories listed.
Without a doubt, grep is the best command to search a file (or files) for a specific text. By default, it returns all the lines of a file that contain a certain string. This behavior can be changed with the -l option, which instructs grep to only return the file names that contain the specified text.
Open the command-line shell and write the 'ls” command to list only directories. The output will show only the directories but not the files. To show the list of all files and folders in a Linux system, try the “ls” command along with the flag '-a” as shown below.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With