目录
Linux基础命令注释(3)
安装压缩工具
- 挂载
[root@hostnamectl ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载 [root@hostnamectl ~]# ls /mnt/ addons extra_files.json isolinux Packages RPM-GPG-KEY-redhat-release EFI GPL LiveOS repodata TRANS.TBL EULA images media.repo RPM-GPG-KEY-redhat-beta [root@hostnamectl ~]# rpm -ivh /mnt/ addons/ images/ RPM-GPG-KEY-redhat-beta .discinfo isolinux/ RPM-GPG-KEY-redhat-release EFI/ LiveOS/ TRANS.TBL EULA media.repo .treeinfo extra_files.json Packages/ GPL repodata/ [root@hostnamectl ~]# rpm -ivh /mnt/ addons/ images/ RPM-GPG-KEY-redhat-beta .discinfo isolinux/ RPM-GPG-KEY-redhat-release EFI/ LiveOS/ TRANS.TBL EULA media.repo .treeinfo extra_files.json Packages/ GPL repodata/
- 安装gzip
[root@hostnamectl ~]# rpm -ivh /mnt/Packages/gzip-1.5-9.el7.x86_64.rpm 警告:/mnt/Packages/gzip-1.5-9.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY 准备中... ################################# [100%] 软件包 gzip-1.5-9.el7.x86_64 已经安装
- 安装bzip2
[root@hostnamectl ~]# rpm -ivh /mnt/Packages/bzip2-1.0.6-13.el7.x86_64.rpm 警告:/mnt/Packages/bzip2-1.0.6-13.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:bzip2-1.0.6-13.el7 ################################# [100%]
- 安装zip
[root@hostnamectl ~]# rpm -ivh /mnt/Packages/zip-3.0-11.el7.x86_64.rpm 警告:/mnt/Packages/zip-3.0-11.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:zip-3.0-11.el7 ################################# [100%]
[root@hostnamectl ~]# rpm -ivh /mnt/Packages/unzip-6.0-16.el7.x86_64.rpm 警告:/mnt/Packages/unzip-6.0-16.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:unzip-6.0-16.el7 ################################# [100%]
- 安装XZ
[root@hostnamectl ~]# rpm -ivh /mnt/Packages/xz-5.2.2-1.el7.x86_64.rpm 警告:/mnt/Packages/xz-5.2.2-1.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID fd431d51: NOKEY 准备中... ################################# [100%] 软件包 xz-5.2.2-1.el7.x86_64 已经安装
压缩解压缩
gzip压缩与解压缩,不会保留源文件
- 压缩
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# gzip 1 [root@hostnamectl tem]# ls 1.gz 2 3 4 a b c d e f
- -d 解压缩
[root@hostnamectl tem]# gzip -d 1.gz [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]#
bzip2 压缩与解压缩,不保留原文件
- 压缩
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# bzip2 1 [root@hostnamectl tem]# ls 1.bz2 2 3 4 a b c d e f
- -d 解压缩
[root@hostnamectl tem]# bzip2 -d 1.bz2 [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f
- -k保留源文件
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# bzip2 -k 1 [root@hostnamectl tem]# ls 1 1.bz2 2 3 4 a b c d e f
zip 压缩与解压缩(归档),会保留源文件
- 压缩(归档)
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# zip 1.zip 1 adding: 1 (stored 0%) [root@hostnamectl tem]# ls 1 1.zip 2 3 4 a b c d e f
- unzip 解压缩(拆包)
[root@hostnamectl tem]# mv 1.zip a/ [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# cd a [root@hostnamectl a]# unzip 1.zip Archive: 1.zip extracting: 1 [root@hostnamectl a]# ls 1 1.zip
XZ 压缩解压缩,不保留原文件
- 压缩
[root@hostnamectl tem]# ls 1.xz 2 3 4 a b c d e f
- -d解压缩
[root@hostnamectl tem]# xz -d 1.xz [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f
- -k保留源文件
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# xz -k 1 [root@hostnamectl tem]# ls 1 1.xz 2 3 4 a b c d e f
归档工具tar
- Usage: tar [OPTION...] [FILE]...
常用的OPTION:
-c:创建归档文件
-f file.tar:指定要操作的归档文件
-x:还原归档
-v:显示归档过程
-p:归档时保留权限信息。只有管理员才有权限用此选项
-C:指定还原归档或解压时的目标目录
-tf:不展开归档,直接查看归档了哪些文件- 例:归档
[root@hostnamectl tem]# ls 1 1.xz 2 3 4 a b c d e f [root@hostnamectl tem]# tar cf holle.tar {a..f} [root@hostnamectl tem]# ls 1 1.xz 2 3 4 a b c d e f holle.tar
- 例:解包
[root@hostnamectl tem]# mv holle.tar f [root@hostnamectl tem]# ls f holle.tar [root@hostnamectl tem]# cd f [root@hostnamectl f]# tar xf holle.tar [root@hostnamectl f]# ls a b c d e f holle.tar
常用的组合项:
-zcf file.tar.gz:归档并调用gzip进行压缩
-jcf file.tar.bz2:归档并调用bzip2进行压缩
-Jcf file.tar.xz:归档并调用xz进行压缩
-xf file.tar.xz; 可以自动选择解压工具- -zcf归档压缩
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# ls e [root@hostnamectl tem]# tar zcf war3.tar.gz * [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f war3.tar.gz
- 解压缩拆包
[root@hostnamectl tem]# mv war3.tar.gz e/ [root@hostnamectl tem]# ls e war3.tar.gz [root@hostnamectl tem]# cd e [root@hostnamectl e]# tar zxf war3.tar.gz [root@hostnamectl e]# ls 1 2 3 4 a b c d e f war3.tar.gz
- -jcf归档压缩
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# tar jcf LOL.tar.bz2 * [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f LOL.tar.bz2
- 解压缩拆包
[root@hostnamectl tem]# mv LOL.tar.bz2 b [root@hostnamectl tem]# ls b LOL.tar.bz2 [root@hostnamectl tem]# cd b [root@hostnamectl b]# tar jxf LOL.tar.bz2 [root@hostnamectl b]# ls 1 2 3 4 a b c d e f LOL.tar.bz2
- -Jcf归档压缩
[root@hostnamectl tem]# ls 1 2 3 4 a b c d e f [root@hostnamectl tem]# tar Jcf HELLDIVRS.tar.xz * [root@hostnamectl tem]# ls 1 2 3 4 a b c d e f HELLDIVRS.tar.xz
- 解压缩拆包
[root@hostnamectl tem]# ls c [root@hostnamectl tem]# mv HELLDIVRS.tar.xz c/ [root@hostnamectl tem]# cd c [root@hostnamectl c]# tar Jxf HELLDIVRS.tar.xz [root@hostnamectl c]# ls 1 2 3 4 a b c d e f HELLDIVRS.tar.xz [root@hostnamectl c]#
通配符
- 创建文件
[root@hostnamectl y]# touch {A..z} [root@hostnamectl y]# touch {0..100} [root@hostnamectl y]# ls ` 10 16 22 29 35 41 48 54 60 67 73 8 86 92 99 d G k N r U y ^ 100 17 23 3 36 42 49 55 61 68 74 80 87 93 a D h K o R v Y _ 11 18 24 30 37 43 5 56 62 69 75 81 88 94 A e H l O s V z [ 12 19 25 31 38 44 50 57 63 7 76 82 89 95 b E i L p S w Z ] 13 2 26 32 39 45 51 58 64 70 77 83 9 96 B f I m P t W 0 14 20 27 33 4 46 52 59 65 71 78 84 90 97 c F j M q T x 1 15 21 28 34 40 47 53 6 66 72 79 85 91 98 C g J n Q u X
- *:匹配任意长度的任意字符
``bash [root@hostnamectl y]# ls *
10 16 22 29 35 41 48 54 60 67 73 8 86 92 99 d G k N r U y
^ 100 17 23 3 36 42 49 55 61 68 74 80 87 93 a D h K o R v Y
_ 11 18 24 30 37 43 5 56 62 69 75 81 88 94 A e H l O s V z
[ 12 19 25 31 38 44 50 57 63 7 76 82 89 95 b E i L p S w Z] 13 2 26 32 39 45 51 58 64 70 77 83 9 96 B f I m P t W
0 14 20 27 33 4 46 52 59 65 71 78 84 90 97 c F j M q T x
1 15 21 28 34 40 47 53 6 66 72 79 85 91 98 C g J n Q u X
- ?:匹配任意单个字符
[root@hostnamectl y]# ls ?[ 1 4 7 a B d E g H j K m N p Q s T v W y Z ^ ] 2 5 8 A c D f G i J l M o P r S u V x Y _ 0 3 6 9 b C e F h I k L n O q R t U w X z ``` - []:匹配指定范围内的任意单个字符 ```bash [root@hostnamectl y]# ls [a-k] a A b B c C d D e E f F g G h H i I j J k ``` ```bash [root@hostnamectl y]# ls [1-4][1-4] 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 ``` - [[:punct:]]:表示标点符号 ```bash [root@hostnamectl y]# ls [[:punct:]]
^ _ [ ]- [[:lower:]]:表示小写字母
bash
[root@hostnamectl y]# ls [[:lower:]]
a b c d e f g h i j k l m n o p q r s t u v w x y z
- [[:upper:]]:表示大写字母
bash
[root@hostnamectl y]# ls [[:upper:]]
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
- [[:alpha:]]:表示大小写字母
bash
[root@hostnamectl y]# ls [[:alpha:]]
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
- [[:digit:]]:表示数字
bash
[root@hostnamectl y]# ls [[:digit:]]
0 1 2 3 4 5 6 7 8 9
- [[:alnum:]]:表示数字和大小写字母
bash
[root@hostnamectl y]# ls [[:alnum:]]
0 2 4 6 8 a b c d e f g h i j k l m n o p q r s t u v w x y z
1 3 5 7 9 A B C D E F G H I J K L M N O P Q R S T U V W X- [^]:匹配指定范围之外的任意单个字符
bash
[root@hostnamectl y]# ls [^[:alnum:]]
` ^ _ [ ]--- ## 文本排序 - 创建多行文件
bash
[root@hostnamectl y]# ls
[root@hostnamectl y]# cat > abc << ? //问号表示以问号开头以问号结尾
2
4
6
8
1
3
5
6
7
9
?
```
---
[root@hostnamectl y]# cat > aaa <<?? > 1 w > 2 r > 3 i > 4 k > 5 a > 6 d > 7 c > ??
[root@hostnamectl y]# cat > bbb << ?? > 1 A > 2 c > 3 D > 4 C > 5 a > 6 b > 7 B > ??
- 文本排序命令
sort
Usage: sort [OPTION]... [FILE]...
常用的OPTION:
-n:以数值大小进行排序
-r:逆序排序
-t:字段分隔符
-r:逆序排序
-k:以哪个字段为关键字进行排序
-t:字段分隔符
-u:去重,排序后相同的行只显示一次
-f:排序时忽略字符大小写
---
- -n:以数值大小进行排序
[root@hostnamectl y]# sort -n abc 1 2 3 4 5 6 6 7 8 9
- -r:逆序排序
[root@hostnamectl y]# sort -nr abc 9 8 7 6 6 5 4 3 2 1
- -k[n]:以n个字段为关键字进行排序
[root@hostnamectl y]# sort -k2 aaa 5 a 7 c 6 d 3 i 4 k 2 r 1 w
- -t:字段分隔符
[root@hostnamectl y]# sort -t' ' -k2 aaa 5 a 7 c 6 d 3 i 4 k 2 r 1 w
- -f:排序时忽略字符大小写
[root@hostnamectl y]# sort -t' ' -fk2 bbb 1 A 5 a 6 b 7 B 2 c 4 C 3 D [root@hostnamectl y]# sort -t' ' -k2 bbb 5 a 1 A 6 b 7 B 2 c 4 C 3 D
- -u:去重,排序后相同的行只显示一次(需要先排序再去重)
[root@hostnamectl y]# sort -nu abc 1 2 3 4 5 6 7 8 9
文本去重命令uniq
- Usage: uniq [OPTION]... [INPUT [OUTPUT]]
常用的OPTION:
-c:显示文件中行重复的次数
-d:只显示重复的行
-u:只显示未重复的行
---
- -c:显示文件中行重复的次数
[root@hostnamectl y]# sort -n abc | uniq -c 1 1 1 2 1 3 1 4 1 5 2 6 1 7 1 8 1 9
- -d:只显示重复的行
[root@hostnamectl y]# sort -n abc | uniq -d 6
- -u:只显示未重复的行
[root@hostnamectl y]# sort -n abc | uniq -u 1 2 3 4 5 7 8 9
基础命令之cut
- Usage: cut OPTION... [FILE]...
常用的OPTION:
-d:指定字段分隔符,默认是空格
-f:指定要显示的字段
-f 1,3:显示1和3字段
-f 1-3:显示1到3字段
显示第一个单词列和第三个单词列
[root@hostnamectl y]# cp /etc/passwd . [root@hostnamectl y]# ls aaa abc bbb passwd [root@hostnamectl y]# head passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin [root@hostnamectl y]# cut -d:-f1,3 passwd cut: 分界符必须是单个字符 Try 'cut --help' for more information. [root@hostnamectl y]# cut -d:-f1,3 passwd cut: 分界符必须是单个字符 Try 'cut --help' for more information. [root@hostnamectl y]# cut -d: -f1,3 passwd root:0 bin:1 daemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 operator:11 games:12 ftp:14 nobody:99 systemd-network:192 dbus:81 polkitd:999 postfix:89 chrony:998 sshd:74 tom:1000
- --output-delimiter=" "输出用空格隔开
[root@hostnamectl c]# cut -d: -f1,3 --output-delimiter=" " passwd root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 nobody 99 systemd-network 192 dbus 81 polkitd 999 postfix 89 chrony 998 sshd 74 tom 1000
- 显示第1到3单词列
[root@hostnamectl y]# cut -d: -f1-3 passwd root:x:0 bin:x:1 daemon:x:2 adm:x:3 lp:x:4 sync:x:5 shutdown:x:6 halt:x:7 mail:x:8 operator:x:11 games:x:12 ftp:x:14 nobody:x:99 systemd-network:x:192 dbus:x:81 polkitd:x:999 postfix:x:89 chrony:x:998 sshd:x:74 tom:x:1000
高级命令之awk
- 显示第1个单词列和第3个单词列
[root@hostnamectl y]# awk -F: '{print $1,$3}' passwd root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 nobody 99 systemd-network 192 dbus 81 polkitd 999 postfix 89 chrony 998 sshd 74 tom 1000
- 像df -h 这种不规则输出格式用awk能更方便显示
[root@hostnamectl c]# df -h | awk '{print $2}' 容量 39G 901M 912M 912M 912M 1014M 19G 183M
- OFS 输出分隔符
[root@hostnamectl c]# awk 'BEGIN{FS=":";OFS=":"} {print $1,$3}' passwd root:0 bin:1 daemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 operator:11 games:12 ftp:14 nobody:99 systemd-network:192 dbus:81 polkitd:999 postfix:89 chrony:998 sshd:74 tom:1000
高级命令之sed
加-i才会修改源文件,不加-i也可生成新文件 >[new filename]
- -n '[n][n]p'只输出打印n行或包含n的行
[root@hostnamectl c]# sed -n '/root/p' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
- 输出答应第3到5行
[root@hostnamectl c]# sed -n '3,5p' passwd daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
- 删除第十行后面所有
[root@hostnamectl y]# sed -i '10,$d' passwd [root@hostnamectl y]# cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
- 修改替换所有root为runtime(只是查看效果没有改动文件)
[root@hostnamectl y]# cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin [root@hostnamectl y]# cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin [root@hostnamectl y]# sed 's/root/runtime/g' passwd runtime:x:0:0:runtime:/runtime:/bin/bash
- 只替换第一个root为runtime
[root@hostnamectl y]# sed 's/root/runtime/1' passwd runtime:x:0:0:root:/root:/bin/bash
- 只替换第二个root为runtime
[root@hostnamectl y]# sed 's/root/runtime/2' passwd root:x:0:0:runtime:/root:/bin/bash
文本过滤命令grep
grep命令根据正则表达式搜索文本,并将符合正则表达式的文本显示出来。
默认使用基本正则表达式来过滤文本。Usage: grep [OPTION]... PATTERN [FILE]...
常用的OPTION:
-i:忽略大小写
--color:匹配到的内容高亮显示
-v:显示没有被正则表达式匹配到的内容
-o:只显示被正则表达式匹配到的内容
-E:使用扩展正则表达式
-q:静默模式,不输出任何信息
-A #:此处的#必须是数字。被正则匹配到的内容以及其后面#行的内容都显示出来
-B #:此处的#必须是数字。被正则匹配到的内容以及其前面#行的内容都显示出来
-C #:此处的#必须是数字。被正则匹配到的内容及其前后各#行的内容都显示出来[root@hostnamectl y]# grep 'var' passwd adm:x:3:4:adm:/var/adm:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
- -i 忽略大小写
[root@hostnamectl y]# grep -i 'var' passwd adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/Var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
- -color默认显示
[root@hostnamectl y]# alias alias cdnet='cd /etc/sysconfig/network-scripts' alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto'
- -v取反
[root@hostnamectl y]# grep -v 'var' passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/Var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
- -E同时过滤多个条件
[root@hostnamectl c]# grep -E 'root|nologin' passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- -q 静默输出
[root@hostnamectl c]# grep -Eq 'root|nologin' passwd [root@hostnamectl c]# echo $? //echo $? 表示上一条命令是否成功 ==0 就是成功 ,其他值表示不成功 0
- -B [n] 关键字上n行
[root@hostnamectl y]# grep -B 3 'halt' passwd lp:x:4:7:lp:/Var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
- -B [n] 关键字下n行
[root@hostnamectl c]# grep -A 3 'halt' passwd halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin
- C [n] 关键字上下n行
[root@hostnamectl y]# grep -C 1 'halt' passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
文件查找命令find
语法:find [OPTION]... 查找路径 查找标准 查找到以后的处理动作
- 查找路径:默认为当前目录
查找标准:
-name filename:对文件名精确匹配,支持通配符
-iname filename:文件名匹配时不区分大小写
-regex pattern:基于正则表达式进行文件名匹配
-user:查找某用户的所有文件
-group:查找某组的所有文件
-uid:根据UID进行查找
-gid:根据GID进行查找
-nouser:查找没有拥有者的文件
-nogroup:查找没有属组的文件
-type:根据文件类型进行查找
-size:根据文件大小进行查找。如1k、1M、+10k、+10M、-1k、-10M+表示大于,-表示小于
-mtime:根据修改时间查找
+5:表示查找5天以前修改的文件
-5:表示查找5天以内修改的文件-atime:根据访问时间查找
-ctime:根据改变时间查找
-perm mode:根据权限精确查找
-perm –mode:文件权限能完全包含此mode时才符合条件
-perm /mode:9位权限中有任何一位权限匹配都视为符合查找条件
多条件组合使用:
-a 并且-o 或者
-not 取反
! 取反处理动作:默认动作是显示到屏幕上
-print:打印到屏幕上
-ls:类似ls –l的形式显示每一个文件的详细信息
-delete:删除查找到的文件
-fls /path/to/somefile:将查找到的所有文件的长格式信息保存至指定文件中
-ok COMMAND {} ;:对查找到的所有文件执行COMMAND,每次操作都需要用户确认
-exec COMMAND {} ;:对查找到的所有文件执行COMMAND,操作不需要确认
xargs:通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可- 当前路径用文件名查找
[root@hostnamectl ~]# find tem tem
- -name filename:当前路径用文件名查找,对文件名精确匹配,支持通配符
[root@hostnamectl ~]# find -name tem ./tem
- 指定路径超查找
[root@hostnamectl ~]# find /etc/ -name tmp /etc/selinux/tmp
- -iname filename:文件名匹配时不区分大小写
[root@hostnamectl ~]# find -iname passwd ./y/passwd
- -user:查找某用户的所有文件
[root@hostnamectl ~]# find -user root -name passwd ./y/passwd
[root@hostnamectl ~]# find -user root . ./.bash_logout ./.bash_profile ./.cshrc ./.tcshrc ./anaconda-ks.cfg ./.bash_history ./.bashrc ./qwq ./a ./a/qwq ./a/c ./a/bbb ./a/a ./bbb ./c ./wqw ./cc ./abc ./vbn ./acb ./20190910 ./.lesshst ./tem ./y ./y/abc ./y/aaa ./y/bbb ./y/passwd
[root@hostnamectl ~]# find / -user root /mnt/addons/ResilientStorage/repodata/4e6f842c87e87b11e3ac4950ef74b8066660bb64ac386cc6f0b58688676bd707-other.sqlite.bz2 /mnt/addons/ResilientStorage/repodata/5b1866be7d672aa3312ac506d752457999c61f76407fa00bfc8ffadbbe3acc7a-productid.gz /mnt/addons/ResilientStorage/repodata/5b501f162c7d546a3d92778f0d55382994b5a7b9da0122dfd862cf63310cae7e-primary.xml.gz /mnt/addons/ResilientStorage/repodata/660afe5874f40e7f8b336f677050024d2b056ad8fef7b83028eb48943870f6e1-comps-Server-ResilientStorage.x86_64.xml /mnt/addons/ResilientStorage/repodata/89eee7141f44a87ee60b6c83bb821c89d65a94f1ce7b27a0d7f7ff49b82af6d3-comps-Server-ResilientStorage.x86_64.xml.gz /mnt/addons/ResilientStorage/repodata/94da878a802a92f6f6147c43e892eabe2ecaf58fe475539ead8ee3bad4b6222e-primary.sqlite.bz2 /mnt/addons/ResilientStorage/repodata/TRANS.TBL
- -nouser:查找没有拥有者的文件
[root@hostnamectl /]# find -nouser
- -nogroup:查找没有属组的文件
[root@hostnamectl /]# find -nogroup
- -type:根据文件类型进行查找
[root@hostnamectl /]# find -type b ./dev/dm-2 ./dev/dm-1 ./dev/dm-0 ./dev/sr0 ./dev/sda2 ./dev/sda1 ./dev/sda
- -size:根据文件大小进行查找。如1k、1M、+10k、+10M、-1k、-10M
+表示大于,-表示小于
[root@hostnamectl ~]# find -size +1k ./anaconda-ks.cfg ./.bash_history ./vbn
[root@hostnamectl ~]# find -size -1k ./a/bbb ./bbb ./20190910
- -mtime:根据修改时间查找
+5:表示查找5天以前修改的文件
-5:表示查找5天以内修改的文件
[root@hostnamectl ~]# find -mtime -1 . ./.bash_history ./a ./a/a ./tem ./y ./y/abc ./y/aaa ./y/bbb ./y/passwd
- -a并且,多条命令
[root@hostnamectl c]# find -mtime -5 -a -name a ./a
- -print:打印到屏幕上
- -ls:类似ls –l的形式显示每一个文件的详细信息
[root@hostnamectl ~]# find -type f -name anaconda-ks.cfg ./anaconda-ks.cfg ··· ···bash [root@hostnamectl ~]# find -type f -name anaconda-ks.cfg -print ./anaconda-ks.cfg
[root@hostnamectl ~]# find -type f -name anaconda-ks.cfg -ls 100663362 4 -rw------- 2 root root 1451 9月 6 22:57 ./anaconda-ks.cfg
- -fls :将查找到的所有文件的长格式信息保存至指定文件中
[root@hostnamectl c]# find -type f -fls acc.txt [root@hostnamectl c]# ls a acc.txt passwd [root@hostnamectl c]# rm -f acc [root@hostnamectl c]# cat acc.txt 100663381 4 -rw-r--r-- 1 root root 883 9月 15 15:01 ./passwd 100663405 0 -rw-r--r-- 1 root root 0 9月 15 17:41 ./acc.txt [root@hostnamectl c]#
- -delete:删除查找到的文件
[root@hostnamectl ~]# ls 20190910 a abc acb anaconda-ks.cfg bbb c cc qwq tem vbn wqw y [root@hostnamectl ~]# find . -name tem -type d -delete [root@hostnamectl ~]# ls 20190910 a abc acb anaconda-ks.cfg bbb c cc qwq vbn wqw y
- -exec COMMAND {} ;:对查找到的所有文件执行COMMAND,操作不需要确认
[root@hostnamectl ~]# ls 20190910 a abc acb anaconda-ks.cfg bbb c cc qwq vbn wqw y [root@hostnamectl ~]# find . a -type d -exec rm -rf {} \; rm: refusing to remove "." or ".." directory: skipping "." find: ‘./a’: 没有那个文件或目录 find: ‘./c’: 没有那个文件或目录 find: ‘./y’: 没有那个文件或目录 find: ‘a’: 没有那个文件或目录 [root@hostnamectl ~]# ls 20190910 abc acb anaconda-ks.cfg bbb cc qwq vbn wqw [root@hostnamectl ~]#
[root@hostnamectl ~]# ls a anaconda-ks.cfg c [root@hostnamectl ~]# find -name a -type d -exec mv {} c \; find: ‘./a’: 没有那个文件或目录 [root@hostnamectl ~]# ls anaconda-ks.cfg c [root@hostnamectl ~]# ls c a
- -ok COMMAND {} ;:对查找到的所有文件执行COMMAND,每次操作都需要用户确认
[root@hostnamectl ~]# ls anaconda-ks.cfg ccc [root@hostnamectl ~]# find . ccc -type d -ok rm -rf {} \; < rm ... . > ? y rm: refusing to remove "." or ".." directory: skipping "." < rm ... ./ccc > ? y find: ‘./ccc’: 没有那个文件或目录 find: ‘ccc’: 没有那个文件或目录 [root@hostnamectl ~]# ls anaconda-ks.cfg
- xargs:通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可
[root@hostnamectl c]# ls a acc.txt passwd [root@hostnamectl c]# find -type d | xargs rm -rf rm: refusing to remove "." or ".." directory: skipping "." [root@hostnamectl c]# ls acc.txt passwd