文章

Linux 筆記: 快捷鍵與常見基本指令

前言

雖然Linux指令只要熟悉就背起來了,但避免學了後又忘記,還是把學到的指令記下來好了。學習的資源都列在下面

快捷鍵

  • 開啟terminal: Ctrl+Alt+t
  • 關掉terminal: Ctrl+D
  • 終止執行: Ctrl+C

Linux常見基本指令

cd

change directory的縮寫,後面接相對或絕對路徑

pwd

print working directory的縮寫,印出當前路徑

ls

列舉當前路徑下所有檔案

  • -a: 顯示所有隱藏檔案
    1
  • -l: 顯示檔案的細節
  • -R: 顯示所有子目錄

file

後面接檔案,可以印出檔案的類型

locate

後面加關鍵字,可以找出檔案,若要更新資料庫用指令sudo updatedb

type

後面接指令,可以告訴你指令位置與種類

man

後面接指令,查看指令的manual

mkdir

make directory的縮寫,用來創建資料夾

1
$ mkdir d1 d2 d3
  • -p: 若建立多層資料夾,缺失的資料夾會自動建立,例如指令mkdir -p one/two/three會除了建立/one/two/three資料夾外,如果沒有料夾/one和/one/two,會自動幫忙建立。

touch

後面接檔案名稱,用來access檔案來改變編輯時間,若檔案不存在則創立空的檔案

1
2
3
4
$ ls
$ touch file1
$ ls
file1

cat

可以用來建立、檢視、複製和合併文字檔案

  • 建立檔案:
    按Ctrl+Shift+D結束鍵盤輸入,此方法會蓋掉原有檔案的內容
    1
    2
    
    $ cat > file1
    hello
    

    若不要覆寫原有檔案的內容,用

    1
    2
    
    $ cat >> file1
    world
    
  • 檢視檔案:
    1
    2
    3
    
    $ cat file1
    hello
    world
    
  • 複製檔案:
    複製file1檔案的內容到file2,此方法會蓋掉file2檔案的內容
    1
    2
    3
    4
    
    $ cat file1 > file2
    $ cat file2
    hello
    world
    

    若不要覆寫file2檔案的內容,用

    1
    2
    3
    4
    5
    6
    
    $ cat file1 >> file2
    $ cat file2
    hello
    world
    hello
    world
    
  • 合併檔案:
    合併file1的內容與file2的內容至newfile
    1
    2
    3
    4
    5
    6
    7
    8
    
    $ cat file1 file2 >file3
    $ cat file3
    hello
    world
    hello
    world
    hello
    world
    

    若不要覆寫newfile檔案的內容,用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    $ cat file1 file2 >> file3
    $ cat file3
    hello
    world
    hello
    world
    hello
    world
    hello
    world
    hello
    world
    hello
    world
    

cp

複製檔案

  • 複製file1的內容到file2:
    1
    
    $ cp file1 file2
    
  • 複製file1、file2、file3至dir資料夾底下:
    1
    
    $ cp file1 file2 file3 dir
    

option:

  • -r: 連同子目錄一起複製

rm

刪除檔案

  • 刪除file1、file2:
    1
    
    $ rm file1 file2
    
  • 刪除dir1、dir2下的所有檔案:
    1
    
    $ rm -r dir1 dir2
    

option:

  • -r: 連同子目錄一起刪除

sudo rm -rf /會刪掉根目錄,小心斜線後不要有空格

本文章以 CC BY 4.0 授權