1.重復用touch命令創建同一份文件,會修改文件的時間戳。
alias命令:
別名
查看已有別名:alias
[root@oldboy ~]# 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'
設置別名:alias net=‘cat /etc/sysconfig/network-scripts/ifcfg-eth0’
別名優先于命令
如何不使用別名?
(1)使用絕對路徑
用which查找命令的絕對路徑
[root@oldboy ~]# which cp
alias cp='cp -i'/usr/bin/cp
[root@oldboy ~]# /bin/cp test.txt ./a.txt
(2)在命令前加上反斜線
[root@oldboy ~]# \ll
-bash: ll: 未找到命令
[root@oldboy ~]# \ls -l
總用量 12
-rw-------. 1 root root 1438 11月 26 21:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 3525 12月 18 19:39 a.txt
-rw-r--r--. 1 root root 3525 11月 29 20:24 test.txt
(3)取消別名:
unalias + 命令
[root@oldboy ~]# unalias net
[root@oldboy ~]# net
-bash: net: 未找到命令
別名永久生效
全局環境變量文件:
/etc/profile
/etc/bashrc
用戶環境變量文件:
~/.bash_profile
~/.bashrc
將alias別名命令寫入全局環境變量就可永久生效
重定向
1.標準輸入重定向,用數字0表示
< 或 0< ,標準輸入
[root@oldboy ~]# cat a.txt
1
2
3
4
5
[root@oldboy ~]# tr "1" "A" < a.txt #按字符替換
A
2
3
4
5
可以按范圍替換:
xargs:分組,按照個數分組
[root@oldboy ~]# cat a.txt
1
2
3
4
5
[root@oldboy ~]# xargs -n 2 < a.txt
1 2
3 4
5
2.標準輸出重定向,用數字1表示
> 或 1> ,將正確的信息重定向,會清空文件
[root@oldboy ~]# echo 1234675 1> a.txt
[root@oldboy ~]# cat a.txt
1234675
標準輸出追加:>>或1>>
清空文件命令:
[root@oldboy ~]# > a.txt
[root@oldboy ~]# cat a.txt -n
[root@oldboy ~]# cat /dev/null > a.txt
[root@oldboy ~]# cat a.txt -n
3.標準錯誤輸出重定向,用數字2表示
重定向錯誤信息:
[root@oldboy ~]# abc 2> a.txt
[root@oldboy ~]# cat -n a.txt 1 -bash: abc: 未找到命令
標準錯誤輸出追加:2>>
4.大段內容非交互式編輯
[root@oldboy ~]# cat > a.txt << EOF
> 123
> 123
> 23
> EOF
[root@oldboy ~]# cat a.txt
123
123
23
cat的標準輸出內容會重定向至a.txt文件,而cat的標準輸入是EOF之間的內容。
5. 將正確信息和錯誤信息寫入同一個文件
(1)
[root@oldboy ~]# echo 123 >> a.txt 2>> a.txt
[root@oldboy ~]# cat a.txt
123
[root@oldboy ~]# cho 123 >> a.txt 2>> a.txt
[root@oldboy ~]# cat a.txt
123
-bash: cho: 未找到命令
(2)
[root@oldboy ~]# echo 123 &>> a.txt
[root@oldboy ~]# cho 123 &>> a.txt
[root@oldboy ~]# cat a.txt
123
-bash: cho: 未找到命令
(3)標準錯誤和標準輸出一樣
[root@oldboy ~]# echo 123 >> a.txt 2>&1
[root@oldboy ~]# cho 123 >> a.txt 2>&1
[root@oldboy ~]# cat a.txt
123
-bash: cho: 未找到命令
uniq命令-去重
cat > a.txt << EOF
10.0.0.1
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.1
10.0.0.1
EOF
uniq + 文件:把相鄰的相同行去重
[root@oldboy ~]# uniq a.txt
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.1
uniq -c +文件:去重并且計數
[root@oldboy ~]# uniq a.txt -c5 10.0.0.11 10.0.0.21 10.0.0.31 10.0.0.46 10.0.0.1
sort 排序
按字典序列排序
[root@oldboy ~]# sort a.txt
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
-n:按數字排序
-t:指定分隔符
-k:指定哪一列
-r:倒序排序
[root@oldboy ~]# sort -n -t . -k 4 a.txt
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.14
10.0.0.16
10.0.0.23
1. 企業面試題:查看重復IP,按照次數排序
[root@oldboy ~]# sort -n -t . -k 4 a.txt -r | uniq -c | sort -nr -t " " -k 111 10.0.0.11 10.0.0.41 10.0.0.31 10.0.0.231 10.0.0.21 10.0.0.161 10.0.0.14