
因为资料有很多,会忘记放在了什么位置,所以需要通过查找的方式进行搜索。(相当于windows查找文件 计算机--->搜索框)
二.Linux中怎么查找文件查找
因为linux中没有图形工具,所以只能使用命令工具--find--来搜索
三.find命令使用语法
命令(find) 路径(/etc/) 选项(要搜索什么) 表达式(名称,大小,文件类型,时间,组和用户) 动作(删除,-exec \)
四.如何通过名称,大小,文件类型,时间,组和用户来搜索文件。
1.按名称查找 (-iname 不区分大小写)
[root@localhost ~]# find ./ -name "zy*"
[root@localhost ~]# find ./ -name "*zy"
(搜索当前目录下以zy开头的所有内容)
[root@localhost ~]# find ./ -iname "zy*"
(搜索当前目录下不区分大小写zy开头的内容)
2.按文件大小查找(以/etc/目录为例)
[root@localhost ~]# find /etc/ -size +5M
(搜索/etc/目录中大于5MB的内容)
[root@localhost ~]# find /etc/ -size -5M
(搜索/etc/目录中小于5MB的内容)
[root@localhost ~]# find /etc/ -size 5M
(搜索/etc/目录中等于5MB的内容)
PS:M单位也可以是k,G。
3.按文件类型查找 (-type)
文件类型:
f 普通文件
d 目录
s socket套接字文件
l 链接文件
c 字符设备
b 块设备
[root@localhost ~]# find ./ -type f -iname "zy*"
(搜索当前目录中所有以“zy”开头的文件并且不区分大小写)
[root@localhost ~]# find /etc/ -type f -size +5M -name "*.bin"
(搜索/etc/目录中以.bin结尾的并且文件大于5M的文件)
[root@localhost ~]# find /etc/ -type f -name "*.repo"
(搜索/etc/目录中名称以.repo结尾的文件)
[root@localhost ~]# find /dev/ -type b -name "sda*"
(搜索/dev/目录中名称以sda开头的块设备文件)
[root@localhost ~]# find /dev/ -type c -name "tty*"
(搜索/dev/目录中以tty开头的字符设备文件)
4.按时间查找(-mtime)
[root@localhost ~]# find ./ -type f -mtime 7
(查找出当前目录下第7天的文件)*例:今天是10号,我要查找第七天的内容,就是10号之前的7天就是3号。*
[root@localhost ~]# find ./ -type f -mtime +7
(查找出当前目录下7天之前的文件内容)
[root@localhost ~]# find ./ -type f -mtime -7
(查找出当前目录下最近七天的文件内容)
实际使用方案
find /backup/ -iname “*.bak” -mtime +7 -delete
(保留最近七天的文件其他全部删除)
find /backup/ -iname “*.bak” -mtime +90 -delete
(保留最近三个月的文件其他全部删除)
find /backup/ -iname “*.bak” -mtime +180 -delete
(保留半年的文件其他全部删除)
5.按用户和组查找(-user -group -nouser -nogroup)
[root@localhost ~]# find /home/ -user zhangyao
(查找属主是zhangyao的内容)
[root@localhost ~]# find /home/ -group zhangyao
(查找属组是zhangyao的内容)
[root@localhost ~]# find /home/ -type d -user root -group zhangyao
(查找属主是root,属组是zhangyao的目录)
[root@localhost ~]# find /home/ -nouser
(查找/home/目录下没有属主的内容)
[root@localhost ~]# find /home/ -nogroup
(查找/home/目录下没有属组的内容)
[root@localhost ~]# find /home/ -nouser -nogroup
(查找/home/目录下没有属主或没有属组的内容)
四.查找到内容后的处理动作
find的默认动作是-print(打印)
-print 打印查找到的内容
-ls 以长格式显示的方式打印查找到的内容
-delete 删除查找到的文件 (删除目录,仅能删除空目录)
-ok 后面跟自定义命令(会提示是否 *** 作)
-exec 后面跟自定义命令(标准写法 -exec \)
例:
[root@localhost ~]# find ./ -type d -name "find_*" -exec rm -rf {} \
(查找当前目录下以find_开头的目录然后删除)删除少量文件时用这个
[root@localhost ~]# find ./ -type d -name "find_*" | xargs rm -f
(查找当前目录下以find_开头的目录然后删除)删除大量文件时候用这个
[root@localhost ~]# find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \'
[root@localhost ~]# find /var/log/ -type f -name "*log" -mtime +7 | xargs rm -f
(两种方法都是删除七天之前日志文件)
五.查找只记得内容的文件
需要将find和grep组合起来用
例:
find /etc/ -type f | xargs grep “log_group” --color==auto(加颜色,可加可不加)
六.find逻辑运算符
例:
查找当前目录下属主不是root的所有文件,然后以长格式列出
find ./ -type f ! -user root -ls
查找当前目录下属主是zhangyao并且文件大小小于1k的所有文件
find ./ -type f -a -user zhangyao -a -size 1k
查找当前目录下属主为root或者以xml结尾的文件
find ./ -type f -a -user root -o -name "zy*"
转义
find ./ -type f -a \(“空格” -user root -o -name "zy*" “空格” \)
等于
find ./ -type f -a -user root + find ./ -type f -a -name "zy*"
符号连接 (symbolic link)文件 也被称为 软连接
ln -s创建软连接 ln 不接参数,创建硬链接
字符(character) /块 (block)设备文件
以c开头的就是字符设备,猫等串口设备
以b开头的就是块设备 硬盘,光驱等都都属于块设备
套接口(socket)文件
以s开头
sock文件也是一类特殊文件,这类文件通常用在网络之间,进行数据连接,列如 我们可以启动一个程序来监听客户端的请求
file 显示文件类型
which 显示命令全路径
PATH :
命令行命令会从PATH 对应的路径中查找,如果PATH对应的路径中没有查找到,就会报错
whereis 显示命令.源码 说明文档 ,及其相关文件全路径
find 查找目录下的文件
查找原理:从磁盘里一个一个查找,速度慢
查找 路径 名字 文件名
显示 位置
如果oldboy 记不全,就写old* 表示模糊查找,查的是old开头的所有文件 *boy 是查boy结尾的所有文件
按文件类型查找 -type -f
-type -d 按目录类型查找
总结: -name 按文件名查找
- type 按文件类型查找
-exec 对查找到的结果再处理
默认是交集 ,省略了-a 如果想要并集,加 -o
xargs 分组 需要接< 便准输入 符号
-n n为数字,即为几个一组
[root@oldboyedu /data]# echo {1..10} >test.txt
[root@oldboyedu /data]# cat test.txt
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu /data]# xargs -n 3 <test.txt
1 2 3
4 5 6
7 8 9
10
[root@oldboyedu /data]# xargs -n 4 <test.txt
1 2 3 4
5 6 7 8
9 10
[root@oldboyedu /data]# xargs -n 5 <test.txt
1 2 3 4 5
6 7 8 9 10
-d 自定义分隔符,如果不指定,分隔符就是空格
-i 把{} 当作前边查找的结果
date 显示与设置系统时间
-s 修改时间
date +%F 显示当前时间
tar 打包压缩命tar zcvf
打包: tar zcvf 位置 ./文件
查看 tar -tf 位置.tar.gz
解压 tar zxvf 文件.gz (-C 可以指定解压位置)
语法:tar 参数: 筐(包) 苹果(文件)
-z 压缩
-c 创建mn
-v 显示输出过程
-f 指定压缩文件的名字
-t 查看压缩内容
-C 指定解压目录
-x 解压
-h 跟随软连接
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)