Sponsored Content
Full Discussion: Structure of find command
Top Forums Shell Programming and Scripting Structure of find command Post 302976236 by Don Cragun on Sunday 26th of June 2016 04:55:37 PM
Old 06-26-2016
Quote:
Originally Posted by MadeInGermany
Actions return a status like condtions, so can be followed by more conditions or actions.
Most actions (like -prune) return a true.
The default operator is -a (logical "and") and has higher precedence than -o (locical "or").
You can write your example as
Code:
find . \( -name 'secret' -a -prune \) -o -print

The right side of an "and" is only executed if the left side is true.
The right side of an "or" is only executed if the left side is false.
Applied to your example:
If the -name 'secret' is true then the -prune is executed and gives true, so the parenthesis part is true, so the -print is not executed.
If the -name 'secret' is false then the parenthesis part is false, the -prune is skipped, the -print is executed.
It isn't quite that simple... Most primaries evaluate to true if and only if some condition is true. (For example the primary -name secret evaluates to true if and only if the name of the file it is currently processing is named secret.) The -prune primary ALWAYS evaluates to true.

For purposes of this discussion, let us assume that we have a file hierarchy rooted in . as shown by
Code:
$ ls -lR .
total 8
drwxr-xr-x  6 dwc  staff  204 Jun 25 17:48 secret
-rwxr-xr-x  1 dwc  staff  141 Jun 26 09:12 tester
drwxr-xr-x  6 dwc  staff  204 Jun 25 17:48 wideopen

./secret:
total 0
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 1
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 2
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 3
-rw-r--r--  1 dwc  staff  0 Jun 25 17:48 secret

./wideopen:
total 0
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 1
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 2
-rw-r--r--  1 dwc  staff  0 Jun 25 17:13 3
-rw-r--r--  1 doc  staff  0 Jun 25 17:48 secret

Note the there is a directory named secret in . and regular files named secret in the directories ./secret and ./wideopen.

The -prune primary is different from all of the other primaries. The -prune primary causes files in the file hierarchy under any directory it is given to be ignored (pruned) by find. The -prune primary does NOT prune the name of the directory itself. The other important thing to remember for this discussion is that the find utility by default adds a -print primary to the end of the command line if and only if the arguments supplied by the invoker do NOT include any -print primaries AND do NOT include any -exec command [;|&] primaries and do NOT include any -ok command ; primaries. So if any portion of the primaries in a command separated by -o operators does not include a -print, -ok, or -exec; that set of primaries between -o operators will produce no output. So, surprisingly to some users, the command:
Code:
find . -name secret -prune

does not prune files named secret; instead it produces the output:
Code:
./secret
./wideopen/secret

(i.e., it prints the pathnames of files named secret that are not located in a file hierarchy rooted in a directory named secret so ./secret/secret is not printed) and there is no -o operator to select files that are not named secret.

And the command:
Code:
find . -name secret -prune -o -print

or the logically equivalent:
Code:
find . \( -name secret -a -prune \) -o -print

produces the output:
Code:
.
./tester
./wideopen
./wideopen/1
./wideopen/2
./wideopen/3

By choosing the primaries and operators in your find command, you can choose to have find with -prune print the names of non-directory files with a name matching the names of directories being pruned, but not print the names of pruned directories, and print the pathnames of any files that have not been pruned that are not named secret:
Code:
find . -name secret -type d -prune -o -print

producing:
Code:
.
./tester
./wideopen
./wideopen/1
./wideopen/2
./wideopen/3
./wideopen/secret

or print the pathnames of pruned directories and the pathnames of non-directory files with the names of pruned directories, and print the pathnames of any other files that have not been pruned:
Code:
find . -name secret -prune -type d -print -o ! -name secret -print

producing the output:
Code:
.
./secret
./tester
./wideopen
./wideopen/1
./wideopen/2
./wideopen/3

or both of the above:
Code:
find . -name secret -prune -print -o -print

producing the output:
Code:
.
./secret
./tester
./wideopen
./wideopen/1
./wideopen/2
./wideopen/3
./wideopen/secret

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies

2. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies

3. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

4. UNIX for Dummies Questions & Answers

Why cant i find my ftp directories in the unix structure

Hi, I'm new here so hello to everyone...i'm also new to linux/unix but got my first dedicated server about 2 weeks ago and have been using ssh to configure it on ubuntu linux. I have a question about something i can't work out....if i use ftp to transfer files ( php, html, javascript files... (2 Replies)
Discussion started by: elduderino
2 Replies

5. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

6. Shell Programming and Scripting

stop unix find on a directory structure after finding 1st occurrence

Hi, Has anyone tried to restrict Solaris 10 unix find on a large directory structure based on time to stop running after finding the first occurrence of a matching query. Basically I'm trying to build up a usage map of user workspaces based on file modification (week/month/3 months/year etc) and... (3 Replies)
Discussion started by: jm0221
3 Replies

7. Shell Programming and Scripting

Script to run a command on all txt files present in a dir structure

Hi, I have a directory structure like the one given below root\a\b1 root\a\b2 root\b\b1 root\b\b2 . . . root\j\b1 root\j\b2 Now, there are a txt files in each dir and subdir, there is a root.txt I have to write a script where in i have to run a command called "genrb <filename>"... (6 Replies)
Discussion started by: vikramsinghnegi
6 Replies

8. UNIX for Dummies Questions & Answers

Find a tree structure in software modules

I have a list of software funtions in tcl code. Some of these functions call other functions. I want to build a tree structure of all called functions. Right now I list all the functions into a file then read this file so that I can cat each function and grep for EXECUTE (command that calls... (0 Replies)
Discussion started by: MissI
0 Replies

9. Shell Programming and Scripting

find size, cp breaks directory structure

I'm using this now: find /some/path/with/sourcefiles -type f -size -7M -exec /bin/cp -uv {} /some/path/ \; but it doesn't preserve the directory structure, also I've tried it with find /some/path/with/sourcefiles -type f -size -7M -exec /usr/bin/rsync -auv {} /some/path/ \; but that doesn't... (9 Replies)
Discussion started by: unclecameron
9 Replies

10. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies
All times are GMT -4. The time now is 04:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy