查找命令集:which/whereis/locate/find

本文详细介绍Linux下使用which、whereis、locate及find等命令进行文件查找的方法,包括各种选项和参数的使用,以及如何结合管道和xargs进行高效操作。

本文索引:

  • which
  • whereis
  • locate
  • 快捷键使用
  • atime/ctime/mtime
    • 场景说明
    • 总结
  • find
    • 常用选项
    • 常用参数
    • 配合管道和xargs
    • 多参数组合
  • 文件后缀

which

which 搜索可执行文件,必须在PATH环境变量目录中!!否则无法搜到!

[root@centos7 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

whereis

用来查找一个命令相关的文件(二进制文件、源文件和manual文件)

[root@centos7 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

locate

默认系统未安装 使用 yum install -y mlocate 安装;locate会搜索包含命令后续所给参数的所有文件!!

初次完装需要初始化数据库:updatedb

[root@centos7 ~]# yum install -y mlocate
[root@centos7 ~]#  updatedb
[root@centos7 ~]#  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
/boot/grub2/i386-pc/ls.mod
/boot/grub2/i386-pc/lsacpi.mod
/boot/grub2/i386-pc/lsapm.mod
/boot/grub2/i386-pc/lsmmap.mod
/boot/grub2/i386-pc/lspci.mod
.....

默认在每天凌晨4点会自动执行数据库的更新;

注意:在更新后新创建的文件、目录,使用locate命令将无法搜索到,这时手动执行 updatedb 后才能搜索到!

快捷键

  • ctrl + d 暂停执行的命令
  • ctrl + c 停止执行的命令
  • ctrl + u 删除光标前至开头的输入
  • ctrl + a 移动光标至行首
  • ctrl + e 移动光标至行尾

atime/mtime/ctime

stat命令可以显示文件、目录的三个时间

[root@centos7 ~]# stat 1.txt 
  文件:"1.txt"
  大小:0         	块:0          IO 块:4096   普通空文件
设备:803h/2051d	Inode:16808582    硬链接:2
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2017-09-20 22:26:23.383747117 +0800
最近更改:2017-09-20 22:26:23.383747117 +0800
最近改动:2017-09-20 22:26:33.616862124 +0800
创建时间:-

[root@centos7 test]# stat /test
  文件:"/test"
  大小:32        	块:0          IO 块:4096   目录
设备:803h/2051d	Inode:8812627     硬链接:2
权限:(0755/drwxr-xr-x)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:default_t:s0
最近访问:2017-10-21 10:28:30.074269196 +0800
最近更改:2017-10-21 10:28:29.311262182 +0800
最近改动:2017-10-21 10:28:29.311262182 +0800
创建时间:-
  • atime 访问时间
  • mtime 更改时间(改的是内容)
  • ctime 改动时间(权限、名称、inode号)

修改了内容,其mtime会变,同时ctime也会变(inode会随内容变化而变化),但atime不变

场景说明
  • cat命令只会更新atime
  • vim命令修改文件但不保存不更新三个time
  • vim命令修改文件并保存,三个time都更新
  • chmod修改权限更新ctime
  • touch命令三个time都更新(很特殊

使用echo追加数据至文件,不改变atime!因为文件未被查看,也没被打开,但是文件的内容、inode都变了,因此mtime和ctime变了!

总结

!!但凡更改了mtime,ctime会随之更改;但反过来更改ctime,mtime却不一定改变。(touch命令情况除外)

对文件内容进行修改,文件的inode号随之更改,因此ctime会改变;

而改变了文件的权限,文件的ctime会改变,但是只要文件的内容不变,mtime不会改变(touch除外)

find

一个功能强大的搜索命令

使用方法:find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

常用选项(option)
# 测试
[root@centos7 test]# tree
.
├── 1.txt
├── 2.txt
├── dir1
│   ├── test1.tar
│   ├── test.conf
│   ├── test.sh
│   ├── test.tar
│   └── test.txt
├── test1.tar
├── test.conf
├── test.sh
└── test.tar

1 directory, 11 files
  • -name 按文件名来查找(-iname 忽略大小写)
[root@centos7 test]# find /test/ -name "1.txt" 
/test/1.txt
  • -type 按文件类型来查找
[root@centos7 test]# find /test/ -type d 
/test/
/test/dir1
  • -size 按文件大小来查找
[root@centos7 test]# find /test/ -size -10k
/test/
/test/dir1
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -inum 按inode号来查找
[root@centos7 test]# ls -il
总用量 0
25721052 drwxr-xr-x. 2 root root 71 10月 21 13:39 dir1
 8812618 -rw-r--r--. 1 root root  0 10月 21 13:26 test1.tar
 8812616 -rw-r--r--. 1 root root  0 10月 21 13:26 test.conf
 8812617 -rw-r--r--. 1 root root  0 10月 21 13:26 test.sh
 8812623 -rw-r--r--. 1 root root  0 10月 21 13:26 test.tar
[root@centos7 test]# find /test/ -inum 8812618 
/test/test1.tar
  • -depth 大类

按目录深度来查找(具体有-maxdepth/-mindepth)

[root@centos7 test]# find /test/ -maxdepth 1 
/test/
/test/dir1
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -time 大类

按时间来查找(具体有-amin/cmin/mmin,atime/ctime/mtime)

# 以ctime为例查找
[root@centos7 test]# find /test/ -ctime -1 
/test/
/test/2.txt
/test/dir1
/test/dir1/test.txt
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/1.txt
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -executable 按是否可执行(有x权限)
[root@centos7 test]# find /test/ -executable 
/test/
/test/dir1
  • -newer 按是否比参考文件更新(修改时间里现在更近),对应的有-older参数
[root@centos7 test]# find /test/ -newer /root/test.cap 
/test/
/test/2.txt
/test/dir1
/test/dir1/test.txt
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/1.txt
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -user/group 按文件的所有者/所属组来查找
[root@centos7 test]# find /test/ -user root
/test/
/test/2.txt
/test/dir1
/test/dir1/test.txt
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/1.txt
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -perm MODE 按文件权限(数字形式)
[root@centos7 test]# find /test/ -perm 644 
/test/2.txt
/test/dir1/test.txt
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/1.txt
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
  • -uid/gid 按文件的uid/gid来查找
[root@centos7 test]# find /test/ -uid 0
/test/
/test/2.txt
/test/dir1
/test/dir1/test.txt
/test/dir1/test1.tar
/test/dir1/test.conf
/test/dir1/test.sh
/test/dir1/test.tar
/test/1.txt
/test/test.conf
/test/test.sh
/test/test1.tar
/test/test.tar
常用参数
  • -delete 对查找的内容进行删除操作
# 配置-name使用,删除找到的文件/目录
[root@centos7 test]# find /test/ -name "*.txt" -delete 
[root@centos7 test]# ls -l
总用量 0
drwxr-xr-x. 2 root root 71 10月 21 13:39 dir1
-rw-r--r--. 1 root root  0 10月 21 13:26 test1.tar
-rw-r--r--. 1 root root  0 10月 21 13:26 test.conf
-rw-r--r--. 1 root root  0 10月 21 13:26 test.sh
-rw-r--r--. 1 root root  0 10月 21 13:26 test.tar

  • -exec 对查找的内容执行后接命令
# 将大于100M的文件创建备份
# 注意命令最后的“\;”
[root@centos7 test]# find / -type f -size +100M -exec cp {} {}.bak \;
  • -print 一行显示打印的文件的全称
  • -print0 在null字符后打印出文件的全称(结果显示为一串)
# -print0打印时会将换行符也取消,显示内容的一部分会出现在下一个提示符行首
[root@centos7 test]# find /test -type d -print0
/test/test/dir1[root@centos7 test]# 

[root@centos7 test]# find /test -type d -print
/test
/test/dir1
配合管道和xargs
它会将目录的子目录下的文件也都显示出来!!
[root@centos7 test]# find /test -type f | xargs ls -l 
-rw-r--r--. 2 root root 8 10月 21 10:22 /test/1.txt
-rw-r--r--. 2 root root 8 10月 21 10:22 /test/2.txt
-rw-r--r--. 1 root root 0 10月 21 10:59 /test/dir1/test.txt
多个参数配合使用
  • -a 同时满足多个条件
# 多个条件限定查找
[root@centos7 test]# find /root -type d -a -name "test" | xargs ls -ld 
drwxr-xr-x. 3 root root 162 10月 19 20:40 /root/test
  • -o 满足其中一个要求即可
[root@centos7 test]# find /root -name "tmp" -o -name "test" | xargs ls -ld 
drwxr-xr-x. 3 root root 162 10月 19 20:40 /root/test
drwxr-xr-x. 2 root root  73 10月  6 20:54 /root/tmp

文件后缀

Linux下的文件后缀没有什么实际意义,给文件添加后缀的目的是为了方便区分!!

使用stat命令可以查看文件类型!

# 我将一个普通的文本文件改名为1.tar(常规意义上的打包文件),其实质还是普通的文本文件!
[root@centos7 test]# stat 1.tar 
  文件:"1.tar"
  大小:12        	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:8812615     硬链接:2
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:default_t:s0
最近访问:2017-10-21 11:14:20.514251393 +0800
最近更改:2017-10-21 11:13:34.243028671 +0800
最近改动:2017-10-21 11:30:45.301244474 +0800
创建时间:-

转载于:https://my.oschina.net/LuCastiel/blog/1554287

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值