day11 linux文件之查找、打包、压缩、管道
1关闭selinux的方式
• 临时关闭 setenforce 0
• 永久关闭 vim /etc/selinux/config
2.关闭firewalld的几种方式
systemctl stop firewalld
systemctl disable --now firewalld
3.设置系统编码的方式
LANG=zh_CN.UTF-8
vim /etc/locole.conf
localectl set-locale en_US.utf-8(推荐)
4.用户登录系统的方式
ssh test@192.168.15.200
su test
su -test
5.查看当前用户的命令
sh-4.2# whoami
root
6.设置密码的方式
passwd test
echo 123456 | passwd --stdin test
7.查看selinux的状态和firewalld的状态
• getenforce
• systemctl status firewalld
今日内容
1.查找文件——find命令
在linux系统中,按照我们的要求去查找文件,如按创建的日期查找文件,查询出所有的目录或文件夹,查询出权限为644的文件,查询出属主为test的文件,我们需要用到find命令.
1.格式:find [查询的路径] [匹配模式] [匹配规则]
2.匹配模式:
-name:按名字去匹配
*:匹配任意数量的任意字符(匹配零个或多个任意字符)
?:匹配任意一个字符
-type:按照文件的类型匹配
常见的文件类型:
f:普通文件
d:普通文件夹
b:块设备文件
c:字符设备文件
s:socket文件
l:链接文件
-perm :按文件的权限来查询
常见的文件权限:755:文件夹的默认权限
644:文件的默认权限
-user:按照文件的属主来查询
-nouser:查询用户被删除了的文件
-group:按照文件的属组来查询
-nogroup:查询没有属组的文件
知识储备:删除用户:userdel
删除用户组:groupdel
-size:按照文件的大小来查询
+ 查询超过n的文件
- 查询小于n的文件
知识储备:stat:查看文件的各种时间
-mtime:按照修改文件的时间来查询
-ctime:按照文件的创建时间来查询
-atime:按照访问时间来查询文件
-a(默认):并且(and)
-o:或者(or)
-exec(xargs):处理匹配之后的内容
案例:
案例1:查询出/etc目录下的hosts文件
[root@localhost ~]# find /etc -name "hosts"
案例2:查询出/etc目录下的以ifcfg开头的文件
[root@localhost ~]# find /etc/ -name "ifcfg*"
案例3:查询出/etc目录下以.conf结尾的文件
[root@localhost ~]# find /etc/ -name "*.conf"
案例4:查询出/etc目录下,文件名中包含host的文件有哪些
[root@localhost ~]# find /etc/ -name "*host*"
案例5:查询出/etc目录下,所有的普通文件。
[root@localhost ~]# find /etc/ -type f
案例6:查询出/etc目录下,所有的文件夹
[root@localhost ~]# find /etc/ -type d
案例7:查询出/dev/目录中的所有的块设备文件
[root@localhost ~]# find /dev/ -type b
案例8:查询出/dev目录中所有的字符设备文件
[root@localhost ~]# find /dev/ -type c
案例9:查询出/etc目录中文件名包含nginx的普通文件
[root@localhost ~]# find /etc/ -name "*nginx*" -type f
案例10:查询出/root目录下,权限为755的文件
[root@localhost ~]# find /root/ -perm 755
案例11:查询出属主为test01的文件
[root@localhost ~]# find /root/ -user test01
案例12:查询属主被删除了的文件
[root@localhost ~]# find /root/ -nouser
案例13:查询属组为test的文件
[root@localhost ~]# find /root/ -group test
案例14:查询属组被删除了的文件
[root@localhost ~]# find /root/ -nogroup
案例15:查询2天以前修改过的文件
[root@localhost ~]# find /root/ -mtime +2
案例16:查询2天以内创建的文件
[root@localhost ~]# find /root/ -ctime -2
案例17:查询2天以内访问过的文件
[root@localhost ~]# find /root/ -atime -2
案例18:查询大于1M的文件
[root@localhost ~]# find /root/ -size +1M
案例19:查询小于1M的文件
[root@localhost ~]# find /root/ -size -1M
案例20:查询在3天以内创建的文件,并删除
[root@localhost tmp]# find /tmp/ -ctime -3 -type f -exec rm -rf {} \;
-exec : 处理查询之后的内容
{} : 代表的是查询到的内容、
\; : 固定搭配
知识储备:
xargs :将所有的内容格式化成一行,我们可以通过管道或标准输入给命令传递参数,但这意味着给命令的参数可能包含换行,而有些命令是不能接受换行的,所以可以通过xargs将参数过滤,将换行换成空格
[root@localhost tmp]# find /tmp/ -ctime -3 -type f | xargs -I {} rm -rf {}
练习1:要求将所有3天前创建的普通文件加上.bak后缀
[root@localhost tmp]# find /tmp -ctime -3 -type f -exec mv {} {}.bak \;
[root@localhost tmp]# find /tmp/ -type f -ctime -3 | xargs -I {} mv {} {}.bak
linux系统压缩包
gzip :压缩软件,将文件做成一个压缩包,会删除原来的文件,生成一个新的压缩包文件
格式:
压缩:gzip [文件路径]
解压:gzip -d [压缩包路径]
缺陷:
gzip不能压缩目录
bzip:压缩软件,将文件做成一个压缩包,会删除原来的文件,生成一个新的压缩包文件
格式:
压缩:bzip2 [文件路径]
解压:bzip2 -d [压缩包路径]
缺陷:
bzip2不能压缩目录
gzip比bzip2压缩率更大
tar:打包文件,不会删除原文件,可以打包目录;tar命令是可以跟gzip或bzip2共同使用的
格式:tar [参数] 压缩包的名称
tar -c -f chaoge.txt.tar chaoge.txt
tar -c -j -f chaoge.txt.tar.bz2 chaoge.txt
参数:
-f:指定压缩包的名称
-c(小):打包文件
-z:指定使用gzip压缩,一般使用gzip压缩的文件都以.tar.gz作为扩展名
-j: 指定使用bzip2压缩,一般使用bzip2压缩的文件都以.tar.bz2作为扩展名
-v:显示压缩包压缩的过程
-x:解压,不需要指定压缩包的压缩类型,他会自动匹配压缩包的内省自行解压
-P(大写):当压缩包中存在根目录时,自动移除根目录
-t:查看压缩包中的内容
-C(大) : 解压至
练习:将/etc目录中的所有的普通文件压缩成/tmp/etc.tar.gz文件
tar -czvPf /tmp/etc.tar.gz $(find /etc/ -type f | xargs)
知识储备:$():相当于数学中的括号,优先看括号中的内容 (1+2)*3
zip:
安装压缩与解压命令
yum -y install zip unzip
zip - package and compress (archive) files 压缩文件或文件夹
压缩
-r 选项指定你想递归地(recursively)包括所有包括在 filesdir 目录中的文件
zip -r etc.zip /etc
解压
unzip - list, test and extract compressed files in a ZIP archive 显示、测试、解压ZIP包
-v 显示压缩目录内容,但是不解压 unzip -v etc.zip
-t 检查压缩文件是否正确、完整,但是不解压 unzip -t etc.zip
-l 列出压缩文件内容,但是不解压 unzip -l etc.zip
示例:
zip -r tmp.zip /tmp unzip tmp.zip
zip install.log.zip install.log unzip install.log.zip
linux中的管道
用于传输数据,可以将前一个命令的结果,交给管道之后的命令处理
格式: |
案列1:删除/tmp目录下,一天以内创建的文件
find /tmp/ -ctime -1 -type f | xargs -I {} rm -rf {}
案例2:将etc中所有的普通文件,复制到/tmp目录中
[root@localhost tmp]# find /etc/ -type f | xargs -I {} cp -r {} /tmp/