Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find files not in a list

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
like image 720
rurouni Avatar asked Sep 05 '11 10:09

rurouni


People also ask

How do you find files that do not contain a string?

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.

How do I find all files not containing specific text on Linux?

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.

How do I find all files containing specific text?

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.

How do I search for only files in UNIX?

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.


2 Answers

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.
like image 96
dogbane Avatar answered Oct 16 '22 18:10

dogbane


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
like image 41
Fredrik Pihl Avatar answered Oct 16 '22 18:10

Fredrik Pihl