Structure of find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Structure of find command
# 1  
Old 06-25-2016
Structure of find command

I am assuming questions about find command have been asked many times, however I couldn't find in the old threads what I was looking for. I want to know the basic structure of find command. I believe it is something like that:

find <paths> <conditions> <actions>

Does each command finally breaks down to above form? For example I want to list out all files and folders in the current directory except for those in a folder called 'secret'. I do as follows =>

Code:
find . -name 'secret' -prune -o -print

Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!



How is the above command broken up by find and evaluated? Is it like

<each file and folder> <tested with -name> <acted by -prune -o -print>
OR
<each file and folder> <tested with -name> <acted by -prune> ORed with<each file and folder> <tested with -name> <acted by -print>

This example may not be fully sufficient to illustrate the question, but that is close to what I am trying to understand. Any answers greatly appreciated.

Thanks,
RC

Last edited by RudiC; 06-26-2016 at 07:24 AM.. Reason: Added code tags.
# 2  
Old 06-25-2016
What you have to keep in mind is that -prune is an action and not a condition.
The general break down to the use of prune in find:
Code:
find <path> <condition of prune> -prune -o <other conditions> <action>


Last edited by Aia; 06-25-2016 at 09:39 PM..
# 3  
Old 06-25-2016
If you want to find out how the find command works, the obvious way to do so is to look at the find man page on your system:
Code:
man find

not search web pages for a matching example.

The command:
Code:
find . -name 'secret' -prune -o -print

searches for files in the file hierarchy rooted in the current working directory (.). The -o -print causes any pathnames that have been selected by prior criteria to be printed with one pathname per line. The prior criteria in this case (-name 'secret' -prune), says that if any file found while waking the file hierarchy is named secret and is of type directory, do not list that directory nor any files in the file hierarchy rooted in that directory. All other files that have not been skipped by the selection criteria will be listed.
# 4  
Old 06-26-2016
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.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
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:
# 6  
Old 06-27-2016
Very helpful.
I am using RHEL 6.2 which doesn't provide the man page for find. The --help also doesn't give info in much details. Once again, many thanks.
# 7  
Old 06-27-2016
Quote:
Originally Posted by Rameshck
Very helpful.
I am using RHEL 6.2 which doesn't provide the man page for find. The --help also doesn't give info in much details. Once again, many thanks.
I don't know of any Linux system that doesn't provide man pages. But, on many systems, loading those man pages is an installation option. If the system administrator who installed your system chose not to load man pages and there are people (like you) using that system to do any programming, ask your sys admin to load the man pages for you.

If all else fails, you can find the Linux 2.6 find man page in the UNIX & Linux forum Man Pages section as well as manual pages for several other systems.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question