且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

day10-linux查找find命令 介绍

更新时间:2022-10-01 09:50:58

上一次我们学习了关于linux的用户权限方面的管理:

    在使用linux系统,有时候需要对文件进行查找,而find命令比较全面:支持使用文件名、文件大小、所属组/主、是否为空、访问时间、修改时间等:

1下面在介绍find命令前,先介绍几个系统的查找命令:which\localte\whereis:

1.1: which:用来搜索命令(会在PATH里面来寻找路径)-所以只能查找命令类:

1
2
3
4
5
[root@localhost ~]# which cp             #用which来查找cp的路径:
alias cp='cp -i'
        /usr/bin/cp
[root@localhost ~]# echo $PATH           #会提前在生成的PATH路径里来查找:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

1.2 whereis:搜索命令,会列出配置文件的位置,输出很模糊:

[root@localhost ~]# whereis cp                

cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz 

1.3:mlocalte:用来搜索文件/目录和命令,但是搜索的结果比较模糊:

第一次使用时:会提示此命令存在:可以用mlocate来安装:

yum    install   -y   mlocate

工作原理:locate会在事先生成的一个数据库中去寻找(数据库用来统计所有命令和文件),据说会在每天凌晨4点来自动更新统计:

第一次使用则需要手动生成一下:用updatedb命令

用法:localte查找会列出所有相关的文件/目录和命令等:

1
2
3
4
5
6
[root@localhost ~]# locate ls            #会匹配到很多选项:
/boot/grub2/i386-pc/blscfg.mod
/boot/grub2/i386-pc/cbls.mod
/boot/grub2/i386-pc/command.lst
/boot/grub2/i386-pc/crypto.lst
/boot/grub2/i386-pc/fs.lst

2、linux系统的快捷键使用统计:

ctrl+l(小写L)           #清除屏幕内容,并将光标定位到第一行:

ctrl+d                     #退出当前终端,相当于exit或者logout.

ctrl+c                     #暂停当前输入,并跳到下一行:

ctrl+u                    #删除光标前的内容:

ctrl+d                    #删除光标后的内容(一个字符一个字符的删除):

ctrl+k                    #删除光标后的内容(全部删除)

ctrl+e                    #光标移动到行尾:end

ctrl+a                   #光标移动到行首:

3、find命令介绍:具体使用格式如下:

find   路径   条件    ====    find    /etc/     -type  d   

用法一:find基于名称搜索:可以支持通配符*。

1
2
3
4
[root@localhost ~]# find    /etc/    -name    "ssh*"       #支持名称来搜索:
/etc/ssh
/etc/ssh/ssh_config
/etc/ssh/ssh_host_rsa_key

用法二:find基于文件类型搜索:   find   /etc/   -type  类型

d(目录)       f(文件)     l(连接文件)      s(socket文件)   b(块设备文件)  c(串口设备文件)

1
2
3
[root@localhost ~]# find /etc/ -type d  -name ssh*    #查到是目录的并且是sshd.
/etc/ssh
/etc/selinux/targeted/active/modules/100/ssh

find支持 -o 选项,是或者的意思:多个参数之间要用 -o 选项来隔开:

1
2
3
4
5
[root@localhost ~]# find /etc/ -type d  -o -name ssh*  #目录或者是ssh文件都会被打印出来
/etc/
/etc/grub.d
/etc/pki
/etc/pki/rpm-gpg

并且: find /etc/ -type d  -name "ssh*"    不加-o则是并且的意思:

3.2 find的类型还有三个time:   atime     mtime      ctime

那么我们如何查看着三个time:    stat命令

1
2
3
4
5
6
7
8
[root@localhost ~]# stat 2.txt      #用stat后加文件名称来查看:
  文件:"2.txt"
  大小:0               块:0          IO 块:4096   普通空文件
设备:803h/2051d        Inode:33574980    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2017-10-28 01:58:56.757868054 +0800          #atime(aacces time)
modify:2017-10-28 01:58:56.757868054 +0800          #mtime(modify time)
change:2017-10-28 01:58:56.757868054 +0800          #ctime (change time)

atime:访问时间:读取和执行文件时修改:    #ls   -lu  filename   列出文件的atime

mtime:更改文件内容时间,如写入内容:       #ls   -l  filename   列出文件的mtime

ctime:更改inode时间,(包含属性、权限、文件大小、链接等): #ls  -lc filename  文件的ctime.

注意:文件内容的改动,会同时更改mtimectime,因为内容的内容会造成inode信息改变:

ctime改变,mtime不会改变,如修改了文件的权限,而不更改文件的名称:

用法一:查找出一天以内的文本: 参数: -mtime   -1

1
2
3
4
5
[root@localhost ~]# find / -type f -mtime -1 #只查找是文件类型的,文件内容更改时间在1天内的文件:
/proc/fb
/proc/fs/xfs/xqm
/proc/fs/xfs/xqmstat
/proc/bus/pci/00/00.0

用法二:查找出一天以外的文本: 参数: -mtime  +1

[root@localhost ~]# find / -type f -mtime +1  #只查找是文件类型的,文件内容更改时间在1天以外的文件:

/boot/grub2/device.map

/boot/grub2/i386-pc/gcry_rmd160.mod

/boot/grub2/i386-pc/acpi.mod

/boot/grub2/i386-pc/gcry_rsa.mod

ctime和atime的用法也是同mtime一样如此:

用法三:find还有有一个常用用法:查找硬链接:

分析:硬链接是根据inode号来区分,也就是我们要找到inode号相同的,即为链接文件:

1
2
3
[root@localhost ~]# find / -inum 33574980     #查找inode号相同的文件:
/root/2.txt
/tmp/2.txt.bak

用法四:find可以以分钟为来查找文件:mmin +1(一小时外)      mmin  -1(一小时内)

[root@localhost ~]# find /root -type f -mmin -60      #查找1小时以内的文件:

/root/2.txt

另外find支持直接打印和列出:有以下两种方法实现:输出是一样的。

方法一:通过xargs来实现:如下:

[root@localhost ~]# find /root -type f -mmin -60 |xargs ls -l

-rw-r--r-- 2 root root 6 10月 28 02:26 /root/2.txt

方法二:通过-exec来实现:如下:

[root@localhost ~]# find /root/ -type f -mmin -60 -exec ls -l {} \;

-rw-r--r-- 2 root root 6 10月 28 02:26 /root/2.txt

用法五:find支持直接打印后对文件进行操作,如修改名称和删除文件:如下:

直接查找类型是文件并且在150分钟内,直接重命名为*.bak.

1
2
3
4
5
6
7
[root@localhost ~]# find  /root/  -type f -mmin  -150 -exec mv {}  {}.bak \;
[root@localhost ~]# find /root/ -mmin -150
/root/
/root/yuanhh
/root/yuanhh/1.bak
/root/2.txt.bak
/root/1.txt.bak

 用法六:find支持以文件的大小来查找:支持k,M,G

[root@localhost ~]# find /root/ -type f -size -10k -exec ls -lh {} \;

-rw-r--r--. 1 root root 18 12月 29 2013 /root/.bash_logout

-rw-r--r--. 1 root root 176 12月 29 2013 /root/.bash_profile

-10k(则表示10k)  +10M(则表示10M)

 整理:find常用命令小整理:

-name:名称搜索,可支持通配符,如"yum*"等.

-mtime:以时间天为单位搜索,如“-5则表示5天内,+5则表示5天外”.

-type:类型搜索:文件类型:如“-type  d”表示目录,“! type d”表示取反,除目录之外的文件

-size:文件大小搜索:如“-size +10M”表示大于10M,“-10M”则表示小于10M的。

-mmin:以分钟为单位,如“-mmin  -60”表示60分钟以内的文件,"+60"表示60分钟以外的文件:

-inum:表示查找硬链接(后面跟inode号):如:

[root@localhost ~]# find /root/ -inum 661735

-perm:表示查找权限(后更要查找的权限):如下:

[root@localhost ~]# find /root/ -perm +0644

4.文件名的后缀名:

 linux下的文件/目录的后缀名可以自定义:

但是我们为了方便区分,会写成类似如下的后缀名称:

  .txt: 文本文档:

  .gz : 压缩文件:

  .conf : 配置文件:










本文转自 芬野 51CTO博客,原文链接:http://blog.51cto.com/yuanhaohao/2053223,如需转载请自行联系原作者