歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux——簡單介紹

Linux——簡單介紹

日期:2017/3/3 13:53:17   编辑:Linux技術

Introduction

許多人通過一個圖形用戶界面(graphical user interface)與計算機交互。GUI如下:

在GUI之前,人們是通過命令行界面(command-line interface)與計算機進行交互的,它也可以稱為一個shell或終端。對於一個編程任務,命令行界面比GUI更快更強大。 幾乎所有的程序員和數據科學家都廣泛的使用終端,並且認為能與它進行交互是一個至關重要的技能。每個系統的終端都稍微有點不同,它們使用不同的命令和語法。比如linux和OSX,因為他們都是基於unix操作系統,所以他們的終端非常相似。但是Windows操作系統與上面兩個的終端就截然不同,因為命令都是不同的。由於使用linux較為廣泛,因此以下的任務都是運行在Ubuntu 14.04上。

Filesystem

[code]~$ pwd                                                                  
/home/dq

當我們在終端中輸入pwd時,它提示我們現在所在的文件夾。終端能夠在目錄之間進行切換。它與圖形界面不同的是圖形界面是點擊文件夾,而命令行界面是輸入一個命令。當學會使用終端後,在終端中進行目錄導航將比圖形界面快很多。

我們把根目錄描述為斜槓/,home目錄是在根目錄下的,而dq目錄是home目錄的子目錄。

當輸入cd / 表示進入到根目錄

[code]~$ cd / 
~$

Absolute Vs Relative Paths

當輸入/時這是切換到根目錄,任何路徑以/開頭都是絕對路徑。絕對路徑是與文件的根目錄相關的。無論你現在在哪個目錄,當你輸入cd /home/dq都將進入到dq目錄。

另一方面,相對路徑是相對你現在所處的目錄,不能以 / 開頭。如果你現在在home目錄下,你輸入cd dq將切換到 /home/dq目錄下。如果你在 / 根目錄下輸入cd dq,此時會報錯,因為在當前目錄下不存在dq這個目錄。

[code]/$ cd home                                                              
/home$

此時利用相對路徑進入到home目錄中,由於home是在根目錄下,因此home文件也叫根文件夾。

Getting Back To The Dq Folder

現在我們處在home文件夾中,我們需要進入到dq中,由於dq就在home文件夾下,采用相對路徑即可:
[code]/home$ cd dq                                                            
~$

Users

現在我們以用戶dq的身份進入到這個習題中,我們可以通過whoami命令來查詢我是誰:
[code]~$ whoami                                                               
dq 
~$

The Home Directory

每個用戶都有一個home目錄,而且每一個home目錄都在/home這個目錄下。比如現在我是dq用戶,那麼我的home目錄就是/home/dq.輸入cd ~將自動切換到當前用戶的home目錄:
[code]~$ cd ~                                                                 
~$

Making A Directory

可以通過mkdir命令來新建一個目錄。由於有相對路徑和絕對路徑的區別,因此如果你輸入mkdir test,那麼這個test目錄就在當前目錄下,因為輸入的是相對路徑。如果你輸入mkdir /home/dq/test,這是絕對路徑,因為開頭是根目錄 / ,因此test就在/home/dq目錄下。
[code]~$ mkdir test 
~$

Command Options

我們可以通過一個破折號 - 加入一些參數來修改命令的行為,比如在mkdir後面添加 -v表示“冗長”模式,並打印輸出新建的文件夾。
[code]~$ mkdir -v test2                                                       
mkdir: created directory ‘test2’  
~$

Checking Possible Options

大部分命令都提供了–help這個功能,可以供你查詢這個命令有哪些待選參數以及其功能。
[code]~$ mkdir --help                                                         
Usage: mkdir [OPTION]... DIRECTORY...                                   
Create the DIRECTORY(ies), if they do not already exist.                

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask      
  -p, --parents     no error if existing, make parent directories as nee
ded                                                                     
  -v, --verbose     print a message for each created directory   
  -Z, --context=CTX  set the SELinux security context of each created   
                      directory to CTX                                  
      --help     display this help and exit                             
      --version  output version information and exit                    

Report mkdir bugs to [email protected]                              
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>       
General help using GNU software: <http://www.gnu.org/gethelp/>          
For complete documentation, run: info coreutils 'mkdir invocation'   
~$

Listing The Contents Of A Directory

到目前為止,在我的主目錄下創建了兩個文件夾,我們可以通過ls -l來查看dq目錄下的文件:
[code]~$ ls -l                                                                
total 8                                                                 
drwxr-xr-x 2 dq dq 4096 May  4 07:21 test                               
drwxr-xr-x 2 dq dq 4096 May  4 07:21 test2   
~$

Removing A Directory

rmdir可以用來刪除一個文件夾
[code]~$ rmdir test2                                                          
~$ rmdir --help                                                          
~$ rmdir --help                                                         
Usage: rmdir [OPTION]... DIRECTORY...                                   
Remove the DIRECTORY(ies), if they are empty.                           

      --ignore-fail-on-non-empty                                        
                  ignore each failure that is solely because a directory
                    is non-empty                                        
  -p, --parents   remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/
b/c' is  
                    similar to 'rmdir a/b/c a/b a'                      
  -v, --verbose   output a diagnostic for every directory processed     
      --help     display this help and exit                             
      --version  output version information and exit                    

Report rmdir bugs to [email protected]                              
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>       
General help using GNU software: <http://www.gnu.org/gethelp/>          
For complete documentation, run: info coreutils 'rmdir invocation'    
~$

Copyright © Linux教程網 All Rights Reserved