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 =>
Moderator's Comments:
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.
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:
not search web pages for a matching example.
The command:
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.
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
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:
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
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
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:
does not prune files named secret; instead it produces the output:
(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:
or the logically equivalent:
produces the output:
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:
producing:
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:
producing the output:
or both of the above:
producing the output:
This User Gave Thanks to Don Cragun For This Post:
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)