[linux find command] find files in the directory and delete files


find command format

find pathname -options [-print -exec -ok]

find takes two parameters:

  • pathname, the first parameter is used to specify the search path.
  • options, the second parameter is used to specify the search content.

It is not recommended to use an excessively large search range for the find command search, as it will consume a large amount of system resources and cause excessive pressure on the server.

1. Search by file name

find search path [option] search content

options:

  • -name: search by filename
  • -iname: search by file name, regardless of file name size
  • -inum: search by inode number

for example:

  • name
# 搜索的文件名必须和你的搜索内容一致才能找到。如果只包含搜索内容,则不会找到。
[wqf@b1i10 rm_test]$ find ./ -name test_3.txt
./test_3.txt
  • bend down

File names in Linux are case-sensitive, that is to say, if you search for lowercase files, you will not find uppercase files.

# 不区分文件名大小写,用iname
[wqf@b1i10 rm_test]$ find ./ -iname test_3.txt
./TEST_3.TXT
./test_3.txt
  • drink

Each file has an inode number, and if we know the inode number, we can also search for files according to the inode number.

# 如果知道文件名,用"ls -i"来査找inode号
[wqf@b1i10 rm_test]$ ls -i test_3.sh
29884450 test_3.sh
# 如果知道inode号,则可以用find命令来査找文件
[wqf`在这里插入代码片`@b1i10 rm_test]$ find ./ -inum 29884450
./test_3.sh

The find command is based on the depth search of the directory
to traverse the folder recursively and list all the files in the current directory and subdirectories

命令 " find ./ -name "*.txt" "  ,./ 路径下列出当前目录及子目录下以.txt结尾的文件。

If you don't want to search folders recursively, as long as the files in the current directory match the search range.

命令 " find ./ -maxdepth 1 -name "\*.txt"   " ,./ 当前目录下查找以.txt结尾的文件,-maxdepth 1表示查找深度为1。

If you want to search all files in at least 2 subdirectories below the current directory

命令 " find ./ -mindepth 2 -name "\*.txt"  "  

2. Search by file size

find search path [option] search content

options:

  • -size[+-] Size: Search for files according to the specified size
    "+" means to search for files larger than the specified size, "-" means to search for files smaller than the specified size.

The default unit of find is 512Byte. If the unit is b or the unit is not written, it will search according to 512Byte. Other size search units are as follows:

'c' for bytes
 #搜索单位是c,按照字节搜索
'w' for two-byte words
#搜索单位是w,按照双字节(中文)搜索
'k'for Kilobytes (units of 1024 bytes)
#按照KB单位搜索,必须是小写的k
'M' for Megabytes (units of 1048576 bytes)
#按照MB单位搜索,必须是大写的M
'G' for Gigabytes (units of 1073741824 bytes)
#按照GB单位搜索,必须是大写的G

for example:

# 当前目录下面有一个大小是2M的文件
[wqf@b1i10 rm_test]$ ls -l --block-size=M mon_tj_cal.txt
-rw-rw-r-- 1 summary_fz_province summary_fz_province 2M Dec  2 12:53 mon_tj_cal.txt

# 当前目录下,查找大小刚好2M的文件,可以找到
[wqf@b1i10 rm_test]$ find ./ -size 2M
./mon_tj_cal.txt

# 搜索小于2M的文件,可以找到很多其他文件
[wqf@b1i10 rm_test]$ find ./ -size -2M
./
./mon_tj_cal.sh
./file.patch
./test_3.sh
./file1.txt
./newtest1
./4.sh
./TEST_3.TXT
./test.txt
./testfile.txt
./1.txt
./test_3.txt
./newtest2
./execute_test.sh
./file2.txt
./test_1.sh
./mon_tj_cal_1.sh
./test_2.sh

3. Search by modification time

find search path [option] search content

options:

  • -atime[+-] time: Search by file access time , record the time when the file was last accessed.
  • mtime[+-] time: Search by the modification time of the file data . When the content of this file is modified, the time displayed by modify will be updated once.
  • ctime[+-] time: Search by file status modification time , the time that changes with file inode changes when file content, file permissions, and link attributes are changed.

Take mtime as an example of the meaning of “[±]” time.
-5: represents files modified within 5.
5: Represents the files modified on the previous 5~6 days.
+5: Indicates files modified 6 days ago.

# 查找6天前修改的文件
[wqf@b1i10 rm_test]$ find ./ -mtime +5
./mon_tj_cal.sh
./newtest1
./4.sh
./test.txt
./testfile.txt
./1.txt
./mon_tj_cal.txt
./newtest2
./gz_silence_val_1.sh
./execute_test.sh
./mon_tj_cal_1.sh

# 查找5天内修改过的文件
[wqf@b1i10 rm_test]$ find ./ -mtime -5
./
./test_3.sh
./TEST_3.TXT
./test_3.txt
./test_1.sh
./test_2.sh

# 查找 5~6 天那天修改的文件
[wqf@b1i10 rm_test]$ find ./ -mtime 5
./file.patch
./file1.txt
./file2.txt

4. Search by file type

find search path [option] search content

options:

  • -type d: find directory
  • -type f: find ordinary files
  • -type l: Find soft link files

for example:

# 查找./目录下面有哪些子目录
[wqf@b1i10 zxt_test]$ find ./ -type d
./
./take_data
./make_data
./drop_tables
./drop_tables/history
./drop_tables/test
./rm_test
./python
./push_data

5. Search by authority

find search path [option] search content

options:

  • -perm permission model: Find files with file permissions exactly equal to "permission model"
  • -perm -permission mode: Find files whose file permissions all contain "permission mode"
  • -perm /+permission mode: Find files whose file permissions contain any permission of "permission mode"

The permission types of files under Linux generally include read, write, and execute. The corresponding letters are r, w, x. We stipulate that the numbers 4, 2 and 1 represent read, write, and execute permissions (for specific reasons, see the detailed explanation of permissions in the next section), that is, r=4, w=2, x=1.
The granularity of permissions under Linux includes owner, group, and other groups. Each file can set different rwx (read, write and execute) permissions for three granularities

for example:

# 测试
[wqf@b1i10 test]$ touch test_1.sh test_2.sh test_3.sh test_4.sh
[wqf@b1i10 test]$ chmod 200 test_1.sh (只有拥有者有写的权限。)
[wqf@b1i10 test]$ chmod 444 test_2.sh (所有用户只拥有读的权限。)
[wqf@b1i10 test]$ chmod 600 test_3.sh (只有拥有者有读写权限。)
[wqf@b1i10 test]$ chmod 755 test_4.sh (拥有者有读、写、执行权限;而属组用户和其他用户只有读、执行权限。)

# 查看权限
[wqf@b1i10 test]$ ll
total 0
--w------- 1 wqf wqf 0 Jan 10 15:13 test_1.sh
-r--r--r-- 1 wqf wqf 0 Jan 10 15:13 test_2.sh
-rw------- 1 wqf wqf 0 Jan 10 15:13 test_3.sh
-rwxr-xr-x 1 wqf wqf 0 Jan 10 15:13 test_4.sh

# 按照指定权限搜索文件,文件的权限必须和搜索指定的权限一致,才能找到
[wqf@b1i10 test]$ find ./ -perm 200
./test_1.sh
[wqf@b1i10 test]$ find ./ -perm 444
./test_2.sh

# "-perm-权限模式":必须完全包含。
# 因为 test_1.sh 的权限是200--w-------)、test_3.sh 的权限600-rw-------)、test_4.sh 的权限755-rwxr-xr-x),所以可以找到;而test_2.sh的权限是(-r--r--r--),不包括200--w-------)权限,所以找不到。
[wqf@b1i10 test]$ find ./ -perm -200
./
./test_3.sh <-此文件权限为600
./test_4.sh <-此文件权限为755
./test_1.sh <-此文件权限为200

# "-perm+权限模式":包含任意一个指定权限,就可以找到。
# 因为test_2.sh的权限444-r--r--r--)、test_3.sh 的权限600-rw-------)、test_4.sh 的权限755-rwxr-xr-x)都含有444-r--r--r--)权限,所以可以找到;而test_1.sh 的权限是200--w-------),所以找不到。

[wqf@b1i10 test]$ find ./ -perm /+444
./
./test_3.sh <-此文件权限为600
./test_4.sh <-此文件权限为755
./test_2.sh <-此文件权限为444

6. Search by owner and group

find search path [option] search content

options:

  • -uid user ID: Find files whose owner is the specified ID according to user ID
  • -gid group ID: Find files whose group belongs to the specified ID according to the user group ID
  • -user username: Find files owned by the specified user by username
  • -group group name: Find files whose group belongs to the specified user group according to the group name
    -nouser: Find files without owner

Seven, logical operators

find search path [option] search content

options:

  • -a: and logic and
  • -o: or logical or
  • -not: not logical NOT

for example:

1) -a:and logic and
find commands also support logical operator options, where -a stands for logic and operation, that is, both conditions of -a are true, and the search result of find is true.

#在当前目录下搜索等于1115KB,并且文件类型是普通文件的文件
[wqf@b1i10 rm_test]$ find ./ -size 1115k -a -type f
./mon_tj_cal.txt

2) The -o:or logical or
-o option represents a logical or operation, that is, as long as one of the two conditions of -o is true, the find command can find the result.

#在当前目录下搜索文件名要么是cals的文件,要么是test_3.sh的文件
[wqf@b1i10 rm_test]$ find ./ -name cals -o -name test_3.sh
./test_3.sh
./test/test_3.sh

3) -not:not logical not
-not is logical not, which means negation.

#在当前目录下搜索文件名不是test_3.sh的文件
[wqf@b1i10 test]$ find ./ -not -name  test_3.sh
./
./test_4.sh
./test_1.sh
./test_2.sh

8. Other options

1) -exec option

find search path [option] search content -exec command 2 {} \;

Note: "{}" and "\;" here are standard formats, as long as the "-exec" option is executed, these two symbols must be completely entered, and there is a space between {} and \. The function of this option is actually to hand over the result of the find command to the command 2 invoked by "-exec" for processing. "{}" represents the search result of the find command, \ is for escape, and ; is the terminator.

for example:

# 查找当前开始30天之前的并且文件名以".log"结尾的文件并删除
# 使用"-exec"选项,把find命令结果直接交给"rm -rf"命令处理。
find ./ -type f -name "*.log" -mtime +30 -maxdepth 1 -exec rm -rf {
    
    } \;

2) -ok option

find search path [option] search content -ok command 2 {} \;

The function of the "-exec" option is basically the same, the difference is: the command "-exec" will be processed directly without asking; the command 2 of "-ok" will ask the user whether to do so before processing, and the command will be confirmed After that, it will be executed.

for example:

# 使用rm命令来删除find找到的结果,删除的动作最好确认一下
[wqf@b1i10 test]$ find ./ -perm 444 -ok rm -rf {
    
    } \;
< rm ... ./test_2.sh > ? y # 需要用户输入y,才会执行

3) -print option: Output the files matched by the find command to standard output

find search path [option] search content-print;

for example:

# 查找当前文件夹下文件名以".log"结尾的文件并输出到屏幕
[wqf@b1i10 rm_test]$ find ./ -name "*.log" -print
./data_val_520_13138333854_2022122200.log
./data_val_520_20221221_2022122117.log
./mon_tj_cal_1_2022120515.log
./mon_tj_cal_1_2022120510.log

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/128626620