Find Command Include Sub Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Command Include Sub Directory
# 8  
Old 02-12-2015
Isn't '-type f' enough to stop directory names?
# 9  
Old 02-12-2015
In a directory where ls -l produces the output:
Code:
total 16
drwxr-xr-x  13 dwc  staff  442 Feb 11 15:04 payments
-rw-r--r--   1 dwc  staff  895 Feb 11 14:59 problem
-rwxr-xr-x   1 dwc  staff  212 Feb 11 16:06 tester

The commands:
Code:
find . -name payments -prune -type f
          and
find . \( -name payments -prune \) -type f

produce no output. (There is an implied AND operation between all of the subexpressions and there is no regular file named "payments"._ The command:
Code:
find . \( -name payments -prune \) -o -type f

produces the output:
Code:
./payments
./problem
./tester

But, the command:
Code:
find . \( -name payments -prune \) -o -type f -print

produces the output:
Code:
./problem
./tester

And, the command:
Code:
find . \( -name payments -prune -print \) -o -type f

produces the output:
Code:
./payments

With nothing that produces explicit output like -print anywhere in the expression, -print is supplied by default to each half of the OR (-o) in the expression. With any -print in the entire expression, an OR subexpression produces output only if there is an explicit element of that subexpression that prints output.
# 10  
Old 02-13-2015
So the '-name <pattern> -prune' upsets the normal implicit '-print' because the '-name <pattern>' is false in the hit space, although the -prune inverts its meaning. Seems like a design error, that pruning should have not used '-name' but been a separate, unified peer option that returns false on a hit and also prevents descending. Too late now. POSIX did not see the incongruity, I guess. The art of UNIX shell still seems to be in knowing all the gotcha's. Smilie
# 11  
Old 02-13-2015
Quote:
Originally Posted by DGPickett
So the '-name <pattern> -prune' upsets the normal implicit '-print' because the '-name <pattern>' is false in the hit space, although the -prune inverts its meaning. Seems like a design error, that pruning should have not used '-name' but been a separate, unified peer option that returns false on a hit and also prevents descending. Too late now. POSIX did not see the incongruity, I guess. The art of UNIX shell still seems to be in knowing all the gotcha's. Smilie
Not really. The thing that upset the normal default -print is that we have an -o binary primary and we only want to print the results from one of the subexpressions. (We don't want to print the names of the directories we have pruned on the left half of the OR, but we do want to print the regular files selected by the right half of the OR.) The POSIX standards documented existing practice; they did not invent new (even though possibly more intuitive) behavior for the find utility.

The behavior of the find -prune primary is described as:
Code:
	−prune	The primary shall always evaluate as true; it shall cause find not to descend the
		current pathname if it is a directory. If the −depth primary is specified, the −prune
		primary shall have no effect.

And the way the standards describe when pathnames are printed by default is:
Code:
	If no expression is present, −print shall be used as the expression. Otherwise, if the given
	expression does not contain any of the primaries −exec, −ok, or −print, the given expression
	shall be effectively replaced by:
		( given_expression ) −print

Personally, I think this is reasonable and rational behavior, but I agree that it is something that shell script programmer's have to learn and might not be intuitive until you do learn the details of how and when it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Find out directory where command is located

so i have a script that i do not want copies of that script to be roaming around. i want that script to be in only one location on the filesystem, and whoever wants to use it should just link to it. any idea on how to exit from a script if it is detected that the running version is a copy and... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

How to include file pattern in find command?

Hi I've to remove the files which has the following file pattern in path /home/etc/logs fnm_HST_date1 fnm_hst_date1 fnm_HST_date2 I've used the following code to to remove the files having file names like "HST" . #!/usr/bin/ksh set -x file_path=/home/etc/logs file_nm=HST find... (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Find command with ignore directory

Dear All, I am using find command find /my_rep/*/RKYPROOF/*/*/WDM/HOME_INT/PWD_DATA -name rk*myguidelines*.pdf -print The problem i am facing here is find /my_rep/*/ the directory after my_rep could be mice001, mice002 and mice001_PO, mice002_PO i want to ignore mice***_PO directory... (3 Replies)
Discussion started by: yadavricky
3 Replies

5. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

6. UNIX for Dummies Questions & Answers

find command to look for current directory only

i have this find command on my script as: for i in `find $vdir -name "$vfile" -mtime +$pday` the problem with this code is that the sub-directories are included on the search. how do i restrict the search to confine only on the current directory and ignore the sub-directories. please advise.... (7 Replies)
Discussion started by: wtolentino
7 Replies

7. Shell Programming and Scripting

Find command, -name by directory and subdirectory?

Hi All, I'm trying to use the find command to return matches for a directory and file. For example, given the following directories: /one/two/three/file1.txt /one/three/two/file1.txt /one/four/two/three/file1.txt I'm expecting the following to be returned: ... (16 Replies)
Discussion started by: makodarear
16 Replies

8. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

9. Shell Programming and Scripting

include all files under a directory

I want to include all the subnet files under /etc/dhcpd/ to /etc/dhcpd.conf so here is my content of dhcpd.conf ... include "/etc/dhcpd/*"; however, the check-syntax reports syntax error, as they do not recognize the wildcard *, and display that " file /etc/dhcpd/* could not be found. ... (4 Replies)
Discussion started by: fredao
4 Replies

10. UNIX for Dummies Questions & Answers

using find command only in current directory

I am trying to use the find command to find files in the current directory that meet a certain date criteria. find . -type -f -mtime +2 However, the above also checks the directories below. I tried -prune, but that seems to ignore this directory completely. I read about using -path w/... (5 Replies)
Discussion started by: jliebling
5 Replies
Login or Register to Ask a Question