RHCSA_DAY09
常用特殊符号的使用
Linux系统下通配符起到了很大的作用,对于不确定的文档名称可以使用以下特殊字符表示
*常用的特殊符号,在文件名上,用来代表任意多个任意字符**
? 常用的特殊符号,在文件名上,用来代表任意单个任意字符
[0-9] #在文件名上,用来代表多个字符或连续范围中的一个,若无则忽略
{a,b,cd,abcd} #在文件名上,用来代表多组不同的字符串,全匹配
- 范例
#查找以tab结尾的文件
[root@localhost ~]# ls /etc/*tab
[root@localhost ~]# ls /etc/*wd
[root@localhost ~]# ls /etc/*.conf
[root@localhost ~]# ls /etc/redhat*
[root@localhost ~]# ls /etc/*ss*
#查找以tty开头的文件,结尾以一个任意字符结尾
[root@localhost ~]# ls /dev/tty?
[root@localhost ~]# ls /etc/host?
[root@localhost ~]# ls /etc/pass??
#查找tty开头结尾以1-5连续字符结尾
[root@localhost ~]# ls /dev/tty[1-5]
[root@localhost ~]# ls /dev/tty[4-9]
[root@localhost ~]# ls /dev/tty[1,3,5,7,9,15,20,30]
#查找tty开头结尾为不连续字符结尾
[root@localhost ~]# ls /dev/tty{1,3,5,7,9,15,20,30}
[root@localhost ~]# ls /dev/tty{1..9}
[root@localhost ~]# ls /dev/tty{1..10}
[root@localhost ~]# ls /dev/tty[1-10]
grep文件内容过滤
grep用于查找文件中符合条件的字符串,它能利用正则表达式搜索文件中的字符串,并把匹配到的字符串的行打印出来
命令格式:grep [-选项] "查找条件" 目标文件
常用选项:
-n #以行号形式输出
-i #忽略字符串大小写
-v #显示不包含匹配的行(排除)
常用正则表达式符号
^字符串 #显示以该字符串开头的行
$字符串 #显示以该字符串结尾的行
^$ #显示空行
grep命令示例
#过滤包含root关键字的行
[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
#以行号形式过滤包含root关键字的行
[root@localhost ~]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost ~]# grep -n bash /etc/passwd
[root@localhost ~]# grep -n : /etc/passwd
#忽略大小写过滤
[root@localhost ~]# grep -i -n ssh /etc/passwd
38:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
#排除包含#号的行
[root@localhost ~]# grep -n -v '^#' /etc/fstab
#过滤以root开头的行
[root@localhost ~]# grep ^root /etc/passwd
#过滤以root结尾的行
[root@localhost ~]# grep -n 'root$' /etc/passwd
[root@localhost ~]# grep -n 'bash$' /etc/passwd
#语法错误示范
[root@localhost ~]# grep -n -v '^#' ^$ /etc/fstab
grep: ^$: 没有那个文件或目录
/etc/fstab:1:
/etc/fstab:9:/dev/mapper/centos-root / xfs defaults 0 0
/etc/fstab:10:UUID=ae55ec6b-973b-498e-a366-f35e14b3d153 /boot xfs defaults 0 0
/etc/fstab:11:/dev/mapper/centos-swap swap
#语法错误示范
[root@localhost ~]# grep -n -v '^#' /etc/fstab | grep -v ^$
1:
9:/dev/mapper/centos-root / xfs defaults 0 0
10:UUID=ae55ec6b-973b-498e-a366-f35e14b3d153 /boot xfs defaults 0 0
11:/dev/mapper/centos-swap swap swap defaults 0 0
#正确语法
[root@localhost ~]# grep -v '^#' /etc/fstab | grep -v ^$ -n
2:/dev/mapper/centos-root / xfs defaults 0 0
3:UUID=ae55ec6b-973b-498e-a366-f35e14b3d153 /boot xfs defaults 0 0
4:/dev/mapper/centos-swap swap swap defaults 0 0
#显示该文件内有效配置的行
[root@localhost ~]# grep -v '^#' /etc/login.defs | grep -v ^$ -n | wc -l
find文件/目录查找命令
find 命令根据预设的条件递归查找文件或目录所在位置
命令格式:find 查找路径 查找条件1 查找条件2 .. [-exec 处理命令 {} ; ]
–exec 可接额外的命令来处理查找到结果
{} 代表find查找到的内容被放置{}中
; 代表额外处理命令结束
常用查找条件
-type 类型(f文件 d目录 l链接文件)
-name “文件名”
-iname 按文件名查找忽略大小写
-size 文件大小(k、M、G + 大于 - 小于)
-a (并且)两个条件同时满足
-o (或者)两个条件满足任意一个即可
-user 用户名
-mtime 按日期查找(+ 代表多少天之前 - 代表多少天之内,0代表24小时之内)
find命令范例
[root@localhost ~]# ls /var/log
#按照类型查找,类型为文件
[root@localhost ~]# find /var/log -type f
[root@localhost ~]# ll boot.log-20210417
[root@localhost ~]# ll /var/log/boot.log-20210417
[root@localhost ~]# ll /var/log/vmware-network.2.log
#按照类型查找,类型为目录
[root@localhost ~]# find /var/log -type d
[root@localhost ~]# ll -d /var/log/tuned
[root@localhost ~]# ll -d /var/log/qemu-ga
#按照类型查找,类型为链接文件
[root@localhost ~]# find /var/log -type l
[root@localhost ~]# fin /etc/ -type l
[root@localhost ~]# find /etc/ -type l
[root@localhost ~]# ll /etc/scl/conf
#按照名字查找
[root@localhost ~]# find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
#按照名字查找,类型为文件
[root@localhost ~]# find /etc/ -name passwd -type f
#按照名字查找,以tab结尾,类型为文件
[root@localhost ~]# find /etc/ -name '*tab' -type f
#按照名字查找,以pass开头,类型为文件
[root@localhost ~]# find /etc/ -name 'pass*' -type f
[root@localhost etc]# find . -name '*.conf' -type f
[root@localhost ~]# find /etc/ -name '*tab*' -type f
#按照名字忽略大小写查找,类型为文件
[root@localhost ~]# find /etc/ -iname FSTAB -type f
/etc/fstab
[root@localhost ~]# find /etc/ -name FSTAB -type f
#查找大于10k的文件
[root@localhost ~]# find /var/log -size +10k -type f
[root@localhost ~]# du -h /var/log/boot.log-20210417
16K /var/log/boot.log-20210417
#查找大于1M的文件
[root@localhost ~]# find /var/log -size +1M -type f
[root@localhost ~]# du -h /var/log/audit/audit.log
2.4M /var/log/audit/audit.log
[root@localhost ~]# find /home -size +1M -type f
#查找小于1M的文件
[root@localhost ~]# find /var/log -size -1M -type f
[root@localhost ~]# du -h /var/log/spooler
0 /var/log/spooler
#查找大于10k并且下于20k,类型为文件
[root@localhost ~]# find /var/log -size +10k -a -size -20k -type f
#查找大于10k或者小于100k,类型为文件
[root@localhost ~]# find /var/log -size +10k -o -size -100k -type f
#查找属于lisi用户的文件/目录
[root@localhost ~]# find /home -user lisi
#查找30天之前被修改过,类型为文件
[root@localhost ~]# find /var/log -mtime +30 -type f
[root@localhost ~]# find /var/log -mtime +10 -type f
#查找10天之内被修改过,类型为文件
[root@localhost ~]# find /var/log -mtime -10 -type f
root@localhost ~]# find /var/log -mtime -30 -type f
#查找30之前被修改过,类型为文件,拷贝到/opt目录下
[root@localhost ~]# find /var/log -mtime -30 -type f -exec cp {} /opt \;
题型:
- 查找/etc/目录下以.conf结尾的文件(只能在/etc这一层目录去查找)
[root@localhost ~]# ls /etc/*.conf - 查找/etc/目录下以.conf结尾的文件(包含所有的子目录)
[root@localhost ~]# find /etc/ -name '*.conf' -type f
压缩与解压缩
Linux独有压缩格式及命令工具:
gzip---> .gz
bzip2---> .bz2
xz---> .xz
压缩命令格式
gzip [选项...] 文件名
常用选项:-d 解压缩
bzip2 [选项...] 文件名
常用选项:-d 解压缩
xz [选项...] 文件名
常用选项:-d 解压缩
查看压缩文件内容
zcat [选项...] 文件名
bzcat [选项...] 文件名
xzcat [选项...] 文件名
[root@localhost ~]# cp /etc/services /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# ll services
-rw-r--r--. 1 root root 670293 4月 17 17:06 services
[root@localhost opt]# ll -h services
-rw-r--r--. 1 root root 655K 4月 17 17:06 services
#使用gzip格式对文件进行压缩
[root@localhost opt]# gzip services
[root@localhost opt]# ls
services.gz
[root@localhost opt]# ll -h services.gz
-rw-r--r--. 1 root root 133K 4月 17 17:06 services.gz
#不解压查看压缩文件内容
[root@localhost opt]# zcat services.gz
#解压文件
[root@localhost opt]# gzip -d services.gz
#使用bzip2格式对文件进行压缩
[root@localhost opt]# bzip2 services
[root@localhost opt]# ls
services.bz2
[root@localhost opt]# ll -h services.bz2
-rw-r--r--. 1 root root 122K 4月 17 17:06 services.bz2
#不解压查看文件内容
[root@localhost opt]# bzcat services.bz2
#解压文件
[root@localhost opt]# bzip2 -d services.bz2
#使用xz格式对文件进行压缩
[root@localhost opt]# xz services
[root@localhost opt]# ls
services.xz
[root@localhost opt]# ll -h services.xz
-rw-r--r--. 1 root root 98K 4月 17 17:06 services.xz
#解压文件
[root@localhost opt]# xz -d services.xz
tar打包工具
tar命令用在linux下用于对文件/目录打包,使用 tar 程序打出来的包常称为 tar 包,tar 包文件通常都是以 .tar 结尾
tar 命令格式:tar 选项 压缩包名字 被压缩文件
常用选项:
-c 创建打包文件
-f 指定打包后的文件名称
-z 调用gzip压缩工具 -J 调用xz压缩工具 -j 调用bzip2压缩工具
-t 列出打包文档内容
-x 释放打包文件
-C 指定解压路径
-v 显示详细信息
tar命令范例
#同时打包多个文件/目录并使用gzip格式压缩
[root@localhost opt]# tar -czf xxx.tar.gz /etc/passwd /etc/fstab /home
#将压缩包数据解压到/media目录
[root@localhost opt]# tar -xf xxx.tar.gz -C /media/
[root@localhost opt]# ls /media/etc
[root@localhost opt]# rm -rf xxx.tar.gz
#同时打包多个文件/目录并使用xz格式压缩
[root@localhost opt]# tar -cJf xx.tar.xz /etc/hostname /etc/services /home
#错误语法,f选项要放到所有选项右边
[root@localhost opt]# tar -ft xx.tar.xz
tar: 您必须从"-Acdtrux"或是"--test-label"选项中指定一个
请用“tar --help”或“tar --usage”获得更多信息。
#不解压查看压缩包数据
[root@localhost opt]# tar -tf xx.tar.xz
etc/hostname
#将压缩包数据解压到/tmp目录
[root@localhost opt]# tar -vxf xx.tar.xz -C /tmp
[root@localhost opt]# ls /tmp
#同时打包多个文件/目录并使用bzip2格式压缩
[root@localhost opt]# tar -cjf abc.tar.bz2 /etc/hostname /etc/group /home
#解压缩
[root@localhost opt]# tar -xf abc.tar.bz2 -C /media/
RHCSA_DAY09的更多相关文章
随机推荐
- 14.5、redis-sentinel高可用
1.redis主从同步配置: (0)主机配置: 服务器名称 ip地址 实例6379 实例6380 实例6381 controller-node1 172.16.1.90 主 从 从 (1)确定主从: ...
- 15、oracle多表查询
15.0.实验建表: --父表 create table class( id number(10)constraint class_id_pk primary key, class_name varc ...
- Springboot:单元测试Junit基本注解@BeforeClass、@AfterClass、@Before、@After、@Test、
一.unit中集中基本注解,是必须掌握的. @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意public ...
- final添加内存屏障问题
看了 why大佬的 博客一个困扰我122天的技术问题,我好像知道答案了. 发现他留了个坑,在变量i类型为 int 或者 Integer 时,int类型的i死循环了而Integer类型的i可以结束 in ...
- XSS challenges 1-10
学长发的xss靶场,刚好js学完了,上手整活. 这个提示说非常简单,直接插入就完事了 <script>alert(document.domain)</script> 第二关. ...
- jenkins报错The goal you specified requires a project to execute but there is no POM inthis directory
报错截图及详细: 15:30:29[ERROR]The goal you specified requires a project to execute but there is no POM i ...
- Luogu P2754 星际转移问题
Luogu P2754 星际转移问题 思路 首先,对于地球能否到达月球的问题,考虑使用并查集维护. 对于每艘飞船能够到达的站点,放进一个集合里,若两艘飞船的集合有交集,那么就合并两个集合,最后只要地球 ...
- 【论文阅读】DSDNet Deep Structured self-Driving Network
前言引用 [2] DSDNet Deep Structured self-Driving Network Wenyuan Zeng, Shenlong Wang, Renjie Liao, Yun C ...
- 全面了解Nginx到底能做什么
最近做项目需要动静分离,便用nginx的反向代理来实现.后来看到一篇好文,记录下. 来自https://www.jianshu.com/p/8bf73d1a758c 前言 本文只针对Nginx在不加载 ...
- Kettle——shell交互命令
Kettle--shell交互命令 在kettle上开发了job或transform可以以单独的文件存在,也可以存放在资源库中.调用这些程序可以通过shell脚本调用,记录下: 资源库中的job: . ...