find command

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]

find [path path …]

Option

  • -depth step into sub files first, then current directory
    .The -delete action also implies -depth

  • -name [-iname] find file by name
    find /etc /root -name passwd

  • -type

    • d directory
    • f regular file
  • -size

    • b for 512-byte blocks (this is the default if no suffix is used)
    • c for bytes
    • w for two-byte words
    • k for Kilobytes (units of 1024 bytes)
    • M for Megabytes (units of 1048576 bytes)
    • G for Gigabytes (units of 1073741824 bytes)
    1
    2
    3
    4
    # find files bigger than 30 kilobytes
    # %k %b in printf represent different block size

    find -size +30k -printf '%p %k %b \r\n'
  • -a[and] -o[or] -not logic option

    1
    2
    3
    4
    5
    6
    # find files name end with sh, and belong to user
    # ubuntu or group id is 1000
    find /usr -name '*.sh' -a -user ubuntu -o -gid 1000

    # find files name not end with class
    find . -not -name '*.class'
  • -atime -mtime -ctime -amin -mmin -cmin
    a access file
    m modify file
    c change property
    time days
    min minutes

    1
    2
    3
    4
    5
    # find files be accessed in last 19 days
    find -atime -19 -printf '%p %a %t \r\n'

    # find files not be accessed in last 19 days
    find -atime +19 -printf '%p %a %t \r\n'
  • -newer -anewer -cnewer
    based on parameter file modify time
    -newer File was modified more recently than file
    -anewer File was last accessed more recently than file was modified.
    -cnewer File’s status was last changed more recently than file was modified.

  • -perm
    find files based on privilege

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # find files in current directory which
    # permissions is -rwxr-xr-x
    find -perm 755

    # at least a kind of user have write privilege
    find -perm +444

    # all kind user have write privilege
    find -perm -444

Action

  • -printf format out put

    1
    2
    3
    #   name  depth  space[512-kbytes]  space[1024-kbytes]
    # permissions access_time change_time modify_time
    find -user ubuntu -printf '%p %d %b %k %M %a %c %t \r\n'
  • -ls
    list in ls command

  • -ok
    ask user yes/no execute command

  • -exec command {} +
    execute command no asking

    1
    2
    3
    4
    # delete files in current directory no accessed in 30 days
    find . -atime +30 –exec rm –rf {}
    # same effect
    find . -atime +30 | xargs rm -rf

linux find