days ago, or even -n for finding files that were accessed less than n days ago.

How do find works?

  • The action is taken if the expression value is true.

    Note: Spaces are requierd before and after operators like !, \(, \),
    and {}, in addition to spaces before and after every other operator.




    Examples:

    Find files on our current directory and all its sub-directories that were modified 10 days ago.

  • $find . -mtime +5 -mtime -8 -print

    Find files on your current directory and all its sub-directories that were modified
    between 5-8 days.
  • $find . -type f -atime +35 -print

    Find files on your current directory and all its sub-directories that are FILES and were
    not accessed for more than 35 days.
  • $find . -name "*.tmp" -o -name "*.c" -print

    Find files on our current directory and all its sub-directories that either ends with a
    .tmp or ends with a .c.

  • Now i want to find files that end with either *.tmp or *.o and that were last
    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.

  • What if we do not want to match the criteria we put on the last example? We put a !
    mark:

    $find . ! \( -mtime +6 \( -name "*.tmp" -o -name "*.c"\) \) -print
  • $find / -type f -perm -20 -exec chmod g+x {} \;

    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.

  • $find . -size +15000c -size -20000c -print

    Will find files on the current directory and its subdirectories that are greater than 15,000 bytes
    but less than 20,000 bytes.
  • $find . -type f -user mia -perm 755 -print

    Find all of the user mia's files that are with permission -rwxr-xr-x.


    Tips

  • If you specify a number with a - (minus) sign (-mtime -3) it referce to the period
    since that time. Example -mtime -3 is any time between now and three days ago.
    Every thing between 0-72 hours.
  • If you specify a number with + (plus) sign, it referce to the 24-hour period before

    that time. Example -mtime +3 is any time more than 3 days ago.

  • What if you want to find files that were created on your system after a specific time
    and date? How can we do that? The answer is quite simple:

    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.

  • We can use newer to search our system for files between two accurate times:
  • $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.

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.

    Piping files with spaces to xargs

    Change mode of all files to "-rw-r--r--". This will also include files with spaces.

    $ find ./ -type f -printf "\"%p\"\n"| xargs chmod 644
    

    The Power of Find !!

    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

    -user name Find files belonging to user name.
    -group name Find files belonging to group name.
    -size n Find files that are
    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.
    -inum n Finds files with the inode
    number n.
    -atime n Finding files that
    were accessed n days ago.
    You can also use the +n for finding files that were accessed over n
    -mtime n
    Same as -atime but here we
    find files that were modified n days ago.
    -ctime n Same as -atime and -mtime but
    here we find files that were changed n days ago.
    -newer file Find files that have been
    modified more recently than
    file.

    Matching several criteria:
    operator1 -a operator2 Find files
    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.
    operator1 -o operaror2 Find files that match
    operator 1 or operator2.
    !operator Find files that do not match
    operator.
    \( expression \) In a complex expression,
    evaluate the expression before the rest.

    What actions to do when we find those files?
    -print Print the file's that were
    found. You will not get any output to the screen from the command unless you will use -print.
    -exec command Execute a command on
    those files that were found
    -ok command Same as -exec command
    but it prompts you before performing the command on the files that were found.
    Comment