2.23/2.24/2.25 find命令
which命令
- 在Linux下搜索文件,which命令
- which命令是在PATH环境变量中去查找那些命令在哪里
可查找某个命令的绝对路径。
whereis命令通过预先生成的一个文件列表库查找与给出的文件名相关文件。
- whereis 【-bms】 【文件名】
- -b:只查找二进制文件
- -m:只查找帮助文件(在man目录下的文件)
- -s:只查找源代码文件
locate 命令
- locate命令,需要yum安装——>yum install -y mlocate
- 在安装后使用locate命令,会提示:locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db’: 没有那个文件或目录——>是因为还没有产生db这个文件,就是这个文件所产生的一个数据库
- 数据库每天凌晨四点会自动生成,若想要手动生成,只需要执行updatedb命令即可
- 属于模糊搜索
[root@hf ~]# yum install -y mlocate
[root@hf ~]# locate ls
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录
[root@hf ~]# updatedb
[root@hf ~]#
- 类似whereis命令,也是通过预先生成的文件列表库来告诉用户要查找的文件在哪,后面直接跟文件名
- locate搜索到的文件列表,不管是目录名还是文件名,只要包含关键词,都会列出来。
which命令
- which 搜索命令的绝对路径(搜索的目录,默认为 echo $ PATH 该变量下的目录),前提必须得在默认变量下的目录下,同时还得有执行权限,才能搜索得到。
whereis、locate命令
- whereis命令是搜索文件的
- whichis命令,通过预先生成的一个文件列表库查找与给出的文件名相关文件。 (有局限性)
- whereis 【-bms】 【文件名】
- -b:只查找二进制文件
- -m:只查找帮助文件(在man目录下的文件)
- -s:只查找源代码文件
- locate命令——>安装包 yum install -y mlocate (模糊搜索)
- 通过预先生成的文件列表库来告诉用户要查找的文件在哪,后面直接跟文件名 locate搜索到的文件列表,不管是目录名还是文件名,只要包含关键词,都会列出来。( locate命令,不会搜索 tmp 下的文件)
- 使用需要先产生库文件,可以手动升级 updatedb 更新数据库
- 通过预先生成的文件列表库来告诉用户要查找的文件在哪,后面直接跟文件名 locate搜索到的文件列表,不管是目录名还是文件名,只要包含关键词,都会列出来。( locate命令,不会搜索 tmp 下的文件)
linux基础快捷键
- Linux快捷键使用:
- ctrl+l:清屏
- ctrl+d: 退出终端;或者输入命令瑞出,exit 或者 logout(在行内有字符的情况下是向后删)
- ctrl+c:直接取消、暂停当前正在运行的进程、取消当前输入
- ctrl+u:清除当前光标位置至最前内容
- ctrl+k:清除当前光标位置至最后内容
- ctrl+e:移动光标是末尾
- ctrl+a:移动光标是开头
- ctrl+z:暂停进程运行 fg 可以会之前暂停的进程
- ctrl+s:暂停动态运行的进程的屏幕 ctrl+q 恢复屏幕动态
find命令
- find命令语法:
- find [路径][参数]
- find 搜索用 用法:
- 1.根据名字查找
- find 路径 -name “文件名字 ” 去搜索
[root@hf ~]# find /etc/ -name "sshd_config"
/etc/ssh/sshd_config
[root@hf ~]#
- find只知道名字的模糊搜索
[root@hf ~]# find /etc/ -name "sshd*"
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
/etc/systemd/system/multi-user.target.wants/sshd.service
[root@hf ~]#
- 2.根据文件类型查找
- find 路径 -type 文件类型
- d(目录)
- f(普通的文件)——>二进制的、或文本文档都可以列出来
- l(软链接文件)
- s、c(字符串设备文件)
- b(块设备文件)-name
指定只要目录去搜索
[root@hf ~]# find /etc/ -type d -name "sshd*"
指定类型去搜索
[root@hf ~]# find /etc/ -type f -name "sshd*"
/etc/sysconfig/sshd
/etc/ssh/sshd_config
/etc/pam.d/sshd
[root@hf ~]#
stat命令
- stat命令可以列出文件的具体信息,包括atime、ctime、mtime
- 格式:stat 文件/目录
stat 2.txt
- 三个时间属性:
- Access time 为最近访问时间
- Modify time 为最近创建或更改时间 (更改内容会改变时间)
- Change time 为更改文件,更改权限,更改属组,属主时间(更改文件大小,也会改变)
3.根据时间查找
- 最近访问为atime、 最近更改(更改的是文件的内容,包括创建)为mtime、 最近改动(更改所有者、权限、大小,随着inode更改而更改等)为ctime。
- ①文件的access time(atime)是在读取文件或执行文件时更改的。
- ②文件的modified time(mtime)是在写入文件时随文件的内容更改而更改的。
- ③文件的change time (ctime)是在写入文件,更改所有者、权限或链接设置时随着inode内容更改而更改。 inode(索引节点)用来存放档案及目录的基本信息,包含时间信息、文档名、所有者以及所属组。
- 更改文件的内容即会更改mtime和ctime,但文件的ctime更改了,mtime未必会会更改,如:更改了文件权限,但是文件内容没有变化。
- 三个time的属性值都放在了inode中,若mtime,atime被修改,那么inode一定会更改,既然inode更改了,那么ctime也就会跟着更改。
- -o :或者的意思 or 查找atime更改小于一天的文件
- 查找,etc目录下, f 类型,一天以内mtime有变动的文件(+1:当天以外,-1:当天)
[root@hf ~]# find /etc/ -type f -mtime -1
/etc/tuned/active_profile
/etc/resolv.conf
[root@hf ~]#
- 查找,etc目录下, f 类型,一天以内mtime有变动的,且文件名以conf结尾的文件
[root@hf ~]# find /etc/ -type f -mtime -1 -name "*.conf"
/etc/resolv.conf
[root@hf ~]#
- 查找,etc目录下, f 类型,或者一天以内mtime有变动的,或者文件名以conf结尾的文件
[root@hf ~]# find /etc/ -type f -o -mtime -1 -o -name "*.conf"
- 查找,etc目录下, f 类型,小于60分钟内改动过,且文件名以conf结尾的文件
[root@hf ~]# find /etc/ -type f -mmin -60 -name "*.conf"
- 4.find查找文件的硬链接
- find 【路径】-inum inode号
[root@hf ~]# ls -l 1_heard.txt
-rw-r--r-- 2 root root 0 2月 13 22:31 1_heard.txt
[root@hf ~]# ls -i 1_heard.txt
76239367 1_heard.txt
[root@hf ~]# find / -inum 76239367
/root/1_heard.txt
/tmp/1.txt.bak
[root@hf ~]#
使用find查找inode号,来找到这个硬链接文件
5.find查找到文件后,直接显示出结果
- find 【路径】【参数】 -exec 【命令】 { } (这是空格)\;
- 注意:结尾一定要有 ;
[root@hf ~]# find /etc/ -type f -mtime -1 -name "*.conf" -exec ls -l {} \;
-rw-r--r-- 1 root root 54 2月 13 17:04 /etc/resolv.conf
[root@hf ~]#
- 查找,根目录下, f 类型,小于60分钟内改动过,把查找到的结果显示出来
find / -type f -mmin -60 -exec ls -l {} \;
- 查找,根目录下, f 类型,小于60分钟内改动过,把查找的结果更改为以bak结尾的文件
find / -type f -mmin -60 -exec mv {} {}.bak \;
- 6.find根据文件大小查找
- -size 可以查看 k(文件大小为k),M(文件大小为兆)——>小写m会报错
- 查找root目录下大于10k的文件
[root@hf ~]# find /root/ -size +10k
/root/.bash_history
/root/zabbix-release-3.2-1.el7.noarch.rpm
/root/shell/.fun3.sh.swp
[root@hf ~]#
- 查找,root目录下, f 类型,大于10k的文件,把查找到的结果显示出来
[root@hf ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rw-------. 1 root root 28K 2月 10 00:20 /root/.bash_history
-rw-r--r--. 1 root root 14K 9月 14 2016 /root/zabbix-release-3.2-1.el7.noarch.rpm
-rw------- 1 root root 12K 2月 8 00:15 /root/shell/.fun3.sh.swp
[root@hf ~]#
2.26 文件名后缀
在Linux系统中,文件都是有后缀名的,但在Linux中文件的后缀名,并不代表这个文件是什么类型
- 为了便于区分,习惯相同的文件定义相同的后缀名
- 1.sh 代表的是脚本
- 2.tar.gz代表是压缩文件
- my.cnf是一个配置文件
- test.zip是压缩文件
echo $LANG 输出当前系统语言
[root@hf ~]# echo LANG
LANG
[root@hf ~]# echo $LANG
zh_CN.UTF-8
[root@hf ~]# stat !$
stat 2.txt.bak
文件:"2.txt.bak"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:67189291 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2017-12-31 00:14:59.034915638 +0800
最近更改:2017-12-31 00:14:59.034915638 +0800
最近改动:2017-12-31 00:14:59.034915638 +0800
创建时间:-
[root@hf ~]#


&spm=1001.2101.3001.11974&articleId=79765502&d=1&t=3&u=e647e3d3c8b648a9854c049a5ec12971)

被折叠的 条评论
为什么被折叠?



