find 命令 (搜索文件)
搜索文件命令还有 which,whereis(不常用),locate
组合键解释:
Ctrl+a : 光标移动到命令最前面;
Ctrl+e: 光标移到命令最后面;
Ctrl+L: 清屏
Ctrl+u: 删除光标前面所有的命令
概念: find 命令 就是用于搜索文件
命令格式: find 路径 参数
参数:
-atime +n 和 - n : 表示访问或执行时间大于或小于 n 天的文件
-ctime +n 和 - n : 表示写入更改 iNode 属性 (如更改所有者权限或者链接) 的时间大于或小于 n 天的文件
-mtime +n 和 - n : 表示写入时间大于或小于 n 天的文件, 该参数用的最多
find 使用到的命令:
-type 通过文件类型查找文件 例: -type f 以文件类型找到
-mtime 表示几天内 或者几天前的文件, mtime -1 一天内; mtime +10 10 天前
-size 表示文件字节选择列出来的文件 例:-size +10k 大于 10k 的文件
-o 和, 例:-type f -o -mtime -1 查看以文件类型和小于一天的文件
-exec 推动后面的命令, 例: find /root/ -type f -mmin -120 -exec ls -l {} \; 把前面的命令 以 ls -l 列出来
find 命令的使用, 例: 如果知道在哪个目录下, 然后知道文件名字, 如何查找呢? find /etc/ -name "sshd_config"
- [root@zhangzhen-01 ~]# find /etc/ -name "sshd_config" #在这里 find 是搜索,-name 空格后面双引号跟文件全面查找如下
- /etc/ssh/sshd_config
如果你只知道大概文件名, 可以用模糊查找 find /etc/ -name "sshd"
- [root@zhangzhen-01 ~]# find /etc/ -name "sshd*" #在这里, 前面格式一样, 双引号里面 sshd 后面跟的是号, 表示通配符, 以 sshd 开头的文件全部列出来
- /etc/ssh/sshd_config
- /etc/systemd/system/multi-user.target.wants/sshd.service
- /etc/sysconfig/sshd
- /etc/pam.d/sshd
在这里我们可以用 -type , 表示通过文件类型查找文件
概念:-type filetype 表示通过文件类型查找文件如下几种文件类型
d 表示该文件为目录;
f 表示该文件为普通文件
l 表示该文件为链接文件 (如软链接 硬链接)
b 表示该文件为块设备
c 表示该文件为串行端口设备文件
s 表示该文件为套接字文件
该命令格式如下: find /etc/ -type d -name "sshd*" #查找文件为目录的 sshd * 所有文件
stat 获取一个文件或者目录的信息 (如文件的大小, 什么样的文件或者目录, 包括权限及 uid, 还有最近访问(atime access time), 最近更改的时间(mtime modify time) 最近改动的时间 (ctime change ctime change) 和创建的时间)
概念: 这个 stat 命令主要就是看最近访问 (atime) 最近更改 (mtime) 和最近改动的时间 (ctime) 创建时间的
使用命令如下:
[root@zhangzhen-01 ~]# stat dior
文件:"dior"
大小: 32 块: 0 IO 块: 4096 目录
设备: 803h/2051d Inode:499801 硬链接: 2
权限:(0755/drwxr-xr-x) Uid:( 0/ root) Gid:( 1001/ user1)
环境: unconfined_u:object_r:admin_home_t:s0
最近访问: 2018-03-29 00:31:52.739168672 +0800
最近更改: 2018-03-28 22:44:38.990222665 +0800
最近改动: 2018-03-29 00:31:40.627056046 +0800
创建时间:-
做个示例, 搜索小于 1 天内, 在 / etc 目录下, 最近访问的块设备有哪些
- [root@zhangzhen-01 ~]# find /etc/ -type b -atime -1
- [root@zhangzhen-01 ~]# find /etc/ -type f -atime -1
- /etc/resolv.conf
- /etc/pki/tls/openssl.cnf
- /etc/pki/nss-legacy/nss-rhel7.config
- /etc/rpm/macros.dist
- /etc/yum.repos.d/CentOS-Base.repo
- /etc/yum.repos.d/CentOS-CR.repo
- /etc/yum.repos.d/CentOS-Debuginfo.repo
- /etc/yum.repos.d/CentOS-Media.repo
在这里, find 表示搜索, 空格 后面跟需要搜索的文件, 空格 -type(表示通过文件类型查找文件) 空格 后面 b(表示块设备)空格 -atime(最近访问)-1 (小于一天, 可以用 + 多少天以前的,- 表示多少天以内的)
-o 表示或者, 使用方法如下:
[root@zhangzhen-01 ~]# find /etc/ -type f -o -mtime -1 -0 -name "*.com"
如何搜索硬链接, 如何搜索 iNode 文件都有什么
- [root@zhangzhen-01 ~]# ln anaconda-ks.cfg /tmp/23.txt.heh #做个硬链接到 / tmp 下创建个 23.txt.heh 的文件
- [root@zhangzhen-01 ~]# ls -l anaconda-ks.cfg #下面的内容就是硬链接的文件
-rw-------. 2 root root 6901 3 月 27 22:40 anaconda-ks.cfg
- [root@zhangzhen-01 ~]# ls -i /tmp/23.txt.heh #查看刚硬链接的 iNode 号
- 33574978 /tmp/23.txt.heh
- [root@zhangzhen-01 ~]# find / -inum 33574978 #查看 iNode 号的文件都有哪些
- /root/anaconda-ks.cfg
- /tmp/23.txt.heh
如何用 find 查看 2 小时之内的文件
- [root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 **#-mmin 后面跟分钟数
- /root/.bash_history
- /root/dior/1.txt
如果利用 find 把查看的文件直接 ls -l 出来, 格式如下:
root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 -exec ls -l {} \;
-rw-------. 1 root root 9759 3 月 31 00:22 /root/.bash_history
-rw-r--r--. 1 root root 15 3 月 30 23:39 /root/dior/1.txt
注:-exec 也是 find 的一种格式, 后面跟你要对文件进行的命令, {} 花括号表示 ls -l 出来的内容, \; 反斜杠 + 分号只是让他执行这个命令, 推动出来
如何利用 find 给之前创建的文件批量改后缀名呢, 命令格式如下: -mmin
- [root@zhangzhen-01 ~]# find /root/ -type f -mmin -300
- /root/.bash_history
- /root/dior/1.txt
- [root@zhangzhen-01 ~]# find /root/ -type f -mmin -120 -exec mv {} {}.bak \;
- [root@zhangzhen-01 ~]# find /root/ -type f -mmin -300
- /root/dior/1.txt.bak
- /root/.bash_history.bak
注: 第二个花括号表示所每一个文件
如何用 find 把大于 10K 的文件列出来 -size +10k
[root@zhangzhen-01 ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rwxr-xr-x. 2 root root 37K 8 月 4 2017 /root/dior/login
注: 在这里, 小于就用 - 号, 后面跟单位字节
文件名后缀
概念: 在 linux 里, 命令有区分大小写
后缀名可自定义, 不过一般都是分类进行区别, 方便大家查找
参考文献: https://blog.csdn.net/fushaonian/article/details/72782651
来源: http://www.bubuko.com/infodetail-2544845.html