Find and exclude what is in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and exclude what is in file
# 1  
Old 10-25-2016
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:

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

oldExclude
Code:
Folder1/
Folder2/
Folder6/

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

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

Thanks in advance
# 2  
Old 10-25-2016
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.
# 3  
Old 10-25-2016
With this does Not work either:

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

# 4  
Old 10-26-2016
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:
Code:
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:
Code:
#!/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).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-27-2016
Well, I should add the
Code:
-exec ls -l

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

Code:
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
# 6  
Old 10-27-2016
Quote:
Originally Posted by Abu Rayane
Well, I should add the
Code:
-exec ls -l

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

Code:
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.
# 7  
Old 10-28-2016
So, can I extract only the date / time of these ignored folders?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude directories in FIND command

Can you please help tweak the below command to exclude all directories with the name "logs" and "tmp" find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs" find . -type f \( ! -name "*.log*" ! -name "*.jar*" \) -printNote: -path option/argument does not work with the version of find that i have. bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v (7 Replies)
Discussion started by: mohtashims
7 Replies

3. UNIX for Dummies Questions & Answers

How to find from / but exclude certain folder?

:)Hi Unix Specialists, I need your advice on how to find all the files from root ( / ) filesystem but exclude those from /export/home (different filesystem) folder. Below are some of the find statements that I have tried without success: find / -name '/export/home' -prune -o print -ls ... (6 Replies)
Discussion started by: gjackson123
6 Replies

4. Ubuntu

[Solved] Using Find with an exclude/exclude file

I am familiar with using tar and exclude/include files: tar zcf backup.dirs.tgz --files-from=include.mydirs --exclude-from=exclude.mydirs --no-recursion but was wondering if I could use find in the same way. I know that you can just specify the directories to exclude but my list is... (2 Replies)
Discussion started by: metallica1973
2 Replies

5. Shell Programming and Scripting

Find command with exclude

I had a Shell script that removes the files that are in a directory older than the specified days. find /test/files -mtime +10 I would like to add another condition to the find command above that is to exclude any file starting with ‘CGU' Thanks (1 Reply)
Discussion started by: db2dbac
1 Replies

6. Shell Programming and Scripting

Exclude a directory in 'find'

Hi, I'm in the process of writing a shell script which will be ran under cron hourly and will check for files of specific age in my ftp folder, then moves those over inside a folder called "old" (which is within the ftp dir). But, I'm unable to figure out how to exclude the "old" folder when... (1 Reply)
Discussion started by: mutex1
1 Replies

7. Shell Programming and Scripting

Help - Find command to exclude sub-directories

Hi Forum. I'm trying to write a script that finds and deletes files that are older than 300 days. The script will read a table that contains the following 3 columns: 1st col: “Y” means sub-directory scan; "N" means no subdirectory scan 2nd col: sub-directory location 3rd col: File prefix... (7 Replies)
Discussion started by: pchang
7 Replies

8. Shell Programming and Scripting

Find but exclude directories

Hello, I have a line in my script to find the files changed in the last 24 hours. It is as below: find /home/hary -type f -mtime -1 I now want to exclude a directory named "/home/hary/temp/cache" from the above find command. How do I add it to my script? Any help is appreciated. ... (9 Replies)
Discussion started by: tadi18
9 Replies

9. UNIX for Dummies Questions & Answers

find command to exclude directories

Howdy I have this directory structure ... eep eepaptest eepfatest eepgltest eep.old eeppoptest ehf ehfaptest ehfgltest ehp ehpgltest I want to find files in these directories, but I want to exclude eep, ehf & ehp. Cany anyone help with the correct command ?? (1 Reply)
Discussion started by: SmurfGGM
1 Replies

10. Shell Programming and Scripting

how do i exclude the current directory when using find?

i want to compile a list of files in all sub directories but exclude the current directory. the closest i could get was to search 'only' the current directory, which is the opposite of what i wanted. find . ! -name . -prune (7 Replies)
Discussion started by: mjays
7 Replies
Login or Register to Ask a Question