1. 程式人生 > >Ubuntu的基本用法:常用指令,控制流程和重定向

Ubuntu的基本用法:常用指令,控制流程和重定向

常用指令

接下來我將舉例闡述以下常用的基本指令: /!\ 記住要正確的按照下面寫的輸入,後面還得帶上回車鍵!

查詢檔案列表

[email protected]:~ $ ls
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT

ls命令預設狀態下將按首字母升序列出你當前資料夾下面的所有內容,但這樣直接執行所得到的資訊也是比較少的,通常它可以結合以下這些引數執行以查詢更多的資訊:

  • ls / 將列出根目錄'/'下的檔案清單.如果給定一個引數,則命令列會把該引數當作命令列的工作目錄。換句話說,命令列不再以當前目錄為工作目錄。
  • ls -l 將給你列出一個更詳細的檔案清單.
  • ls -a 將列出包括隱藏檔案(以.開頭的檔案)在內的所有檔案.
  • ls -h 將以KB/MB/GB的形式給出檔案大小,而不是以純粹的Bytes.

查詢當前所在目錄:

[email protected]:~ $ pwd
/home/dud

進入其他目錄(用命令cd)

[email protected]:~ $ pwd
/home/dud
[email protected]:~ $ cd /root/
[email protected]:/root $ pwd
/root

上面例子中,當前目錄原來是/home/dud,執行cd /root/之後再執行pwd可以發現,當前目錄已經改為/root了


在螢幕上輸出字元:

[email protected]:~ $ echo "Hello World"
Hello World

這是一個很有用的命令,它可以在螢幕上輸入你指定的引數(""號中的內容),當然這裡舉的這個例子中它沒有多大的實際意義,但隨著你對LINUX指令的不斷深入,就會發現它的價值所在。

顯示檔案內容:

[email protected]:~ $ cat file1.txt
Roses are red.
Violets are blue,
and you have the bird-flue!

cat這個命令可以用來在終端上顯示txt文字檔案的內容。如上例輸出的這首“詩”;

複製檔案:

[email protected]:~ $ cp file1.txt file1_copy.txt
[email protected]:~ $ cat file1_copy.txt
Roses are red.
Violets are blue,
and you have the bird-flue!

移動檔案:

[email protected]:~ $ ls
file1.txt
file2.txt
[email protected]:~ $ mv file1.txt new_file.txt
[email protected]:~ $ ls
file2.txt
new_file.txt

需要注意的是,在命令操作時系統基本上不會給你什麼提示,當然,絕大多數的命令可以通過加上一個引數 -v來要求系統給出執行命令的反饋資訊;

[email protected]:~ $ mv -v file1.txt new_file.txt
`file1.txt' -> `new_file.txt'

加上-v引數後,系統就會輸出操作提示

`file1.txt' -> `new_file.txt'

建立一個空文字檔案:

[email protected]:~ $ ls
file1.txt
[email protected]:~ $ touch tempfile.txt
[email protected]:~ $ ls
file1.txt
tempfile.txt

建立一個目錄

[email protected]:~ $ ls
file1.txt
tempfile.txt
[email protected]:~ $ mkdir test_dir
[email protected]:~ $ ls
file1.txt
tempfile.txt
test_dir

刪除檔案/目錄

[email protected]:~ $ ls -p
file1.txt
tempfile.txt
test_dir/
[email protected]:~ $ rm -i tempfile.txt
rm: remove regular empty file `test.txt'? y
[email protected]:~ $ ls -p
file1.txt
test_dir/
[email protected]:~ $ rm test_dir
rm: cannot remove `test_dir': Is a directory
[email protected]:~ $ rm -R test_dir
[email protected]:~ $ ls -p
file1.txt

我們來分析一下上面的操作:首先我們通過ls命令查詢可知當前目下有兩個檔案和一個資料夾;

  • 你可以用引數-p來讓系統顯示某一項的型別,比如是檔案/資料夾/快捷連結等等;
  • 接下來我們用rm -i嘗試刪除檔案,-i引數是讓系統在執行刪除操作前輸出一條確認提示;i(interactive)也就是互動性的意思;
  • 當我們嘗試用上面的命令去刪除一個資料夾時會得到錯誤的提示,因為刪除資料夾必須使用-R(recursive,迴圈)引數

特別提示:在使用命令操作時,系統假設你很明確自己在做什麼,它不會給你太多的提示,比如你執行rm -Rf /,它將會刪除你硬碟上所有的東西,並且不會給你任何提示,所以,儘量在使用命令時加上-i的引數,以讓系統在執行前進行一次確認,防止你幹一些蠢事。如果你覺得每次都要輸入-i太麻煩,你可以執行以下的命令,讓-i成為預設引數:

alias rm='rm -i'

查詢當前程序:

[email protected]:~ $ ps
PID TTY          TIME CMD
11278 pts/1    00:00:00 bash
24448 pts/1    00:00:00 ps

這條命令會例出你所啟動的所有程序;

  • ps -a可以例出系統當前執行的所有程序,包括由其他使用者啟動的程序;
  • ps auxww是一條相當人性化的命令,它會例出除一些很特殊程序以外的所有程序,並會以一個高可讀的形式顯示結果,每一個程序都會有較為詳細的解釋;

控制流程

輸入/輸出

input用來讀取你通過鍵盤(或其他標準輸入裝置)輸入的資訊,output用於在螢幕(或其他標準輸出裝置)上輸出你指定的輸出內容.另外還有一些標準的出錯提示也是通過這個命令來實現的。通常在遇到操作錯誤時,系統會自動呼叫這個命令來輸出標準錯誤提示;

我們能重定向命令中產生的輸入和輸出流的位置

重定向

如果你想把命令產生的輸出流指向一個檔案而不是(預設的)終端,你可以使用如下的語句:

Example:

[email protected]:~ $ ls > file4.txt
[email protected]:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

以上例子將建立檔案file4.txt如果file4.txt不存在的話。注意:如果file4.txt已經存在,那麼上面的命令將覆蓋檔案的內容。如果你想將內容新增到已存在的檔案內容的最後,那你可以用下面這個語句:

  • command >> filename

示例:

[email protected]:~ $ ls >> file4.txt
[email protected]:~ $ cat file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt
file1.txt
file2.pdf
file3.mp3
file1.pdf
another_file.txt
Yet-Another_file.txt
file-with_other-NAME.TXT
file4.txt

在這個例子中,你會發現原有的檔案中添加了新的內容。接下來我們會見到另一種重定向方式:我們將把一個檔案的內容作為將要執行的命令的輸入。以下是這個語句:

  • command < filename

Example:

[email protected]:~ $ sort < file4.txt
another_file.txt
another_file.txt
file1.txt
file1.txt
file2.pdf
file2.pdf
file3.mp3
file3.mp3
file4.txt
file4.txt
file-with_other-NAME.TXT
file-with_other-NAME.TXT
Yet-Another_file.txt
Yet-Another_file.txt

如你所見,我們將file4.txt(的內容)作為命令的引數輸入。

管道

Linux的強大之處在於它能把幾個簡單的命令聯合成為複雜的功能,通過鍵盤上的管道符號'|' 完成。現在,我們來排序上面的"grep"命令:

grep -i command < myfile | sort > result.text


搜尋 myfile 中的命令,將輸出分類並寫入分類檔案到 result.text 。 有時候用ls列出很多命令的時候很不方便 這時“|”就充分利用到了 ls -l | less 慢慢看吧.

後臺程序

CLI 不是系統的序列介面。您可以在執行其他命令時給出系統命令。要啟動一個程序到後臺,追加一個“&”到命令後面。

sleep 60 &
ls

睡眠命令在後臺執行,您依然可以與計算機互動。除了不同步啟動命令以外,最好把 '&' 理解成 ';'。

如果您有一個命令將佔用很多時間,您想把它放入後臺執行,也很簡單。只要在命令執行時按下ctrl-z,它就會停止。然後鍵入 bg 使其轉入後臺。fg 命令可使其轉回前臺。

sleep 60
<ctrl-z>
bg
fg

最後,您可以使用 ctrl-c 來殺死一個前臺程序。