6.重定向(快樂的Linux命令行)

1.指令英文解釋

cat Concatenate files
sort Sort lines of text
uniq  Report or omit repeated lines
grep Print lines matching a pattern
wc Print newline, word, and byte counts for each file
head Output the first part of a file
tail Output the last part of a file
tee Read from standard input and write to standard output and files

 

2.指令詳解

> 將標準輸出重定向到除屏幕以外的另一個文件。 A > B
>> 重定向結果追加到文件內容後面 A >> B
cat 連接文件 cat [file]
|

管道線,用來作爲過濾器

A | B

uniq

報道或忽略重複行

A | B | uniq

wc

打印行數、字數和字節數

wc A

grep 

打印匹配行

A | grep B

head / tail

打印文件開頭部分/結尾部分

head -n 5 B

tee

從 Stdin 讀取數據,並同時輸出到 Stdout 和文件

A | tee B 

3.指令常用技巧

> hello.txt  清空文件內容       A  0/1/2> B 其中2代表將錯誤信息輸入到B      

A > B 2>&1 等價於 A > B 2 > B 等價於 A &> B  將A的信息以及錯誤信息都輸入到B

cat(用於連接多個文件形成一個完整的文件或從標準輸入讀入數據 比如 cat movie.mpeg.0* > movie.mpeg or 直接 cat)

注意:ctrl + d 等價於 EOF , 表示在操作系統中表示資料源無更多的資料可讀取,用來結束讀取輸入。

A | -d uniq 可以看到重複的是哪些

wc -l A 只能看見A的line行數

head -f B 可以實時監控文件是否發生改變

4.命令實例

[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt
[me@linuxbox ~]$ ls -l ls-output.txt
-rw-rw-r-- 1   me   me    167878 2008-02-01 15:07 ls-output.txt
[me@linuxbox ~]$ less ls-output.txt
[me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt
ls: cannot access /bin/usr: No such file or directory
me@linuxbox ~]$ ls -l ls-output.txt
-rw-rw-r-- 1 me   me    0 2008-02-01 15:08 ls-output.txt
[me@linuxbox ~]$ > ls-output.txt
[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt
[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt
[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt
[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt
[me@linuxbox ~]$ ls -l ls-output.txt
-rw-rw-r-- 1 me   me    503634 2008-02-01 15:45 ls-output.txt
[me@linuxbox ~]$ ls -l /bin/usr 2> ls-error.txt
[me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt 2>&1
[me@linuxbox ~]$ ls -l /bin/usr &> ls-output.txt
[me@linuxbox ~]$ ls -l /bin/usr 2> /dev/null


[me@linuxbox ~]$ cat ls-output.txt
[me@linuxbox ~]$ cat movie.mpeg.0* > movie.mpeg
[me@linuxbox ~]$ cat
[me@linuxbox ~]$ cat
The quick brown fox jumped over the lazy dog.
[me@linuxbox ~]$ cat
The quick brown fox jumped over the lazy dog.
The quick brown fox jumped over the lazy dog.
[me@linuxbox ~]$ cat > lazy_dog.txt
The quick brown fox jumped over the lazy dog.
[me@linuxbox ~]$ cat lazy_dog.txt
The quick brown fox jumped over the lazy dog.
[me@linuxbox ~]$ cat < lazy_dog.txt
The quick brown fox jumped over the lazy dog.


[me@linuxbox ~]$ ls -l /usr/bin | less
[me@linuxbox ~]$ ls /bin /usr/bin | sort | less


[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq | less
[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq -d | less


[me@linuxbox ~]$ wc ls-output.txt
7902 64566 503634 ls-output.txt

[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq | wc -l
2728

[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq | grep zip
bunzip2
bzip2
gunzip
...


[me@linuxbox ~]$ head -n 5 ls-output.txt
total 343496
...
[me@linuxbox ~]$ tail -n 5 ls-output.txt
...
[me@linuxbox ~]$ ls /usr/bin | tail -n 5
znew
...
[me@linuxbox ~]$ tail -f /var/log/messages
Feb 8 13:40:05 twin4 dhclient: DHCPACK from 192.168.1.1
....

[me@linuxbox ~]$ ls /usr/bin | tee ls.txt | grep zip
bunzip2
bzip2
....

 

發佈了59 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章