【SRE笔记 2022.8.19】
cp命令
- 语法 cp 源文件 目标文件
- r 复制目录
- p 保持属性
- a (dpr)d代表设备
- t 把源 和目标
touch命令
- 多次重复新建,只会修改文件的时间戳,即最近访问的时间
[root@aaa ~]# touch a.txt
[root@aaa ~]# touch a.txt
[root@aaa ~]# touch a.txt
[root@aaa ~]# ll a.txt
-rw-r--r--. 1 root root 0 Aug 19 18:59 a.txt
命令 tr
- 按字符替换,从标准输入接收数据,把读到的a替换为A
[root@aaa ~]# cat bbb.txt
a
b
c
d
[root@aaa ~]# tr "a" "A" <bbb.txt
A
b
c
d
xargs (从标准输入接收数据)
-n 分组
[root@aaa ~]# xargs -n 2 < bbb.txt 分为两组
a b
c d
别名 alias/unalias
alias
- 提高输入效率,防止误删数据
- 引号内的命令能执行且在英文状态下输入
[root@aaa ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@aaa ~]# alias net='cat -n aaa.txt'
[root@aaa ~]# net
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@aaa ~]# alias rm="呵呵"
[root@aaa ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias net='cat -n aaa.txt'
alias rm='呵呵' #修改处
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde
别名永久生效
- 全局【环境变量】文件 /etc/profile /etc/bashrc(推荐修改此文件,优先级高于前一个文件)
- 用户【环境变量】文件 ~/.bash_profile ~/.bashrc
- alias net=“cat aaa.txt” 加入上述环境变量文件文件中。
[root@aaa ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias net='cat -n aaa.txt' #### 自定义的
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
- 执行命令时,【别名】优先于【命令】
已使用别名的命令如何不用别名
[root@aaa ~]# /usr/bin/rm a.cap
- 1 使用命令全路径输入命令,会忽略系统设置的别名
root@aaa ~]# which rm #查询路径
/usr/bin/rm
- 2 在命令开头使用反斜线
取消别名unalias
[root@aaa ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias net='cat -n aaa.txt'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@aaa ~]# unalias net
重定向
标准输出重定向
- 用数字1(1可省略)表示。 1>或>,数据流向是向右。
[root@aaa ~]# echo aaa > bbb.txt #输出aaa,清空bbb.txt,把aaa写入到bbb.txt
[root@aaa ~]# cat bbb.txt
aaa
清空文件 用>
[root@aaa ~]# >bbb.txt #也可用 cat /etc/null > bbb.txt
[root@aaa ~]# cat bbb.txt
标准错误输出重定向
- 用数字2表示。2>,只接收执行命令的报错
[root@aaa ~]# ls #显示结果如下
a a10 a12 a3 a5 a7 a9 aaa.txt a.txt b.txt filter_file
a1 a11 a2 a4 a6 a8 aa anaconda-ks.cfg bbb.txt file.pcap
[root@aaa ~]# ls > ccc.txt #将ls的输出结果西写入到ccc.txt
[root@aaa ~]# ls 2> ccc.txt #此时执行命令并无报错,也相当于清空ccc.txt
a a10 a12 a3 a5 a7 a9 aaa.txt a.txt b.txt file.pcap
a1 a11 a2 a4 a6 a8 aa anaconda-ks.cfg bbb.txt ccc.txt filter_file
- 演示命令
[root@aaa ~]# catt 2> ccc.txt
[root@aaa ~]# cat ccc.txt
-bash: catt: command not found
标准输入重定向
- 用数字0表示。用的较少。0<或<,数据、流朝向左。
标准(追加)输出重定向
-
或1>>,数据流也是朝向右。
[root@aaa ~]# echo '123'>> bbb.txt
[root@aaa ~]# cat bbb.txt
a
b
c
d
123
标准错误(追加)输出重定向
- 2>>,数据流向右。
[root@aaa ~]# lss 2>> bbb.txt
[root@aaa ~]# cat bbb.txt
a
b
c
d
123
-bash: lss: command not found
标准输入(追加)重定向
- 0<<或<<
[root@aaa ~]# cat << eof
> aaa
> bbb
> bbbb
> eof
aaa
bbb
bbbb
- 常用举例:把输出内容写入k.txt文件
[root@aaa ~]# cat > k.txt <<eof
> aaa
> bbb
> ccc
> dd
> eof
[root@aaa ~]# cat k.txt
aaa
bbb
ccc
dd
特殊情况:单文件同时收集错误和正确的信息。
方法1
[root@aaa ~]# lss >> zzz.txt 2>> zzz.txt
方法2
[root@aaa ~]# lss &>> zzz.txt
方法3
[root@aaa ~]# lss 1>> zzz.txt 2>&1 ##先把正确的1输入zzz.txt,错误的2也重定向到1。
uniq 命令 去重
- uniq + 文件 把相邻的相同行去重
[root@aaa ~]# cat shuzi.txt
1
1
22
3
4
55
55
55
[root@aaa ~]# uniq shuzi.txt
1
22
3
4
55
- uniq -c 文件 计出重复的次数且去重
[root@aaa ~]# uniq -c shuzi.txt
2 1
1 22
1 3
1 4
3 55
sort 命令 排序
- sort 和 uniq配合使用实现整个文件去重
sort +文件
[root@aaa ~]# sort shuzi.txt
1
1
22
3
4
55
55
55
-n 数字排序
[root@aaa ~]# cat ip.txt
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.1
10.2.0.2
10.0.3.4
10.0.0.5
10.6.5.5
120.0.0.4
10.0.0.15
10.0.0.22
[root@aaa ~]# sort -n ip.txt
10.0.0.1
10.0.0.1
10.0.0.15
10.0.0.2
10.0.0.22
10.0.0.3
10.0.0.5
10.0.3.4
10.2.0.2
10.6.5.5
120.0.0.4
-t 指定分隔符
-k 指定列
-r 倒叙
[root@aaa ~]# sort -n -t . -k 4 ip.txt 正序
10.0.0.1
10.0.0.1
10.0.0.2
10.2.0.2
10.0.0.3
10.0.3.4
120.0.0.4
10.0.0.5
10.6.5.5
10.0.0.15
10.0.0.22
[root@aaa ~]# sort -rn -t . -k 3 ip.txt
10.6.5.5
10.0.3.4
10.0.0.5
120.0.0.4
10.0.0.3
10.0.0.22
10.2.0.2
10.0.0.2
10.0.0.15
10.0.0.1
10.0.0.1
管道 | (能不用就不用,效率较低)
筛选文本数据流
[root@aaa ~]# cat bbb.txt
a
b
c
d
123
-bash: lss: command not found
[root@aaa ~]# cat bbb.txt | grep 123
123
[root@aaa ~]# grep 123 bbb.txt
123
[root@aaa ~]#
stat 命令 查看文件状态
[root@aaa ~]# stat bbb.txt
File: ‘bbb.txt’
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33816621 Links: 1 #设备
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 权限
Context: unconfined_u:object_r:admin_home_t:s0 #环境
Access: 2022-08-19 23:05:46.821252205 +0800 # 最近访问,浏览,针对内容
Modify: 2022-08-19 23:05:45.009252175 +0800 # 最近更改时间,针对内容
Change: 2022-08-19 23:05:45.009252175 +0800 # 最近改动,状态改变,属性改变。
Birth: - #创建时间