du -sh yourDirectory/
2016年6月15日 星期三
Linux Shell :將結果同時重導到File和stdout
故事是這樣的,我寫了一個shell script,裡面執行到哪裡就echo出來(也就是log..)以便我debug,某天我想要把run shell script 的log重導到檔案,但是我卻想同時想看"熱騰騰"的message echo到螢幕上。
例如: $ ./myShellScript.sh > log <--此時就是將stdout重導到log這個檔案
我們知道stdout最終會導向螢幕,問題就來了,因為 ">" 符號本來就是將stdout的重導到檔案上,所以很自然螢幕就不會顯示任何東西啦,於是我想到
$ ./myShellScript.sh > log ; cat log
就這樣利用cat指令將log再印出來一次,但是這是shell script都做完了,最後才印出來,好像還不夠給力,因為我想要在script執行的同時,可以立即看到echo出來的message而且又能重導到檔案。
登登,這裡介紹一個好用的指令 tee,它就能達到我想要的需求囉,
例: $ ./myShellScript.sh | tee log
使用pipe接起來給tee指令,後面則是你要重導到的檔案名
假設想要連stderr也一起併入,則
例: $ ./myShellScript.sh 2>&1 | tee log
參考資料:how to redirect output to a file and stdout
例如: $ ./myShellScript.sh > log <--此時就是將stdout重導到log這個檔案
我們知道stdout最終會導向螢幕,問題就來了,因為 ">" 符號本來就是將stdout的重導到檔案上,所以很自然螢幕就不會顯示任何東西啦,於是我想到
$ ./myShellScript.sh > log ; cat log
就這樣利用cat指令將log再印出來一次,但是這是shell script都做完了,最後才印出來,好像還不夠給力,因為我想要在script執行的同時,可以立即看到echo出來的message而且又能重導到檔案。
登登,這裡介紹一個好用的指令 tee,它就能達到我想要的需求囉,
例: $ ./myShellScript.sh | tee log
使用pipe接起來給tee指令,後面則是你要重導到的檔案名
假設想要連stderr也一起併入,則
例: $ ./myShellScript.sh 2>&1 | tee log
參考資料:how to redirect output to a file and stdout
2016年6月13日 星期一
Linux shell:刪除兩個檔案相同的地方(How to find and delete duplicate lines in two files in vi editor or linux?)
假設有兩的檔案A,B
A:
11111
22222
33333
44444
B:
11111
33333
55555
A1: (= A - A∩B)
22222
44444
B1: (= B - A∩B)
55555
A:
11111
22222
33333
44444
B:
11111
33333
55555
運算後產生A1: (= A - A∩B)
22222
44444
B1: (= B - A∩B)
55555
2016年6月7日 星期二
Linux Shell Diff: 比較兩個file的差異 並output 差異的部分
diff --changed-group-format='%>' --unchanged-group-format='' file1 file2
訂閱:
文章 (Atom)