Visit Our UNIX and Linux User Community


How-To Exclude Directory in find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How-To Exclude Directory in find command
# 1  
Old 06-28-2016
Hammer & Screwdriver How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs"

Code:
find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print

Note: -path option/argument does not work with the version of find that i have.
Code:
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v

# 2  
Old 06-28-2016
find . \( ! -type d \)
# 3  
Old 06-28-2016
Quote:
Originally Posted by vgersh99
find . \( ! -type d \)
Can you tweak my original command to add the condition of excluding this directory -> "/tmp/log" ??
# 4  
Old 06-28-2016
Does your version support the -prune action?
# 5  
Old 06-28-2016
Quote:
Originally Posted by RudiC
Does your version support the -prune action?
Yes.

it supports -prune

But, i m not sure how can i use -prune with the given command.

Last edited by mohtashims; 06-28-2016 at 02:33 PM..
# 6  
Old 06-28-2016
Your request is not clear.

Are you trying to exclude the contents of the specific directory /tmp/logs (which would only be a problem with the script you showed us if you were sitting in / when you invoked your script), or are you trying to exclude the contents of any directory logs that is located in a directory named tmp.

Either way a grep may be the easiest way to solve your problem:
Code:
find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '^./tmp/logs/'

or:
Code:
find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) | grep -v '/tmp/logs/'

depending on your answer to the above question.

Note, however that the first one above may exclude other directories from your search if you are not located in / when you invoke find.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-29-2016
@Don: Thank you for the suggestion.

But where can i put the -print option which you can see in the OP ?

Becoz in the next line i was reading the output file as you can see below.

Code:
find . -type f \( ! -name "*.log*"  ! -name "*.jar*" \) -print |
while read file
do

Please suggest.

Last edited by mohtashims; 06-29-2016 at 10:41 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #345
Difficulty: Medium
Scott McNealy received $238 million USD when the Oracle acquisition closed.
True or False?

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. Red Hat

Find command to exclude a specific path

Hi Folks, I want to run the below command and to exclude the specific path like /var/test/support/... . How to achieve using the below command find / -type f \( –perm –4000 –o –perm –2000 \) –print -Siva Please do not use FONT tags inside CODE tags. And, there is usually no reason to... (2 Replies)
Discussion started by: gsiva
2 Replies

3. UNIX for Dummies Questions & Answers

cmd find: exclude directory when using option -depth

hello, i want to use "-depth" in command "find" and want to exclude a directory. the find command should work in HP-UX and Linux. i see in the find man page: -prune If -depth is not given, true; do not descend the current directory. If -depth is given, false; no effect. -depth... (3 Replies)
Discussion started by: bora99
3 Replies

4. 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

5. Shell Programming and Scripting

SunOS: How to exclude directory in find command?

Hi All, First my OS version is: ksh:0$ uname -a SunOS 5.9 Generic_122300-48 sun4u sparc SUNW,Sun-Fire-V440 I want to exclude the following DIR(./country111) in my search pattern: ksh:0$ find . -name "*.tar" ./country111/COUNTRY_BATCH-801.tar ./country111/COUNTRY_BATCH-802.tar... (3 Replies)
Discussion started by: saps19
3 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. 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

9. Shell Programming and Scripting

How to exclude top level directory with find?

I'm using bash on cygwin/windows. I'm trying to use find and exclude the directory /cygdrive/c/System\ Volume\ Information. When I try to use the command below I get the error "rm: cannot remove `/cygdrive/c/System Volume Information': Is a directory. Can someone tell me what I am doing... (3 Replies)
Discussion started by: siegfried
3 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

Featured Tech Videos