Simple use of find command

Find command


Format: find [option] [Path] [filter condition] [processing action]
Path: default current directory
Filter condition: set filter condition on file/directory
Processing action: display all files by default
  Filter condition:
    -name Namefile or directory name, case sensitive
    -iname NameCase-insensitive Filter by file
    -user User_Nameowner Filter
    -group Group_Nameby file group Filter
    -uid UIDby UID, when a user is deleted, only the UID of the user remains on the file/directory, no user name is displayed
    -gid GIDFilter by GID, when a group is deleted , only the GID is left on the file/directory, no group name shows
    -nouserthe files left by the deleted user The files left
    -nogroupby the deleted group are
    -size [+|-] n [unit]
      +|-
        -size n [unit]n-1 to n units of files
        -size +n [unit]larger than n+1 units of files are
        -size -n [unit]less than n-1 units of file
      unit
        cbits  kKB  MMB  GGB
    filtered by timestamp
      in days: ---------(now-n-1)--------(now-n)- -------now---->
        -atime [+|-] nHours, minutes and seconds are still valid
          -atime n (now-n-1) to (now-n)
          -atime -n(now-n) to (now) to be accessed to
          -atime +n(now-n-1) ) used
        -mtime [+|-] nto be the same as atime
        -ctime [+|-] nand atime

[root@ZYB test_dir1]# date
Wed Apr 25 09:05:23 CST 2018
[root@ZYB test_dir2]# ls
test_file00  test_file01  test_file10  test_file11  test_file20  test_file21
[root@ZYB test_dir1]# touch -mt "04220700.00" test_file00
[root@ZYB test_dir1]# touch -mt "04221300.00" test_file01
[root@ZYB test_dir1]# touch -mt "04230700.00" test_file10
[root@ZYB test_dir1]# touch -mt "04231300.00" test_file11
[root@ZYB test_dir1]# touch -mt "04240700.00" test_file20
[root@ZYB test_dir1]# touch -mt "04241300.00" test_file21
[root@ZYB test_dir1]# stat test_file* | grep "Mod"
Modify: 2018-04-22 07:00:00.000000000 +0800
Modify: 2018-04-22 13:00:00.000000000 +0800
Modify: 2018-04-23 07:00:00.000000000 +0800
Modify: 2018-04-23 13:00:00.000000000 +0800
Modify: 2018-04-24 07:00:00.000000000 +0800
Modify: 2018-04-24 13:00:00.000000000 +0800
[root@ZYB test_dir1]# find -mtime 1
./test_file20
./test_file11
[root@ZYB test_dir1]# find -mtime -1
.
./test_file21
[root@ZYB test_dir1]# find -mtime -2
.
./test_file20
./test_file21
./test_file11
[root@ZYB test_dir1]# find -mtime +2
./test_file00

      In minutes: --------(now-x)--------(now-x+1)-------now----> The
        -amin [+|-] nsecond unit is still valid
          -amin x (now-x) to (now-x+1)
          -amin -x(now-x) to (now)
          -amin +x(now-x) before
        -mmin [+|-] xthe same amin
        -cmin [+|-] xthe same as amin

[root@ZYB test_dir2]# ls
test_file00  test_file03  test_file10  test_file13  test_file20  test_file23
test_file01  test_file04  test_file11  test_file14  test_file21  test_file24
test_file02  test_file05  test_file12  test_file15  test_file22  test_file25
[root@ZYB test_dir2]# stat test_file0? | grep "Modify"
Modify: 2018-04-25 08:29:05.000000000 +0800
Modify: 2018-04-25 08:29:15.000000000 +0800
Modify: 2018-04-25 08:29:25.000000000 +0800
Modify: 2018-04-25 08:29:35.000000000 +0800
Modify: 2018-04-25 08:29:45.000000000 +0800
Modify: 2018-04-25 08:29:55.000000000 +0800
[root@ZYB test_dir2]# stat test_file1? | grep "Modify"
Modify: 2018-04-25 08:30:05.000000000 +0800
Modify: 2018-04-25 08:30:15.000000000 +0800
Modify: 2018-04-25 08:30:25.000000000 +0800
Modify: 2018-04-25 08:30:35.000000000 +0800
Modify: 2018-04-25 08:30:45.000000000 +0800
Modify: 2018-04-25 08:30:55.000000000 +0800
[root@ZYB test_dir2]# stat test_file2? | grep "Modify"
Modify: 2018-04-25 08:31:05.000000000 +0800
Modify: 2018-04-25 08:31:15.000000000 +0800
Modify: 2018-04-25 08:31:25.000000000 +0800
Modify: 2018-04-25 08:31:35.000000000 +0800
Modify: 2018-04-25 08:31:45.000000000 +0800
Modify: 2018-04-25 08:31:55.000000000 +0800
[root@ZYB test_dir2]# date
Wed Apr 25 08:48:19 CST 2018
[root@ZYB test_dir2]# find -mmin 18
./test_file20
./test_file21
./test_file15
./test_file13
./test_file14
./test_file12
[root@ZYB test_dir2]# date
Wed Apr 25 08:53:31 CST 2018
[root@ZYB test_dir2]# find -mmin -19
.
./test_file20
./test_file21
./test_file15
./test_file24
./test_file13
./test_file14
./test_file23
./test_file25
./test_file22
[root@ZYB test_dir2]# date
Wed Apr 25 08:49:33 CST 2018
[root@ZYB test_dir2]# find -mmin +23
./test_file02
./test_file11
./test_file00
./test_file04
./test_file10
./test_file12
./test_file01
./test_file05
./test_file03

    Use the permission as the filter condition -perm [+|-] MODE
      -perm MODEto match
      -perm +MODEany specified permission bit of any user type exactly, -to be /replaced by the specified permission bit of the
      -perm -MODEspecified user type When the
  filter conditions are combined, the brackets need to be escaped

[root@ZYB ~]# find /usr/ -not -user root -not -user bin -ls
2102377    0 -rw-r--r--   1 zyb      zyb             0 Apr 24 20:18 /usr/haha
[root@ZYB ~]# find /usr/ -not \( -user root -o -user bin \) -ls 
2102377    0 -rw-r--r--   1 zyb      zyb             0 Apr 24 20:18 /usr/haha
# -not空格\(空格-user空格root空格-o空格-user空格bin空格\) 空格不能省略

  Processing action:
    -printprint to standard output and
    -lsdisplay it in long format
    -exec COMMAND {} \;for processing the found content. The backslash is an escape character, and a space is used between the curly braces and the backslash.

[root@ZYB test_dir1]# ls
test_file1  test_file2
[root@ZYB test_dir1]# find -name "tes*" -exec mv {} {}_haha \;

    -ok COMMAND {} \;Interactive mode, the same -exec, but requires the user to confirm the executed command
    -xargs COMMANDto operate the search content before operating

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324814522&siteId=291194637