今天莫名其妙會自由泳了,好開心!
1. >
">" 用于定向輸出到文件,如果文件不存在,就會創(chuàng)建文件;如果文件存在,會源文件內(nèi)容清空,再寫入。
$ echo Hello World > hello.txt # 將'Hello World'寫入hello.txt
$ echo ''>hello.txt #清空hello.txt文件內(nèi)容
2. >>
跟">"不同的是,">>"符并不會清空原文件內(nèi)容,而是在文件結(jié)尾追加。
$ echo Hello World > hello.txt # 將'Hello World'寫入hello.txt
$ echo My name is Leo >> hello.txt # 將''My name is Leo" 追加在hello.txt文件最后
3. |
"|"管道符將前面的輸出信息作為輸入信息交給下一個命令繼續(xù)處理。
$ ls -a /etc | less # 用less命令查看 /etc目錄下的文件信息
$ px -aux | grep nginx # 顯示含有'nginx'的進(jìn)程
4. tee
'tee'雙重定向,存到文件/設(shè)備的同時也輸出到屏幕,可以不打斷當(dāng)前操作。
$ ls | tee save_ls.txt
5. cut
'cut'將一段信息的某一段切割出來,處理信息的單位是行。
$ cat hello.txt # 現(xiàn)有的文件
hello world
My name is Leo
$ cut -c 2 hello.txt # 切割出每行的第二個字符
e
y
$ cut -c 2-4 hello.txt # 切割出每行的第二到第四個字符
ell
y n
$ cut -f 2 -d ' ' hello.txt # 切割出用空格分割的每行第2段, -f 以段分割, -d 指定分割符,默認(rèn)為TAB
world
name
6.paste
'paste' 跟cat命令相似,不同的是paste可以讓所有行合并成一行輸出。
$ cat hello.txt # 現(xiàn)有的文件
hello
world
$ cat name.txt # 現(xiàn)有的文件
My
name
is
Leo
$ paste hello.txt # 現(xiàn)有的文件
hello
world
$ paste name.txt # 現(xiàn)有的文件
My
name
is
Leo
$ paste -s hello.txt # 合并成一行輸出hello.txt的內(nèi)容,默認(rèn)以TAB分割
hello world
$ paste -s -d ',' name.txt # 以逗號分割
My,name,is,Leo
$ paste -s -d ' ' hello.txt name.txt # 同時操作多個文件也可以
hello world
My name is Leo
7. head
'head' 顯示文件/文本最前面的信息
$ head -n 15 /var/log/syslog # 顯示前15行
$ head -c 15 /var/log/syslog # 顯示前15個字符
8. tail
'tail' 顯示文件/文本最后面的信息
$ tail -n 10 /var/log/syslog # 顯示最后10行
$ tail -f /var/log/syslog # 持續(xù)輸出最新寫入的信息(非常實(shí)用)
9. expand
'expand' 將TAB轉(zhuǎn)化成空格
$ expand -t 4 sample.txt > result.txt # 將文件里的每個tab用4個空格代替
10. join
'join' 將兩個文件當(dāng)中有相同數(shù)據(jù)的那一行加在一起。順序要一致。
$ cat file1.txt
1 Loe
2 Hh
3 Yy
$ cat file3.txt
1 100
2 99
3 88
$ join file1.txt file3.txt
1 Loe 100
2 Hh 99
3 Yy 88
$ cat file1.txt
1 Loe
2 Hh
3 Yy
$ cat file4.txt
100 1
99 2
88 3
$ join -1 1 -2 2 file1.txt file4.txt
1 Loe 100
2 Hh 99
3 Yy 88
11. split
'split'將一個文件分隔成若干個文件。
$ ls
file1.txt
$ cat file1.txt
1 Loe
2 Hh
3 Yy
$ split -l 1 file1.txt # 已一行為單位分隔file1.txt文件
$ ls # 分隔出來的文件默認(rèn)已x**命名
file1.txt xaa xab xac
$ cat xaa
1 Loe
$ cat xab
2 Hh
$ cat xac
3 Yy
11. sort
'sort'用于排序。
$ sort file1.txt
$ sort -r file1.txt # 倒序
12. uniq
'uniq' 用于去重。
$ cat name.txt
Leo
Leo
Hh
Hh
Yy
Yy
Ky
$ uniq name.txt # 去重顯示,重復(fù)的顯示一次
Leo
Hh
Yy
Ky
$ uniq -c name.txt # 統(tǒng)計每個出現(xiàn)的次數(shù)
2 Leo
2 Hh
2 Yy
1 Ky
$ uniq -u name.txt # 只顯示不重復(fù)的
Ky
特別要注意的是:uniq命令只能去重連續(xù)的數(shù)據(jù)!
$ cat name.txt
Leo
Hh
Leo
Hh
Yy
Ky
Yy
$ uniq -c name.txt
1 Leo
1 Hh
1 Leo
1 Hh
1 Yy
1 Ky
1 Yy
$ uniq name.txt
Leo
Hh
Leo
Hh
Yy
Ky
Yy
所以去重操作一般都要結(jié)合上面介紹的 '|' 和 'sort' 命令來處理。
$ sort name.txt | uniq
Hh
Ky
Leo
Yy
13. wc
'wc'(word count)用于統(tǒng)計文本信息。-l 行數(shù) -w 單詞數(shù) -c字節(jié)數(shù)
$ wc /etc/passwd # 行數(shù) 單詞數(shù) 字節(jié)數(shù)
29 40 1457 /etc/passwd
$ wc -l /etc/passwd # 統(tǒng)計行數(shù)
29 /etc/passwd
14. grep
'grep' 文本匹配。
$ cat test # 現(xiàn)有的文件
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
Leo:x:1000:100:,,,:/home/Leo:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/Leo:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@Loe:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
$ grep root test
root:x:0:0:root:/root:/bin/bash # 匹配含有root的行
$ cat test | grep -n root # 匹配含有root的行,并輸出行號
1:root:x:0:0:root:/root:/bin/bash
$ cat test | grep '^\(root\|http\)' # 匹配以 root或http開頭的行
root:x:0:0:root:/root:/bin/bash
http:x:33:33::/srv/http:/bin/false
15. tr
'tr'(translate) 從標(biāo)準(zhǔn)輸入中替換、縮減和/或刪除字符,并將結(jié)果寫到標(biāo)準(zhǔn)輸出。
$ echo Leo| tr a-z A-Z # 小寫轉(zhuǎn)成大寫
LEO
$ echo Leo | tr 'e' 'a' # e 改成 a
Lao
$ echo Leo | tr -d 'e' # 刪除 d
Lo
$ echo Leeoe | tr -s [a-zA-Z] # 刪除重復(fù)的字母
Leoe