File Find -find command

The basic syntax of the find command is as follows:

command path Options expression action
find [path...] [options] [expression] [action]
Seek area Sister paper 18-25 years old approximately?
(1) Search by name

1. Find by name

[root@yinwucheng ~]# find ./ -name "*eth0"

2. Find the name (not case-sensitive)

[root@yinwucheng ~]# find ./ -iname "*eth0"
(2) Find size by file size

1. Find the files larger than 5M etc / the / directory

[root@yinwucheng ~]# find /etc/ -size +5M

2. Find the file is smaller than 5M's / etc / directory

[root@yinwucheng ~]# find /etc/ -size -5M

5M equal to file under 3. Find the / etc / directory

[root@yinwucheng ~]# find /etc/ -size 5M
(3) Find -type by file type
f    文件
d    目录
s    socket套接字文件
l    链接文件
c    字符设备
b    块设备

1. Find the current directory is the type of file, and the name associated with eth0 are listed

[root@yinwucheng~]# find ./ -type f -iname "*eth0" |xargs ls -l

2. Find the / etc / directory under the file type, size is greater than 5M, whose name ends in .bin

[root@yinwucheng ~]# find /etc/ -type f -size +5M -name "*.bin"
3. Find the / etc / directory under the file type, the name is the end of .repo
[root@yinwucheng ~]# find /etc/ -type f -name ".repo"
4. Find / dev is the block type in the device, and the name of the beginning sda
[root@yinwucheng ~]# find /dev/ -type b -name "sda*" |xargs ls -l
5. Find / dev is a character type in the device, and the name is the beginning of tty
[root@yinwucheng ~]# find /dev/ -type c -name "tty*"

(4) Preparing the environment (shell in order to create the file 1-31)

[root@yinwucheng ~]# for i in {1..31}; do date -s"2019/08/$i" ; touch file-$i ; done

1. Day 7

[root@yinwucheng ~]# find ./ -type f -mtime 7

Content 2.7 days before filtering out, and then delete it. Preserve the contents of the last 7 days

[root@yinwucheng ~]# find ./ -type f -mtime +7 -name "file-*" |xargs rm -rf

3. The last seven days of content will be filtered out

[root@yinwucheng ~]# find ./ -type f -mtime -7 -name "file-*"

4. Local files remain the last seven days of backup files, backup server to retain three months of the backup file (the actual use of the program)

find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +180 -delete
(5) by users and groups to find -user -group -nouser -nogroup
查找属主是jack
[root@yinwucheng ~]# find /home -user jack

查找属组是admin
[root@yinwucheng ~]# find /home -group admin

查找属主是jacky, 属组是jack
[root@yinwucheng ~]# find /home/ -type d -user jacky -group jack

查找没有属主
[root@yinwucheng ~]# find /home -nouser

查找没有属组
[root@yinwucheng ~]# find /home -nogroup

查找没有属主或属组
[root@yinwucheng ~]# find /home -nouser -nogroup
Processing operation after 6.find find?

After finding a file, the file needs to be how to deal with, find the default action is -print

action meaning
-print Find the print content (default) --- ignore
-Ls Long way to find the format of the print content --- ignore | xargs ls -l
-delete Find the file to delete (delete the directory, only delete empty directories) --- ignore xargs | rm -f
-ok Back with self-defined shell commands (you will be prompted whether the operation) --- ignore
-exec Back with self-defined shell commands (standard written -exec \;) | xargs
[root@yinwucheng ~]# time find ./ -type f -name "file*" -exec rm -f {} \;
real 0m6.585s
user 0m3.617s
sys  0m3.532s
[root@yinwucheng ~]# time find ./ -type f -name "file*" | xargs rm -f
real 0m0.152s
user 0m0.016s
sys  0m0.146s
Find / var / log / type of file, and the name is ending .log, and seven days ago, and then delete
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \;
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -delete
[root@yinwucheng ~]# #find /var/log/ -type f -name "*.log" -mtime +7 | xargs rm -f
3. Remember what the file is, but it is not clear what the file name, path do not know where, how do?
  • find a query file
  • grep filter content

    The results of the query ifnd file as a parameter grep

[root@yinwucheng ~]# find /etc/ -type f | xargs grep "log_group" --color=auto
/etc/audit/auditd.conf:log_group = root
Logical operators 4.find
symbol effect
-a versus
-O or
-not | ! non-

1. Find the current directory, is not the root of all master files

[root@yinwucheng ~]# find /home/ ! -user root -ls
[root@yinwucheng ~]# find /home/ -not -user root -ls   使用较少

2. Find the current directory, it is the main part of Jack, and a file size greater than 1k

[root@yinwucheng ~]# find /home/ -type f -a -user jacky -a -size +1k

3. Find the common file owner home directory is the end or in the xml lisi

[root@yinwucheng ~]# find /home -type f -a \ ( -user lisi -o -name '*.xml' \)

5.find practice

1. Find / var directory under, the owner is not the root, and the file name does not begin with file f
[root @ yinwucheng ~] # the Find / var / The -type f! -User root -a! -Name "f *"

2. Find the / var directory subordinate to the main root, and all belong to the group for the mail file
[root @ yinwucheng ~] # find / var / -type f -user root -a -group mail

3. Find / does not belong to root under var directory, all files of lp
[root @ yinwucheng ~] # find / var / -type f! -User root -a! -User lp

4. Find the file generated / var directory under the last week, while the owner is not the root, nor postfix file
[root @ yinwucheng ~] # find / var / -type f -mtime -7 -user root!! - name "postfix"

5. Find all files under the / etc greater than 1M and common file type
[root @ yinwucheng ~] # find / etc / -type f -size + 1M

6. All directory (only) / etc / copied to / lower tmp, directory structure intact
[root @ yinwucheng ~] # find / etc / -type d -exec mkdir -p / tmp / {} \;

7. Copy the / etc directory to / var / tmp /, / var / tmp / etc directory permissions for all the 777 / var / tmp / etc directory permissions for all files 666
[yinwucheng the root @ ~] # CP / etc / / var / tmp / -rp
[yinwucheng the root @ ~] # Find / var / tmp / etc / D -type -exec the chmod 777 {} \;
[yinwucheng the root @ ~] # Find / var / tmp / etc / -type F - exec chmod 666 {} \;

8. Reserved / var / log / log the last seven days file, delete all other
[root @ yinwucheng ~] # find / var / log / -type f -mtime +7 -exec rm -f {} \;

Summarize today

1.find 查找文件
文件名称
文件大小
文件类型
修改时间
用户用户组
2.find处理动作
-print 忽略 默认送
-ls 长格式显示,不能加参数,所以忽略
-delete 删除文件,删除目录必须确保目录为空
-ok 执行command命令,但会提示,忽略
-exec 执行command命令
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
[root@oldboyedu ~]# find ./ -type f -name "file*" ! -
name "file9" -exec rm -f {} \;
10.解释如下每条命令含义
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) - ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -vf {} \;
find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;

Guess you like

Origin www.cnblogs.com/yinwu/p/11412043.html