find

文件查找-find

find命令的基本语法如下

例子:

[root@localhost ~]# find /etc/ -name '*.sh'

/etc/profile.d/colorgrep.sh

/etc/profile.d/colorls.sh

/etc/profile.d/which2.sh

/etc/profile.d/less.sh

/etc/profile.d/256term.sh

/etc/profile.d/lang.sh

/etc/profile.d/vim.sh

/etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh

/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh


根据文件名查找文件

//创建文件

touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

//查找/etc目录下包含ifcfg-eth0名称的文件

[root@zls ~]# find /etc -name "ifcfg-eth1"

//-i 忽略大小写

[root@zls ~]# find /etc -iname "ifcfg-eth1"

//查找/etc目录下包含ifcfg-eth名称所有文件

[root@zls ~]# find /etc/ -name "ifcfg-eth*"

[root@zls ~]# find /etc -iname "ifcfg-eth*"

//查找包含eth的文件

[root@localhost opt]# find /opt/ -name '*eth*'

[root@localhost opt]# find /opt/ -iname '*eth*'

find /root/dir1 ! (-name 'file5' -o -name 'file9' )

根据文件的大小查找

-size n[cwbkMG]

       `b'  block

       `c'  bytes 字节

       `w'  words 单词

       `k'  kb

       `M'  MB

       `G'  GB

# 查找大于5M的文件

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

/etc/udev/hwdb.bin

# 查找等于5M的文件

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

# 查找小于5M的文件

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

## 动作,查看

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

## 动作,删除

[root@localhost ~]# find /tmp/ -size +5M -delete

## 集合name

[root@localhost ~]# find /etc/ -size -5M -name '*.sh'

## 需求:在/etc 找到 .sh 和 .conf 结尾的 小于5M的文件

[root@localhost ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.db'

## 需求:在/etc/ 找到 大于3M 小于5M 名字以.sh结尾和.conf结尾

[root@localhost ~]# find /etc/ \( -size +3M -a -size -6M \) -a \( -name '*.sh' -o -name

'*.conf' \)

## 需求:在/etc 目录下找到小于5M 并且 文件名不是以 .sh 结尾 或者 不是以 .db结尾

[root@localhost ~]# find /etc/ -size -5M ! \( -name '*.sh' -o -name '*.db' \)

根据文件类型查找

f:文件

d:目录

l:软连接

s:socket

p:管道文件

b:块设备

c:字符设备

find / -type f

find / -type d

find / -type b

find / -type f -ls

find / -type f|head -5|xargs ls -l

根据日期查找

-mtime

## 找七天之前的文件,(不包含今天)

[root@localhost ~]# find /opt/ -mtime +7 -name '*.txt'

## 找最近七天的文件

[root@localhost ~]# find /opt/ -mtime -7 -name '*.txt'

## 找第七天的(不包含今天)

[root@localhost ~]# find /opt/ -mtime 7 -name '*.txt'

## 企业需求:只保留N天的备份,其余的都删除

[root@localhost ~]# find /opt/ ! -mtime -7 -name '*.txt' -delete

[root@localhost ~]# find /opt/ ! -mtime -30 -name '*.txt' -delete

条件语句

-a:and 和,并且

-o:or 或者

!:取反

动作

-ls

-delete

-exec

-print:打印出查找的内容,find默认就会打印

-ls:查看找出的文件相信信息

[root@localhost ~]# find /opt/ ! -perm /222 -ls

-delete

[root@localhost opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr

-ok

语法: -ok \;

-exec

语法: -exec \;

## 拷贝找到的文件到/tmp下

[root@localhost opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/

[root@localhost opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ \;

[root@localhost opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/

[root@localhost opt]# find /opt/ -name '*.txt' -ok cp {} /tmp/ \;

< cp ... /opt/2020-04-01_file.txt > ? y

< cp ... /opt/2020-04-02_file.txt > ? y

< cp ... /opt/2020-04-03_file.txt > ? y

< cp ... /opt/2020-04-04_file.txt > ? y

< cp ... /opt/2020-04-05_file.txt > ? y

< cp ... /opt/2020-04-06_file.txt > ? y

< cp ... /opt/2020-04-07_file.txt > ? y

< cp ... /opt/2020-04-08_file.txt > ? y

< cp ... /opt/2020-04-09_file.txt > ? y

< cp ... /opt/2020-04-10_file.txt > ? y

< cp ... /opt/2020-04-11_file.txt > ? y

< cp ... /opt/2020-04-12_file.txt > ? y

< cp ... /opt/2020-04-13_file.txt > ? y

< cp ... /opt/2020-04-14_file.txt > ? y

< cp ... /opt/2020-04-15_file.txt > ? y

< cp ... /opt/2020-04-16_file.txt > ? y

< cp ... /opt/zls.txt > ? y

## find 结合xargs

#拷贝

find / -type f |xargs cp -t /tmp

#查看

find / -type f |xargs ls -l

#替换

find / -type f |xargs sed -i 's###g'

#移动

find / -type f |xargs mv -t /tmp

find / -type f |xargs rm -fr

find的动作很少用

根据时间查找

-atime:access

-ctime:change

-mtime:modify

-7:查找最近7天的文件

+7:查找7天之前的文件,不包含今天

7:查找第7天的文件,不包含今天

find / -mtime +7

find / -mtime 7

find / -mtime -7

根据用户查找文件

-user:查找属主是X个用户

-group:查找属组是X个组

-nouser:查找没有用户

-nogroup:查找没有组

[root@localhost opt]# find ./ -user zls|xargs ls -l

-rw-r--r--. 1 zls qiandao 0 Apr 16 00:36 ./file1

-rw-r--r--. 1 zls zls   0 Apr 16 00:36 ./file3

[root@localhost opt]# find ./ -user zls -group qiandao

./file1

[root@localhost opt]# find ./ -user zls -o -group qiandao

./file1

./file3

./file4

根据深度查找

-maxdepth level

[root@localhost ~]# find /etc/ -maxdepth 2 -type f -name '*.conf'

根据文件权限查找

-perm

## 精确查找

[root@localhost opt]# find /opt/ -perm 644

## 包含指定权限的文件

root@localhost opt]# find /opt/ -perm -222

-222:and

/222:or

属主 属组 其它用户

- 1   2   3

x  w   wx

r-x rwx  rwx

/ 1   2   3

x  w   wx

r-x rwx  rwx

rw  wx  rwx

r-x r   r





练习题:

(1)建议先创建快照

(2)有可能存在命令正确,但是查找不到文件的情况,是因为不存在相关条件的文件

(3)如果存在命令正确,但是查找不到文件的情况,则先创建相关的文件、目录、用户、组,设置好对应的权限,再进行查找

(4)如果是以时间为查找条件的题,则使用date命令修改linux系统时间为几天以前,创建好相关文件后再改回当前时间 (date命令使用可参考https://blog.csdn.net/stalin_/article/details/80436568)

01.找出/tmp目录下,属主不是root,且文件名不以f开头的文件

[root@oldboy ~]# find /tmp/ -type f ! -user root ! -name "f*"

/tmp/qzg

[root@oldboy ~]# find /tmp/ -type f ! \( -user root -o -name "f*" \)

/tmp/qzg

02.查找/etc/目录下,所有.conf后缀的文件

[root@oldboy ~]# find /etc/ -type f  -name "*.conf"

03.查找/var目录下属主为root,且属组为mail的所有文件

[root@oldboy ~]# find /var/ -type f -user root -group mail |xargs ls -l

-rw------- 1 root mail 2073310 Apr 10 14:40 /var/spool/mail/root

04.查找/var目录下7天以前,同时属主不为root,也不是postfix的文件

[root@oldboy ~]# find /var/ -type f -mtime +7 ! -user root ! -name "postfix"|xargs ls -l

05.查找/etc目录下大于1M且类型为普通文件的所有文件

[root@oldboy ~]# find /etc/ -type f -size +1M |xargs ls -l

06.查找/etc目录下所有用户都没有写权限的文件

属主  属组  其他用户

[root@oldboy ~]# find /etc/  -type f ! -perm 222|xargs ls -l

07.查找/目录下最后创建时间是3天前,后缀是*.log的文件

[root@oldboy ~]# find / -type f -mtime +3 -name "*.log"|xargs ls -l

08.查找/目录下文件名包含txt的文件

错误    *.txt    txt 

[root@oldboy /tmp]# find /tmp/ -type f -name "*txt*"

/tmp/txt

/tmp/1.txt

/tmp/1txt1

/tmp/txt.1

09.查找/目录下属主是oldboy并且属组是oldboy的文件

[root@oldboy /tmp]# find / -type f -user oldboy -group oldboy|xargs ls -l

10.查找/目录下属主是oldboy但是属组不是oldboy的文件

[root@oldboy /tmp]# find / -type f -user oldboy  ! -group oldboy|xargs ls -l

11.查找/目录下属主是oldboy或者属主是oldgirl的文件

[root@oldboy /tmp]# find / -type f -user oldboy -a -user oldgirl|xargs ls -l

12.查找/tmp目录下属主既不是oldboy,也不是oldgirl的文件

[root@oldboy /tmp]# find /tmp/ -type f ! -user oldboy ! -user oldgirl|xargs ls -l

-rw-r--r-- 1 root root 513 Apr 10 14:35 /tmp/fstab1.out

-rw-r--r-- 1 www  www  513 Apr  8 14:44 /tmp/qzg

-rw-r--r-- 1 root root  0 Apr 10 14:52 /tmp/txt

-rw-r--r-- 1 root root  0 Apr 10 14:52 /tmp/txt.1

13.查找/var/log目录下7天以前的文件

find /var/log  -type f -mtime +7

14.查找/home目录下,类型是目录的,并且属主是oldboy的目录

[root@oldboy /tmp]# find /home/ -type d -user oldboy|xargs ls -ld

15.查找/var/log下大于100kb且以log结尾的所有文件

错误    1.log    1log

[root@oldboy /tmp]# find /var/log/ -type f -size +100k -name "*log"|xargs ls -l

16.查找tmp目录下所属组group1,所属主user1的目录

[root@oldboy /tmp]# find /tmp/ -type d -user user1 -group group1|xargs ls -ld

drwxr-xr-x 2 user1 group1 6 Apr 10 14:54 /tmp/2txt2

17.同时查找根目录下名为1.txt,2.txt的文件和名字带a的目录

[root@oldboy /tmp]# find /tmp/ -type f -name "[1-2].txt" -o -type d -name "*a*"

[root@oldboy /tmp]# find /tmp/  \( -type f -name '1.txt' -o -name '2.txt'  \) -o  \( -type d -name '*a*'  \)

[root@oldboy /tmp]# find /tmp/ \( -name '1.txt' -o -name '2.txt' -type f \) -o \( -name '*a*' -type d \)

[root@oldboy /tmp]# find /tmp/ -type f -name 1.txt -o -name 2.txt -o \( -type d -name '*a*' \)

[root@oldboy /tmp]# find /tmp/ \( -name '1.txt' -o -name '2.txt' -o -type d -name '*a*' \)

18.查找/tmp目录下所有文件并删除

[root@oldboy /tmp]# find /tmp/ -type f -delete

[root@oldboy /tmp]# find /tmp/ -type f |xargs rm -rf

[root@oldboy /tmp]# find /tmp/ -type f -exec rm -rf {} \;

19.查找根目录下所有的隐藏目录

[root@oldboy /tmp]# find / -type d -name ".*"|xargs ls -ld

20.查找根目录下以rpm结尾的所有文件

find / -type f -name “*rpm”

21.查找/data/bak目录下15天以前的文件删除(自行修改系统时间模拟相关环境)

[root@Qzg ~]# find /var/log/ -type f -mtime  +15 -delete

22.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除

讲过

###想想办法,做之前先看看曾导的博客

23.查找/app/logs下7天以前的log文件并删除(至少三种方法)

[root@Qzg ~]# find /app/log/ -type f -mtime  +7  -delete

[root@Qzg ~]# find /var/log/ -type f -mtime  +15 |xargs rm -rf

[root@Qzg ~]# find /var/log/ -type f -mtime  +15 -exec rm -rf {} \;

24.将/etc目录下大于100k的文件移动至/tmp下(至少三种方法)

[root@oldboy ~]# find /etc/ -type f  -size +100k |xargs -i mv {} /tmp/

[root@oldboy ~]# find /etc/ -type f  -size +100k |xargs mv -t /tmp/

[root@oldboy ~]# find /etc/ -type f  -size +100k -exec \mv {} /tmp/ \;

25.文件权限为r-x------, 请找出在/oldboy目录下面的所有此权限目录,并复制到/tmp目录(至少三种方法)

500

[root@oldboy ~]# find /oldboy/ -type f  -perm 500 |xargs -i  cp {} /tmp/

[root@oldboy ~]# find /oldboy/ -type f  -perm 500 |xargs cp -t /tmp/

[root@oldboy ~]# find /oldboy/ -type f  -perm 500 -exec cp -a {} /tmp/ \;

[root@oldboy ~]# cp -a `find /oldboy/ -type f  -perm 500`  /tmp/


1.查找/tmp目录下,属主不是root,且文件名不是以f开头的文件

[root@oldboy /tmp]# find /tmp/ -type f ! -user root  -a ! -name "f*"|xargs ls -l

[root@oldboy /tmp]# find /tmp/ -type f ! \( -user root  -o  -name "f*" \)|xargs ls -l

2.查找/var目录下属主为root,且属组为mail的所有文件

[root@oldboy ~]# find /var/ -type f -user root -group mail |xargs ls -l

-rw------- 1 root mail 457194 Apr 13 14:35 /var/spool/mail/root

3.查找/var目录下不属于root、oldboy、zls组的所有文件

[root@oldboy ~]# find /var/ -type f ! \( -group root -o -group oldboy -o group zls \)

4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

-mtime +7    7天以前   

[root@oldboy ~]# find /var/ -mtime -7 \( ! -user root -o  ! -name 'postfix' \)|xargs ls -ld

5.查找/etc/下所有大于1M且类型为普通文件的所有文件

[root@oldboy ~]# find /etc/ -type f -size +1M

6.将/etc中的所有目录(仅目录)复制到/tmp下,目录结构不变

#方法一

[root@oldboy ~]# find /etc/ -type d -exec mkdir  /tmp/{} \;

#方法二

[root@oldboy ~]# find /tmp/etc/ ! -type d -delete

7.将/etc目录复制到 /var/tmp,/var/tmp/etc的所有目录权限为777,/var/tmp/etc/目录中所有文件权限为666

[root@oldboy ~]# find /tmp/etc/ -type d |xargs chmod -R 777

[root@oldboy ~]# find /tmp/etc/ -type f |xargs chmod -R 666

9.创建touch file{1..10}10个文件,保留file9,其他一次全部删除

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 -rvf {} \;

find /root/dir1 ! (-name 'file5' -o -name 'file9' ) -exec rm -vf {} \;

###第二部分(find+之前的知识点回顾)

01.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变

02.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666

03.解释如下每条命令含义

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 -rvf {} \;

find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;

04.将/data 目录下的修改时间是7 天以前,并且大于100k 的文件复制到/tmp 目录下。(不低于三种方法)

find /data -type f -mtime +7 -size +100k |xargs cp -t /tmp

find /data -type f -mtime +7 -size +100k -exec cp {} /tmp  \;

cp  $(find /data -type f -mtime +7 -size +100k)  /tmp

find /data -type f -mtime +7 -size +100k |xargs -i cp  {} /tmp

05.把/data 目录复制到 /tmp目录下并改名为data_20190110 (20190110为当天时间)?

[root@oldboy ~]# cp -r /data/ /tmp/data_`date +%Y%m%d`

[root@oldboy ~]# mkdir /data

[root@oldboy ~]# cp -r /data/ /tmp/data_$(date +%Y%m%d)

06.把mysql-5.6.34.tar.gz解压到/application目录下?

-C

07.linux下面ping www.baidu.com 出现unknown host 错误如何排查?

#1.查看一下你的ip地址在不在(是否存在)

#2.ping dns

#3.ping 网关

08.CentOS默认的Shell是?

bash shell

09.邮件系统使用的两个主要协议是()和(),前者用来发送邮件,后者用来接收邮件,端口分别是多少()

SMTP    POP    25    110

10.某文件的权限为:d-rw-r--r--,用数值形式表示该权限,则该八进制数为(0644),该文件的属性为(目录)?

11.请简单说出用户管理的相关命令及用途?

useradd    userdel  usermod

groupadd  groupdel  groupmod

chown  chmod    chgrp

12.在/etc/passwd文件中,以冒号分割字段,截取第三列包含数字5的行?

[root@oldboy ~]# awk -F "[:]+" '$3~/5/' /etc/passwd

sync:x:5:0:sync:/sbin:/bin/sync

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

13.查找最后修改时间是3天前,后缀是*.log的文件并删除?

find / -mtime +3 -name '*.log' -delete

find / -mtime +3 -name '*.log' | xargs  rm -rf

find / -mtime +3 -name '*.log' -exec rm {} \;

14.使用命令调换 passwd 文件里 root 位置和/bin/bash 位置? 即将所有的第一列和最后一列位置调换?

[root@oldboy ~]# awk -F "[:]+" '{print $NF":"$2":"$3":"$4":"$5":"$6":"$1}' /etc/passwd

15.添加一个用户oldboy,并指定属于sa组,要求组ID为801,uid为808,并且不建立家目录及禁止其登陆。

groupadd -g 801 sa

useradd -u 808 -g sa -M -s /xxxx  oldboy

16.把/oldboy 目录中以.log 结尾的修改时间是7 天之前的文件中的oldboy 替换为oldgirl

[root@oldboy ~]# find /oldboy/ -type f -mtime +7  -name "*.log" |xargs sed -i 's#oldboy#oldgirl#g'

17.设置别名要求输入net 的时候就显示/etc/sysconfig/network-scripts/ifcfg-eth0的内容并永久生效?

18.将test.txt文件中的内容发送到你邮箱中,主题为"表白小姐姐"?

[root@oldboy ~]# mail -s "表白小姐姐" xxxxxx

19.一个目录中有很多文件(ll查看时好多屏),想最快速度查看到最近更新的文件?

[root@oldboy /etc]# ll -rt

20.授权oldboy目录及其子目录755权限给出命令?

chmod -R 755 oldboy

21.把oldboy目录及其子的属主属组改为oldboy请给出命令。

chown -R oldboy.oldboy oldboy

22. 说明下面这几个文件的作用:/etc/hostname、/etc/resolv.conf、/etc/hosts

主机名配置文件      DNS配置文件        ip与域名映射配置文件

23.写出redhat 中,配置网卡及dns 的配置文件是什么?并说明区别?

/etc/sysconfig/network-scripts/ifcfg-eth0

配置方法:DNS1=xxx.xxx.xxx.xxx  DNS2=xxx.xxx.xxx.xxx

此配置文件配置好DNS后是需要重启网卡配置文件才能生效。

/etc/resolv.conf

配置方法:nameserver  xxx.xxx.xxx.xxx    nameserver  xxx.xxx.xxx.xxx

此配置配置完成后是实时生效的

网卡配置文件中的DNS优先于/etc/resolv.conf

24.虚拟网络分别有哪些模式?

桥接  nat  仅主机模式

25.命令行临时修改主机名分别写出6和7系列的系统

hostname  新的主机名

hostnamectl  set-hostname  新的主机名

26.调试系统服务时,希望能实时查看系统日志/var/log/messages的更新,如何做?

tailf  /var/log/messages

tail -f /var/log/messages

tail -F /var/log/messages  #当文件不存在时,会不断的重试

27.sed命令的-n -r 参数均表示什么意思

-n  取消默认输出

-r  识别扩展正则

28.简述磁盘空间不够,可能造成这个情况的原因,以及处理方法?

答案信息:

磁盘不足原因01:

磁盘空间 inode被占满  利用df -i可以查看利用率

一般是小文件过多所造成

处理方法:删除大量没有的小文件

磁盘不足原因02:

磁盘空间 block被占满  利用df -h可以查看利用率

一般是大文件过多所造成

处理方法:利用du -sh命令找出无用大文件,进行删除

磁盘不足原因03:

大文件数据被进程调用,需要重启服务才能释放磁盘空间

利用 losf|grep "delete"  命令信息进行查看

处理方法:重启服务释放空间,建议尽量不要删除被进程占用的文件,而是尽量采用清空方式

29.寻找名称为mytest开头的文件信息,在/oldboy目录下进行查找,只找一层目录

[root@oldboy /etc]# find /oldboy/  -maxdepth 1  -type f  -name  "mytest*"

/oldboy/mytest01

你可能感兴趣的:(find)