Find and exclude what is in file

Hello everyone,

I try to find folders older than 3 years and display them, but excluding some directories, the below code does NOT exclude listed directories:

find . -maxdepth 3 -mtime +1095 -type d -exec ls -l {} \; | grep -vFf oldExclude >> older

oldExclude

Folder1/
Folder2/
Folder6/

Howerver if I remove the exec command, it works like a sharm:

find . -maxdepth 3 -mtime +1095 -type d | grep -vFf oldExclude >> older

Thanks in advance

Try not to do a fixed-strings match in the grep command. You may need to adapt the contents structure of the "oldExclude" file to be regexes.

With this does Not work either:

find . -maxdepth 3 -mtime +1095 -type d -exec ls -l {} \; | grep -vFf "oldExclude" >> older

Without a clear example of the output you hope to get and a display showing the file hierarchy you're trying to process, it is impossible to guess at what output you want. But, if you just want a list of directories, the output from:

ls -l ./directory_name

clearly is not going to give you what you want and using grep to get rid of some lines containing the string directory_name/ still isn't going to come even close to what you said you want.

This might come closer, but without a clear specification of what you want with examples, I have no idea whether or not this will work for you:

#!/bin/ksh

# Get list of directories to prune...
findArgs=
while read -r dir
do	dir=${dir%/}
	if [ $findArgs = "" ]
	then	findArgs="-name $dir"
	else	findArgs="$findArgs -o -name $dir"
	fi
done < oldExclude

find . -maxdepth 3 -mtime +1095 -type d \( \( $findArgs \) -prune -o -print \)

This was written and tested using a Korn shell, but should work with any shell that performs the basic parameter expansions required for a POSIX-conforming shell (such as ash , bash , ksh , or zsh ).

1 Like

Well, I should add the

-exec ls -l

, I tried, it returns the same result as I would not expected:

find . -maxdepth 3 -mtime +1095 -type d -exec ls -l \; \( \( $findArgs \) -prune -o -print \)

I am trying the list of folders that are older than 3 years

NO! You should not add the -exec ls -l \; . Add that that primary to the find command causes every old directory to be listed instead of just printing the directories you said you wanted to print. And you said you just wanted to print the names of those directories; not the contents of those directories.

Furthermore, you said you wanted to exclude a bunch of directories from the output. Adding the -exec ls -l \; where you added it guarantees that all of the work we went to to exclude directories is ignored.

So, can I extract only the date / time of these ignored folders?

I'm completely lost.

You want to ignore a bunch of directories that are included in a list of directories that you want to exclude from processing, and you insist on adding code that instead of excluding them produces a long listing of the contents of the directories you want to exclude.

And now, in addition to printing a list of all of the files in the directories you want to exclude, you also want to extract some unspecified date and time from the directories you want to exclude? None of this makes any sense to me???

Please clearly explain what you are trying to do! And show us a sample of a file hierarchy you want to process and the output you hope to produce from processing that file hierarchy.