Linux文件查找,打包压缩和发送邮件

find详解: 文件查找,针对文件名

语法:
# find 路径 条件 跟条件相关的操作符   [-exec 动作]
路径:
默认不写路径时查找的是当前路径.
加路径。
条件:
1.指定的名称  -name
2.文件类型  -type
3.权限  -perm
4.时间  -mtime +3   #找三天之前
       -mmin +3   #找三分钟之前
1.1.按文件名
从根开始找文件
time find / -name “file2” #从根开始找文件
time #显示该命令执行时间

find /etc -name "ifcfg-ens33" #以名字的方式查找 
find /etc -iname "Ifcfg-ens33" #-i忽略大小写

熟用*通配符

[root@qfedu.com ~]# find /etc -iname "*.txt"
参数解释:
*:表示所有字符
1.2.按文件大小 -size
find /etc -size +5M		#大于5M
find /etc -size 5M		#等于5M
find /etc -size -5M      #小于5M
find / -size +3M -a -size -5M  #查找/下面大于3M而且小于5M的文件
-o:or
find / -size -1M -o -size +80M      #查找/下面小于1M或者大于80M的文件
-a:a
find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件
1.3按时间查找
按时间找(atime,mtime,ctime)
-atime = access访问时间
-mtime = modify改变时间   内容修改时间会改变
-ctime = change修改时间   属性修改时间会改变
find /opt -mtime +5		#修改时间5天之前
find /opt -atime +1     #访问时间1天之前
find . -mtime -2		#修改时间2天之内

find . -amin +1         #访问时间在1分钟之前
find /opt -amin -4      #访问时间在4分钟之内
find /opt -mmin -2      #修改时间在2分钟之内
1.4按文件类型
find /dev -type f	#f普通文件
find / -type f -size -1M -o -name "*.txt"

find /dev -type d	#d目录
find /etc/ -type d -name "*.conf.d"

find /etc -type l	#l链接

find /dev -type b	#b块设备
find /dev/ -type b -name "sd*"
1.5按文件权限
find . -perm 644            #.是当前目录    精确查找644  
find /usr/bin  -perm -4000  #包含set uid
find /usr/bin  -perm -2000  #包含set gid
find /usr/bin  -perm -1000  #包含sticky
1.6找到后处理的动作 ACTIONS
find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作-----  查找带ifcfg开头的文件复制到tmp下
touch /home/test{1..20}.txt
find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式

find使用xargs

touch /home/test{1..20}.txt
find /home/ -name "test*" | xargs -i cp {} /tmp/ #找到之后删除处理xargs 参数传递

-exec和xargs的区别

-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数转换成下一个命令的参数列表。

1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。 
3、xargs不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,那么使用 xargs比较方便; 否则,就要用 exec了。

**案例1: 分别找出test5 和除了test5的文件 **

[root@qfedu.com ~]# find /home/ -name *test5*
[root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反

打包压缩

window打包压缩工具:

结尾:.rar     .zip
打包工具:winrar zip 7zip bandizip

linux打包压缩工具:

结尾:.tar.gz      .tar.bz2     .zip   xz
工具:gzip和tar(打包)

bzip2(只压缩)

zip

xzip

打包

语法:
#tar cvf xxxx.tar filename   被打包的文件 ...
c :create  创建
v :verbose 详细信息
f :file  指定文件

解包

#tar xvf filename.tar [-C /root/Desktop]
x: extract  解压缩  解包
-C: 指定解包路径

案例

[root@qfedu.com ~]# tar cvf dir1.tar /home/dir10/ #打包目录dir10,将包命名为dir1.tar
[root@qfedu.com ~]# tar xf dir1.tar -C /usr/local/ #将dir1包解压到指定目录

压缩

gzip bzip2 xzip zip

语法:
压缩:
	#gzip  源文件   #格式  file.gz结尾
	#bzip2 源文件   #格式  file.bz2结尾

bzip2需要安装

[root@qfedu.com ~]# yum -y install bzip2   #打包bzip2需要安装

解压缩

语法:
#gunzip    压缩文件
#bunzip2   压缩文件

#gzip 	-d 压缩文件  
#bzip2  -d 压缩文件
-d:dicompress 解压缩

案例

[root@qfedu.com ~]# gzip file1  #压缩
[root@qfedu.com ~]# gzip -d file1.gz #解压缩
[root@qfedu.com ~]# gunzip file1.gz  #也是解压缩包
[root@qfedu.com ~]# gzip -c file1 > /usr/local/file1.gz  #压缩到指定位置(注意以.gz结尾)
[root@qfedu.com ~]# gunzip -c /usr/local/file1.gz > /opt/file1 #解压到指定位置(解压出的名字可以自定义)
-c, --stdout

打包压缩一起:

语法:
#tar cvzf file.tar.gz  源文件
#tar cvjf file.tar.bz2 源文件
#tar cvJf file.tar.xz  源文件
z:表示gz压缩
j:表示bz2压缩

解压解包一起:

语法:
#tar xvzf 压缩文件 [-C 解压路径]
#tar xvjf 压缩文件 [-C 解压路径]

案例

[root@qfedu.com ~]# tar czf dir1.tar.gz dir1              #打包并压缩
[root@qfedu.com ~]# tar xzf dir1.tar.gz -C /usr/local/    #解压到指定位置

打包到指定路径

[root@qfedu.com ~]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/  #将打包的文件放到/tmp目录下,并以当前时间开头命名

扩展–按时间创建目录或者文件

# mkdir `date +%F`-upload
# touch file-`date +%F`.txt

发送邮件

yum -y install mailx
vim /etc/mail.rc
set from=19991820903@163.com
set smtp=smtp.163.com
set smtp-auth-user=19991820903@163.com
set smtp-auth-password=GSLTKOHJSRNPAUBV
set smt p-auth=login
cat /etc/passwd | mail -s "测试邮件" 19991820903@189.cn

stall mailx


```shell
vim /etc/mail.rc
set from=19991820903@163.com
set smtp=smtp.163.com
set smtp-auth-user=19991820903@163.com
set smtp-auth-password=GSLTKOHJSRNPAUBV
set smt p-auth=login
cat /etc/passwd | mail -s "测试邮件" 19991820903@189.cn

image-20240929165100413

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值