Try   HackMD

Linux 基本指令介紹

雖然現代的電腦作業系統,已經具有非常完備的圖形介面可以進行各項操作,所以大多數人已經不再接觸指令操作。但是在熟悉指令後,在進行某些操作可以更簡潔有力,因此接下來將要介紹各式 Linux 的基本指令。

為何選擇 Linux?
目前電腦作業系統主要可以分為三系:Windows、MacOS、Linux,而 MacOS 和 Linux 算是系出同門(Unix-like),所以兩者幾乎沒有差別。而 Windows 的指令承接 DOS 時代,到現在也沒有什麼改進,使用人數少,加上它的 Powershell 能夠相容 Linux 指令,所以在此將以 Linux 的指令為主。

Linux 是什麼,能吃嗎?

在解釋 Linux 是什麼之前,得先了解什麼是作業系統。

Operating System (作業系統)

作業系統是電腦裡的系統程式,負責調度與分配硬體資源給需要的應用程式使用。

而常見的作業系統有 Windows、MacOS、Linux 三者。

Windows

最常見的作業系統,由微軟開發,以完全的圖形操作介面聞名。

MacOS

第二多人使用的作業系統,由蘋果開發,介面優雅簡潔。

Linux

由社群合力開發,雖然一般使用者最少,但卻是伺服器系統的首選,在伺服器領域遠遠超越 Windows Server 版本。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Linux 吉祥物——Tux
Photo credit: lewing@isc.tamu.edu Larry Ewing and The GIMP

Linux (Kernel)

Linux 的本質是一個作業系統核心,意即只包含了最基本的功能。

而 Linux 的核心當初由 Linus Torvalds 開發出來,並且將所有原始碼開放,讓所有人皆能自由修改使用,這也造成了後來 Linux 有各種變化版本。

Linux Distribution (發行版)

Linux 發行版就是將核心加上搭配其他程式打包起來,變成使用者能夠自行安裝進電腦的形式。

而各發行版的差別,最明顯的就是圖形介面。

Android 就是一個為行動裝置設計的 Linux 發行版喔!

學習詠唱咒語:Linux 指令的使用

  • 與 Linux 溝通的第一步

接下來將要正式開始介紹 Linux 指令的使用。

Terminal(終端機)

終端機是一個純文字介面的程式,會提供 Shell(一個用來下指令,並且幫忙解析指令意圖的程式)跟系統進行互動。

而在三大系統裡面,開啟終端機的方式分別如下:

Linux

  • Ctrl+Shift+T
  • 系統功能表->系統工具->終端機

MacOS

  • 在啟動器裡面尋找「Terminal.app」即可

Windows

CMD 與 Linux 指令不相容,所以請注意開啟的終端機程式!

  • 開啟 Powershell(預設是藍色背景)

常用的 Linux 指令

雖然 Windows 的 Powershell 相容 Linux 指令,但在行為上可能與原生 Linux 或 MacOS 有較大差別,以下教學將在 Linux 系統裡操作為主。

pwd

  • print working directory
  • 顯示目前你所在的位置

在終端機裡面通常會想做的第一件事,就是查詢目前所待的目錄位直,而透過 pwd 可以讓我們做到這一件事。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ls

  • list
  • 列出資料夾裡的內容
  • 預設列出你目前所在的資料夾

在知道自己身處的位置之後,接下來就是探索目前所在位置有哪些子資料夾和檔案,這時 ls 就可以做到這件事情。

ls
ls <資料夾名稱>
ls -a #列出隱藏內容
ls -l #列出詳細資料

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

cd

  • change directory
  • 切換資料夾
cd <資料夾位置>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

位置表示法

在表示位置的時候,我們習慣上會有兩種做法,分別為絕對位置相對位置

絕對位置

  • 宇宙銀河系太陽系地球臺灣桃園市龜山區德明路5號S棟

絕對位置是從最初的起點一路表示到所在位置,像是如果要用絕對位置來表示銘傳大學的資訊大樓,就會像上列例子所示。

而在 Linux 裡面,所有目錄的起點為 /,也稱為根目錄或是 root

相對位置

  • 銘傳大學的AA棟隔壁那棟大樓

相對位置可以是用目前所在位置作為參考,進一步去延伸想要表示的位置,就像是以一個現成的建築物去對照資訊大樓的位置。

而在 Linux 裡面,這種比照方式就不是以 / 開頭,而會是 ...

  • . 開頭: 目前所在的位置
  • .. 開頭: 目前所在的位置的上一層

再以 cd 為例,如果我想要直接回到上一層的話,我可以用 cd .. 來達成:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

mkdir

  • make directory
  • 建立資料夾
mkdir <新資料夾名稱>

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

cp

  • copy
  • 複製檔案、資料夾
cp <檔案名稱> <新檔案名稱>
cp -R <資料夾名稱> <新資料夾名稱>

cat

  • concatenate
  • 檢視檔案內容
  • 可以輸入多個檔案一次顯示

如果需要在終端機檢視文字檔,則可以透過 cat 來達成。

cat <檔案名稱1> <檔案名稱2> ......

毀滅性法術:具有風險的指令們

接下來要介紹的指令具有一定危險性,請小心使用!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

mv

  • move
  • 移動檔案或資料夾
  • 更改名稱
  • 覆蓋檔案

move 是一個多功能的指令,除了基本的移動功能之外,如果移動的位置不變,就會有更改檔案名稱的作用。

只是要注意,如果新名稱已經存在,系統將不會警告,直接強制覆蓋掉

mv <檔案名稱> <資料夾名稱>
mv <原名稱> <新名稱>

rm

  • remove
  • 刪除檔案或資料夾
  • 不可逆

透過這個指令,將會直接刪除檔案,不經過垃圾桶。

rm <檔案名稱>
rm -R <資料夾名稱>

sudo

  • superuser do
  • 以管理員權限執行後者指令
sudo <指令>

END

希望讀完這篇文之後,能夠具備詠唱咒語操縱指令的能力owo