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]
1. find文件/目录查找命令
find 命令根据预设的条件递归查找文件或目录所在位置
命令格式:find 查找路径 查找条件1 查找条件2 .. [-exec 处理命令 {} \; ]
–exec 可接额外的命令来处理查找到结果
{} 代表find查找到的内容被放置{}中
\; 代表额外处理命令结束
常用查找条件
-type 类型(f文件 d目录 l链接文件)
-name “文件名”
-iname 按文件名查找忽略大小写
-size 文件大小(k、M、G + 大于 - 小于)
-a (and并且)两个条件同时满足
-o (or或者)两个条件满足任意一个即可
-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
#-o或者,当有多个条件时,满足任意其中一个即可
[root@thinkmo ~]# find /var/log -name "*.log" -o -size -10k -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
百度:多查--多查--多查
查找/var/log/messages 文件,清空文件内容,使用find实现
[root@localhost ~]# find /var/log/ -name messages -type f -exec cp /dev/null {} \;
查找/var/log以.log结尾的文件,清空文件内容,使用find实现
[root@localhost ~]# find /var/log -name *.log -type f -a -mtime +10 -exec cp /dev/null {} \;
2. 压缩与解压缩
Linux独有压缩格式及命令工具:
gzip---> .gz
bzip2---> .bz2
xz---> .xz
压缩命令格式
gzip [选项...] 文件名
常用选项:-d 解压缩
bzip2 [选项...] 文件名
常用选项:-d 解压缩
xz [选项...] 文件名
常用选项:-d 解压缩
查看压缩文件内容
zcat [选项...] 文件名 #查看gzip格式压缩文件
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
3. 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/