NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 1101, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/book # Week 04(2021/10/14) [TOC] ## Vim ### 來由 - 查看檔案內容會用`cat`指令 - `cat` 可以改內容 - 使用`cat > filename`,會附蓋掉原本內容 - 用 `cat >> filename` 則會讓輸入增加在原本內容後面 - `ctrl + d` 儲存並離開 - 未來如果改是設定檔,只是想改一行我卻須要重寫一次檔案 - 需要編輯器,方便編輯檔案 - nano - 大家電腦應該都會有 - 比較早期的文字編輯器,功能有限 - 對一些較嚴謹的檔案做編輯有時會出錯 - e.g. **無法分辨 `space` 或 `tab` 的差異** - Windows 的換行符號`\r\n` - Linux的換行符號`\n` - MAC的換行符號`\r` - 所以如果檔案跨系統搬來搬去,可能會因為換行符號的關係無法正常開啟 - `dosZunix` or `unixZous`可以處理換行符號的關係 - 功能有限 - 想要在這個遠端伺服器上寫程式 - 遠端桌面 - 太吃資源 - vim - 執行環境簡潔,且功能強大 - 解決吃太多資源的問題 ```terminal= // 安裝vim sudo apt install vim ``` - 操作不直觀 ![](https://i.imgur.com/VyltxuK.png) - 一進入 vim 是 command mode - `i`:insert mode - `:w`:write,將剛剛做的變更寫回檔案 - `!`強制執行(例如要離開,打:q!) - `:q`:離開 - 在terminal更改檔案,若要退出/儲存先按 `esc` - `:wq`出去+儲存 - `:w` 儲存檔案 - `:q` 退出檔案 - `!` 強制執行 - `:set nu` 顯示行號 - `/` 搜尋,然後按n顯示下一個 ### 指令 - 安裝: `sudo apt install vim` - 一進入 vim 是 command mode,輸入`i`進入 insert mode - 在 terminal 更改檔案,若要退出 / 儲存先按 esc | 指令 | 用處 | | --- | --------| | :w | 存檔 | | :q | 退出檔案 | | :q! | 強制退出檔案 | | :wq | 儲存並離開 | |:e! |還原到剛開啟檔案的時候| | /+"關鍵字" | 搜尋"關鍵字" | | n |找下一個"關鍵字"| |dd | 刪除一行| |數字+dd |(從游標那行算起)刪除底下"數字"行| |u |還原| |ctrl+r |重做| |dw |一次刪除一個字| |yy | 複製一行 | |"數字"+yy | (從游標那行算起)複製底下"數字"行 | | p |貼上| - 建立/更改 vim 設定檔: `vim .vimrc` ```txt= :set nu # 顯示行數 :set tabstop=4 # tab 轉換成 4 個空白 :set cursorline # 顯示該行的底線 ``` > [Uncle Roger 的 vimrc](https://github.com/nigeljyng/dotfiles/blob/master/vim/.vimrc) > 網路上的 [vim cheat sheet](https://external-preview.redd.it/iigrixvxp5aYN9ox7Gr1dfI_rhLRotWlLsCafjJqjEQ.png?auto=webp&s=1594ddc17408cb9186a73c2a6d1a1bf1e00769dd) ## Linux 基本指令 - `pwd(檔名)` 顯示目前的位置,有時候會自己建一個新檔案 - `cd <路徑>`(change directory) 改變位置 - `cd /` 移至根目錄 - `cd ~` 移至家目錄 - `cd` 也是移至家目錄 - `cd ..` 回到前一層,但是在根目錄使用的話就會停留在根目錄 - 相對路徑 - 從目前的路徑往上 / 下層尋找(不是由 `/` 開頭) - `cd 要去的資料夾` - `cd ./要去的資料夾` 這樣也行,`.`表示當前資料夾 - 絕對路徑 - 自 `/` (根目錄) 寫起 - `cd /要去的資料夾的完整路徑` - `ls` 顯示所有檔案 - `-l` 印出檔案詳細資訊 - `-a` all file(印出隱藏檔) - `-S`用檔案大小排序,ls檔案大小的排序預設為降冪 - `-r`反向排序 - [ls常用指令](http://it.givemepower.net/linux-command-ls-view-files-and-directories/) - ![](https://i.imgur.com/5gpYNFo.png) - `tab`:打前幾個字,若目錄下有名字一樣的檔案,會自動補齊 - `mkdir <dir name>` 產生新目錄 - `man <指令名稱>` 顯示指令說明 - `touch <檔名>` 產生新空白的檔案 - `-a <檔名>`: 更改檔案時間 >![](https://i.imgur.com/OYh9lHH.png) - `stat <檔名>` 顯示相關時間戳記 - `上鍵 `上一個指令 - `cat <檔名>`: 讀取檔案內容 - `more <檔名>` 從上開始顯示 - `less <檔名>` 用分頁顯示(按q退出) - `head -<行數> <檔名>` 輸出前面幾行 - `tail -<行數> <檔名>` 輸出最後幾行 - `tac` 反著印出資料 - `echo <文字>` 打什麼輸出什麼,會回復<文字> - `echo "hello moto"` ,他會印出 `hello moto` - `echo $SHELL`: 現在的shell是什麼 - `echo <文字> > <檔名>` 將輸入的<文字>覆蓋原本檔案的內容 - 例如 `echo 123 > 123.txt` > 原本123.txt的內容全部會被123蓋過,所以123.txt的檔案內容只剩下123 - `echo <文字> >> <檔名>` 將<文字>加入在<檔案>內容的後面 :::info - `>` : stdout replace - `>>` : stdout append - `1>` : stdout replace - `2>` : stderr replace - `2>&1`: 將 "標準錯誤 (stderr)" 重導入至 "標準輸出(stdout)當前的檔案描述符" - ![](https://i.imgur.com/gKmEQxD.png) - `cat&>>` : stdout, stderr 都會寫入 - stdout : 標準輸出 (代碼為1) - ex: `cat 存在檔案出現的內容` - stderr : 標準錯誤輸出(代碼為2) - ex: `cd 到不存在目錄出現的錯誤訊息` - `cd /jsdf 2>&1 | less` - 將錯誤訊息導到標準輸出再 pipe 到 less ::: - `tee` 輸入並顯示(會覆蓋原檔案) - 同時輸出到螢幕與檔案 - `tee -a <檔名>` 在原有的東西後加入後也顯示 - `ls | tee -a <檔名>` - `|`: 將前面的 stdout 當作後面指令的 stdin - 應是前面 `ls` 後的輸出結果導入到後面當作輸入並經由 `tee` 輸出到螢幕與檔案 - `./<指令名稱>` 執行當前目錄此名稱的指令 - `cp <file> <path>` 複製檔案 - `-r` 遞迴複製,複製資料夾及其底下東西 - `-a` 維持原本各種資料,包括權限 - `rm <file>` 刪除檔案(remove) - `-r` 遞迴,刪除資料夾內容 - `-rf` 把所有檔案全部強制刪除 ### 環境變數 - `export | less` 來看有哪些環境變數 - pipeline 進 less 裡是為了方便搜尋 - 在 less 裡按 `/` 接著打 keyword 再按 Enter 來搜尋 - `$HOME` 你的 home 目錄位置 - `$PWD` 當前目錄 - `$SHELL` 當前所使用的 shell 之名稱 - 如果確定要執行的檔案在當前的目錄下,前面加`./`,告訴 SHELL 這個檔案就在這裡,不會執行到其他同名的檔案 - `$PATH` 當執行指令但沒指定確切目錄,則會從 $PATH 裡的路徑去找 - 如果都找不到就會說 command not found - 如果自己寫了一個指令,可以把它放到系統某個地方,再將該路經加到 PATH,就可以執行這個指令 :::info 大家有看過 Windows 的隱藏檔案,和打開**副檔名顯示**ㄇ? 因為顯示副檔名預設是關閉的,有心人士就能騙你打開 `可愛狗勾.jpg.EXE`,你會只看到`可愛狗勾.jpg` ,然後你就中毒了。 ::: - `stat <檔名>` :::info - 在檔案瀏覽器上按「永久刪除」,這檔案在硬碟上不會馬上被抹掉,還在硬碟表面某個磁區裡!為了效率的關係沒抹掉 - 事實上只是取消列出檔案而已,如果你用檔案救援軟體(電子鑑識)還是有機會撿回來喔 - 如何最大化檔案救援回來機會:斷電!不要再做任何寫入了 - 讀檔也有在寫入 ::: ### Linux 基本指令 (cont.) - 先切到bin 底下輸入`xxd zip ` - `xxd zip |less` - `xxd` 用binary狀態開啟檔案 - `mount ro ` 掛載磁區且 read-only - realatime - `mount |grep noatime`: > `/etc/`目錄下 - `cp <檔案> <路徑>`複製檔案 - `-a`維持檔案的相關權限 - `cp -r <目錄> <路徑>`複製目錄 - `rm <檔案>`移除檔案 - `rm -r <目錄>`移除目錄及其底下所有檔案 - `sudo rm -rf <目錄>`強制移除目錄 - `-f`強制 - `mv <檔案1> <目錄>` 把檔案1移到目錄下 - 如果是目錄,後面加`/`比較好(`<目錄>/`) - `clear`、`ctrl+l`清除 terminal - `ls -al `/`ll`: 全部檔案的詳細資訊 ### 檔案權限 ![](https://i.imgur.com/ILgjdQa.png) - `r` `4`讀、`w` `2`寫、`x` `1`執行、 ` ` `7`讀寫執行 (後面的數字是二進位來的) - 分 3 組,分別是擁有者(user)權限、群組(group)權限、任何人(other)權限 [![](https://i.imgur.com/op0q42A.png)](https://www.runoob.com/linux/linux-comm-chmod.html) > image source: [Linux chmod 命令 | 菜鸟教程](https://www.runoob.com/linux/linux-comm-chmod.html) #### 修改權限 - 方法一:文字 - `chmod <人+動作> <file>`修改權限 - `u` user - `g` group - `o` other - `a` all - `<人>+<動作>` 讓<人>可以<動作> > `<人>-<動作>` 讓<人>刪除這個<動作> - `chmod u+x file` - 方法二:數字 - `chmod 777 file` - 777 依序為 `user` `group` `others` - 7的來源為$111_2$ (rwx都可以的二進位) :::info 如果要將一個檔案`hi.txt`的權限變更為下表的樣子,有二種方法 | | u 用戶 | g 群組 | o 其他 | | --- | ------ | ------ | ------ | | str | rwx | rwx | rwx | | str | rw- | rw- | r-- | | 0x | 110 | 110 | 100 | | 0o | 6 | 6 | 4 | - 方法一:`chmod 664 hi.txt` - 方法二:`chmod u+rw hi.txt` ::: :::info **用 bit 記檔案權限,會比用字元 (char) 記省好多空間** - 0x110110100 -> 9 bits - `rw-rw-r--` -> 9 bytes = 72 bits ::: ### Linux 基本指令 (cont.) - `find <path>` 搜尋檔案 - `-perm 數字` 找有這權限的 - `-name <字>` 找名字為 <字> 的 (要完全符合) - `ls | grep <字>`: 找那個目錄下含有該<字>的檔案 (部份符合就好) - grep 參數 - `-i` 不管大小寫 - `log` 找到<字>相同的(輸入的大小寫有差) - `-n` 印出行數 - `-in` 不管大小寫的行數 - `-r` recursion,遞迴往下找 - `ls -l | awk '{print $1"\t"$8}'` 過濾出ls資料的第一列和第8列並將資料印出來 > `awk` 一個可以操作字串的程式,[資工系教授 klim 寫的 cheat sheet](http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/awk-summary.txt) - `sort` 排序 - `-c` 將重複的刪除 - `-u` 刪掉輸出到螢幕上重複行 - `-r` 降冪 - `-n` 依照數值排列 - `-k <第幾個col>` 依照第幾個col排序 - `sed` 只會把操作完的字串顯示出來,沒有改變檔案 - `-i` 操作完的字串顯示並寫入檔案 :::info **`#!/bin/bash` shebang** - 只能寫在第一行才有效 - 我希望接下來的(純文字)內容用什麼...去執行他。這樣就可以開一個文字檔,給他執行權限之後 `./檔名` 來執行 - `#!`:Shebang ::: ## 作業討論??? ### FHS 1.哪一個預設使用者的家目錄,預設沒有放在 /home/ **Ans: 超級使用者** 2.如果我想查詢這台主機有哪些使用者帳號,要查看哪個檔案 ? **Ans: /home/** /etc/passwd ?? passwd或shadow? ### 指令 1.修改檔案 test.py 的權限,檔案 owner 可以讀、寫、執行,與擁有者同一群組者可以讀、執行,其餘的使用者可以讀。請問要如何下指令? - 用數字表示法修改權限 **Ans: chmod 754 test.py** - 用文字表示法修改權限 **Ans: chmod u+rwx,g+rx,o+r test.py** **? chmod u=rwx,g=rx,o=r test.py** 2.若要將當前目錄下檔名含有 LSA 檔案的詳細資料,並依檔案大小(由小到大)輸出於螢幕上,請問要如何下指令? **Ans:ls -S | grep LSA** **? ls -lSr | grep LSA** **? ls -Slr | grep LSA** **ls -lSr | grep LSA** ### 硬碟配置 #### 故事 :::info Uncle 很愛迷因,為此他非常熱心地架了一個網頁,可以即時的更新迷因圖跟大家分享,但 PP 覺得這個網站常常連不進去,要求改善,不然就要請歐姊姊對 Uncle 提告。 Uncle 強迫蓬萊幫他找問題,蓬萊迫於淫威,查了 Apache 網頁伺服器的紀錄檔沒發現問題,又調整了 MySQL 資料庫的設定,但改善程度有限,不知如何是好,便求助鍋子。 不消一炷香時間,就發現 CPU 和記憶體使用量正常,問題其實是硬碟太老舊,所以撐不住資料庫的大量的讀寫,加上網頁空間也快要滿了。 於是 Uncle 又強迫蔣媽花錢更新設備,不過蔣媽沒錢,走在路上一籌莫展時,阿棟剛好騎車經過,剛好背包掉東西出來,又剛好掉了 2 顆 1T SSD 跟 1 顆有多層泡泡紙精心包裝的 4T HDD,被蔣媽撿到, Uncle 拿到新的硬碟後,便強迫 keke 把網頁設置到硬碟上,但 keke 不會用,只好詢問 LSA 的各位學生,該怎麼配置,才能改善這個網頁呢 ? ::: #### 跑在上面服務 - MySQL 資料庫 - Apache 網頁伺服器 #### 硬碟 - `1T SSD` * 2 、 `4T HDD` * 1 Ans

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully