July 19, 2022

RHEL 9.0 Finding Files with find

$ man find
...
       -name pattern
              Base  of  file  name  (the path with the leading directories removed) matches shell pattern pattern. 

       A numeric argument n can be specified to tests (like -amin, -mtime, -gid, -inum, -links, -size, -uid and -used) as

       +n     for greater than n,

       -n     for less than n,

       n      for exactly n.

       -mmin n
              File's data was last modified less than, more than or exactly n minutes ago.

       -perm -mode
              All of the permission bits mode are set for the file.  Symbolic modes are accepted in this form, and this is usu‐
              ally the way in which you would want to use them.  You must specify `u', `g' or `o' if you use a  symbolic  mode.
              See the EXAMPLES section for some illustrative examples.

       -size n[cwbkMG]
              File uses less than, more than or exactly n units of space, rounding up.  The following suffixes can be used:

              `b'    for 512-byte blocks (this is the default if no suffix is used)

              `c'    for bytes

              `w'    for two-byte words

              `k'    for kibibytes (KiB, units of 1024 bytes)

              `M'    for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)

              `G'    for gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)

              The size is simply the st_size member of the struct stat populated by the lstat (or stat) system call, rounded up
              as  shown  above.  In other words, it's consistent with the result you get for ls -l.  Bear in mind that the `%k'
              and `%b' format specifiers of -printf handle sparse files differently.  The `b' suffix  always  denotes  512-byte
              blocks and never 1024-byte blocks, which is different to the behaviour of -ls.

              The + and - prefixes signify greater than and less than, as usual; i.e., an exact size of n units does not match.
              Bear in mind that the size  is  rounded  up  to  the  next  unit.   Therefore  -size -1M  is  not  equivalent  to
              -size -1048576c.  The former only matches empty files, the latter matches files from 0 to 1,048,575 bytes.

       -gid n File's numeric group ID is less than, more than or exactly n.

       -type c
              File is of type c:

              b      block (buffered) special

              c      character (unbuffered) special

              d      directory

              p      named pipe (FIFO)

              f      regular file

              l      symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic
                     link is broken.  If you want to search for symbolic links when -L is in effect, use -xtype.

              s      socket

              D      door (Solaris)

              To search for more than one type at once, you can supply the combined list of type letters separated by  a  comma
              `,' (GNU extension).

       -uid n File's numeric user ID is less than, more than or exactly n.
...

$ sudo find /etc -name '*pass*'
$ sudo find /home -user student
$ sudo find /home -group student 
$ sudo find -uid 1000
$ sudo find -gid 1000
$ sudo find /etc -perm 644 -ls
$ sudo find /etc -perm 644 -ls
$ sudo find / -size +1G 2> /dev/null

When used with / or - signs, the 0 value works as a wildcard because it means any permission.

$ sudo find / -perm -1000 -ls

To search for files for which the user has read permissions, or the group has at least read permissions, or others have at least write permission:

$ sudo find /home -perm /442 -ls

To search for all files with content that changed 120 minutes ago

$ sudo find / -mmin 120

To search for all files with content that changed 200 minutes ago:

$ sudo find / -mmin +200

The following example lists files that changed less than 150 minutes ago:

$ sudo find / -mmin -150

Search for all directories in the /etc directory:

$ sudo find /etc -type d

Search for all soft links in the / directory:

$ sudo find / -type l

Search for all block devices in the /dev directory:

$ sudo find /dev -type b

Search for all regular files with more than one hard link:

$ sudo find / -type f -links +1

No comments: