find is one of the most usefull and important utilities any *NIX has. We will try to demonstrate you with some find basics.
find begins at a specified point on a directory tree and searches all lower branches for files that match a given set of parameters. $ find path operators path is the directory in which find will begin to search. Any number of directories can be specified. The search is recursive. opeators/options tells find which files you are interested in. The operators/options are:
-name filename
Find files with the given filename.
-perm mode
Find files with the given access mode (permission)
-type c
Finds the files of the given type.
-
b - Block special file (device file)
c - Character special file (device file)
d - Directory
f - Regular file
l - Symbolic link
p - Named pipe file
s - Socket
n block long ( a block is 512 bytes). You can also use +n for finding files
that are bigger than n blocks long. If we use nc we refer to n bytes.
number n.
were accessed n days ago.
You can also use the +n for finding files that were accessed over n
days ago, or even -n for finding files that were accessed less than n days ago.
find files that were modified n days ago.
here we find files that were changed n days ago.
modified more recently than
file.
Matching several criteria:
that match operator1 and operator2. The operator -a is not necessary, you can also
do as folows: operator1 operator2 and find will assume you want files that
match both of them.
operator 1 or operator2.
operator.
evaluate the expression before the rest.
What actions to do when we find those files?
found. You will not get any output to the screen from the command unless you will use -print.
those files that were found
but it prompts you before performing the command on the files that were found.
How do find works?
- find looks at one file at a time.
- find using the files i-node information to evaluate an expression
given by the command line operators.
Note: Spaces are requierd before and after operators like !, \(, \),
and {}, in addition to spaces before and after every other operator.
Examples:
-
$find . -mtime 10 -print
Find files on our current directory and all its sub-directories that were modified 10 days ago.
Find files on your current directory and all its sub-directories that were modified
between 5-8 days.
Find files on your current directory and all its sub-directories that are FILES and were
not accessed for more than 35 days.
Find files on our current directory and all its sub-directories that either ends with a
.tmp or ends with a .c.
modified over 6 days ago. How can i do this?
$find . -mtime +6 \( -name "*.tmp" -o -name "*.c"\) -print
Lets explain what we have done. We evaluate the -name "*.tmp" -o -name "*.c"
expression first, than if its true (their are files that match it) only than we look for
files that were last modified over 6 days ago also than if their is a match find
will print it to standard output.
What whould happen if we were doing this?:
$find . -mtime +6 -name "*.tmp" -o name "*.c" -print
This is not what we wanted. Why? when find see two operators without -o between
them it interperate it as AND. It will look for files that were last modified over 6
days ago and end with "*.tmp" or for files that end with "*.c". This is obviously
not what we were asking for.
mark:
$find . ! \( -mtime +6 \( -name "*.tmp" -o -name "*.c"\) \) -print
Finding files on the system with group permission set to write and giving them
execute permissions as well. What will be the difference if we will use this command?
$find / -type f -perm -20 -ok chmod g+x {} \;
It would have asked us to verify every execution.
Note: We used here -perm -20 to match all files that
have group permissions set to w, ----w----. This will match any combination
that includes group write permission, such as : rwxrwxrws (-perm 777), rwxrwxr-w (-perm 775)
rwxrw-rw- (766) etc.
If we were using :
$find / -type f -perm 777 -print
It would only list files with permission that is rwxrwxrwx and not others.
We use the minus (-) sign and the Octal value to reach this varaiety of files and their
permissions.
Will find files on the current directory and its subdirectories that are greater than 15,000 bytes
but less than 20,000 bytes.
Find all of the user mia's files that are with permission -rwxr-xr-x.
Tips
- Times in find command (refering to -atime -mtime -ctime):
- If you specify a number with no sign, for example 3, it means the 24-hour period
that ended excactly three days ago. Every thing between 72-96 hours.
since that time. Example -mtime -3 is any time between now and three days ago.
Every thing between 0-72 hours.
that time. Example -mtime +3 is any time more than 3 days ago.
and date? How can we do that? The answer is quite simple:
-
$touch 09151600 4pm15sep
$find / -newer 4pm15sep -print
What we made here is an empty file and gave it the 15-9 as the date and 4pm as the time using
the touch command. Now we simply search our system (/) for newer files than 4pm15sep.
-
$touch 0915160093 4pmsep15_93
$touch 0820214795 9_47pmaug8_95
$find . -newer 4pmsep15_93 ! 9_47pmaug8_95 -print
Last command will search our current directory and its subdirectories for files between
4pm 15th september 1993 and 9:47 pm August 8th 1995.
Piping files with spaces to xargs
Change mode of all files to "-rw-r--r--". This will also include files with spaces.