<font color="#8A2BE2">[W1-1]</font> Command Line Basic === 快速了解本篇目錄 [TOC] # 1. Intro ## 講古 * 參考 author note 100% 最新的 * 歷史 * Mac OS 用 Bash shell 當作終端機 * Catalina 用 Z shell 當作終端機 (需要一些其他) <br> * :::spoiler 終端機簡稱 * command line = shell = terminal = bash * :::spoiler 終端機功能 * 建立資料夾...等等 # 2. Main Page ## 指令 man man導覽頁 與 q跳出 ```= man man //導覽 q //回到 Command Line ``` man man ![](https://i.imgur.com/mDr12jN.png) ![](https://i.imgur.com/vNIguza.png) q ![](https://i.imgur.com/OOdgMxi.png) # 3. Navigation ## 新增 ```= pwd //你現在在哪 ls //有哪些資料夾 cd 檔案名 斜線 //去哪個檔案 ex: cd Download/ cd //上一頁 clear //刪除之前終端機訊息 man ls //檢索快捷鍵 -a //可以看隱藏資料夾,因為隱藏資料夾都會是 .文件名,configuration file (配置檔案) //.目前檔案 //..檔案裡面的檔案 ls cd Download //或是可以簡寫 cd Dow+tab鍵,會自動補齊文字 cd ``` ![](https://i.imgur.com/yaTaeVj.png) ![](https://i.imgur.com/vITzqS3.png) ![](https://i.imgur.com/YrMVcmA.png) # 4. Creation and Destruction ## 新增、開啟檔案、刪除 ```= mkdir 檔案名 //在目錄中建立檔案 mkdir 檔案名 //在目錄中建立檔案 touch 檔案名.檔案格式 //一般來說會用在開啟瀏覽器,但如果沒有該檔案,會在檔案中建立檔案 open . //用Finder彈出視窗,把檔案開啟給我看 cp 點選檔案名.點選檔案格式 複製新檔案名 新檔案格式 //複製檔案 rm 檔案名.檔案格式/ //刪除檔案,不能刪除資料夾 rmdir 資料夾名/ //刪除資料夾,資料夾內必須是空的,才刪的掉 rm -r 資料夾名/ //直接刪除資料夾以及資料夾內的檔案 步驟示範 mkdir 秘密資料夾 cd 秘密資料夾 touch 秘密檔案-1.txt open . cp 秘密檔案-1.txt 秘密檔案複製檔-2.txt rm 秘密檔案複製檔-2.txt cd .. 返回上一層 rm -r 秘密資料夾 ``` ![](https://i.imgur.com/bZtFloO.png) ![](https://i.imgur.com/7ALHPnk.png) ![](https://i.imgur.com/VJIv1B5.png) ![](https://i.imgur.com/KeaCBAb.png) # 5. Creation and Destruction: Hierarchy Challenge ## 實際練習,並且施作Symbolic Link ```= mkdir 檔案夾A 檔案夾B 檔案夾C //新增3個檔案夾 cd .. //往上一層目錄移動 方法1 cd 檔案夾/ && touch 檔案.檔案格式 && cd .. //進入檔案夾並且新增某個格式的檔案最後跳回上一頁 ls 檔案夾/ //查看某檔案夾裡面的項目 方法2 touch 檔案夾/檔案名.檔案格式 //在某檔案夾內新增某格式的檔案 ln -s /Users/Bob/Downloads/Users/Bob/Desktop ln -s 我要的檔案(拖曳) 要在哪一層層(拖曳) pwd ls mkdir birds mammals reptiles //一次建立三個資料夾 ls cd birds/ && touch eagles.txt && cd .. //進入鳥資料夾並且新增老鷹純文字檔最後返回上一層 ls birds/ //查看鳥資料夾下有什麼項目 touch reptiles/trex.txt //爬行類動物資料夾下新增trex純文字檔 ls touch mammals/koala.txt //哺乳類資料夾下新增無尾熊純文字檔 ln -s birds/eagle.txt strongestAnimals.txt //Symbolic Link ls -l //查看Symbolic Link ``` 一次建立多個資料夾 ![](https://i.imgur.com/Pe2hPKw.png) Link 用程式打 ![](https://i.imgur.com/nfquoVV.png) Link 用滑鼠拖曳 ![](https://i.imgur.com/ap7Cr41.png) ![](https://i.imgur.com/FPNjM6I.png) ![](https://i.imgur.com/NjfJ81G.png) ![](https://i.imgur.com/BmPpl8l.png) [參考Youtube](https://www.youtube.com/watch?v=43mGItOoJIM) ![](https://i.imgur.com/LOcZQzv.png) # 6. Find 眾多檔案中找到user.c檔案 ![](https://i.imgur.com/5II5xsK.png) ```= 拖曳檔案在終端機會直接生成檔案路徑 長按option鍵可以直接 切換移動打字位置 find . -name user.c //使用find方法找到該檔案,印出地址 cd 地址 //進入檔案 進入Vim vim user.c 先按下Esc案件,開通編輯 int user_count() { return 1; } :wq //離開 cd - //回到上次的資料夾,不是上一層,是該層最一開始 find . -name '*.hs' //查找.hs的檔案 find . -empty //查找空檔案或空目錄 ``` ![](https://i.imgur.com/Gl0aGZC.png) ![](https://i.imgur.com/GOUJCDz.png) [參考Vim鳥哥](https://linux.vbird.org/linux_basic/centos7/0310vi.php) # 7. Searching Inside Files 找出csv檔案中,名字是carlos喜愛的數字 ```= head workers.csv //在檔案夾下進入 grep "carlos" workers.csv //報錯誤,因為打小寫 方法1 找出carlos喜歡數字 用grep grep "carlos" workers.csv -i //找到檔案內名字是carlos的人,-i是告訴終端機不要管大小寫差異 方法2 找出carlos喜歡數字 用cat cat workers.csv | grep -Eo ',[0-9]+.[0-9],' ``` 印出csv檔案內 名字與薪水 ``` 印出csv檔案內 名字與薪水 名字在$3欄 薪水在$4欄 head worker.csv awk -F "," '{ print $3, $4}' workers.csv //$匿名參數 ``` ![](https://i.imgur.com/FpXr2Fi.png) :::success | GREP | GREP | | | -------- | -------- | -------- | | ![](https://i.imgur.com/Q3J8cLN.png)|![](https://i.imgur.com/SPI7QP7.png)| | ```= 先進入該檔案夾 cd /Users/yinbob/Desktop/2023/AppWorks/遠端/W1/Command\ Line/video-clb-materials-versions-1.0/07-searching-inside-files 進入檔案方式1 head workers.csv 進入檔案方式2 cat workers.csv | grep "Carlos" 印出favorite number cat workers.csv | grep -Eo ',[0-9]+.[0-9]+,' ``` 進入檔案 ![](https://i.imgur.com/yvFTEnx.png) 找出有叫Carlos的人 方式1 head workers.csv ![](https://i.imgur.com/6687WZW.png) 找出有叫Carlos的人 方式2 cat workers.csv | grep "Carlos" ![](https://i.imgur.com/FW2Lh0G.png) 印出favorite number cat workers.csv | grep -Eo ',[0-9]+.[0-9]+,' ![](https://i.imgur.com/hA8geE0.png) ::: <hr> :::info | AWK | | | | -------- | -------- | -------- | |![](https://i.imgur.com/VogWgvs.png)| ![](https://i.imgur.com/Eq6m8QK.png)| | ```= 總覽 head workers.csv 取出第三欄 第四欄的 名字 薪水 awk -F "," '{ print $3, $4 }' workers.csv //$符號是參數 第三欄column 第四欄column> ``` 總覽 head workers.csv ![](https://i.imgur.com/LxmuDlU.png) 取出第三欄 第四欄的 名字 薪水 awk -F "," '{ print $3, $4 }' workers.csv ![](https://i.imgur.com/ErFoaat.png) ::: 最終結果 ![](https://i.imgur.com/zsBp7F3.png) # 8. Challenge: Sorting ![](https://i.imgur.com/u9rZUng.png) 目標結果 ![](https://i.imgur.com/sWABdCr.png) ```= sort 檔案名 //總排序 sort -r 檔案名 //反排序 man sort man -f 檔案 q //跳出man ``` 查詢top5 第三欄 第四欄 ``` awk -F "," '{ print $3, $4 }' workers.csv | sort -k 3 //這其實是根據姓名 -k 3是第三欄 awk -F "," '{ print $3, $4 }' workers.csv | sort -k 3 -n //順序低到高 awk -F "," '{ print $3, $4 }' workers.csv | sort -k 3 -n -r //順序正確 awk -F "," '{ print $3, $4 }' workers.csv | sort -k 3 -n -r -u //單獨的 awk -F "," '{ print $3, $4 }' workers.csv | sort -k 3 -n -r -u | head -5 //只要top5 awk -F "," '{ print $3, $4 }' workers.csv | sort -r -k 3 -n | head -5 > top5.txt //把這5個夾在新檔案中 查看 ls 看檔案內容 cat top5.txt ``` :::success 總排序 sort workers.csv ![](https://i.imgur.com/1LXL2be.png) 只要5個 awk -F "," '{ print $3, $4 }' workers.csv | sort -r -k 3 -n | head -5 ![](https://i.imgur.com/y8ms1jP.png) 把這5個建立在新檔案中 awk -F "," '{ print $3, $4 }' workers.csv | sort -r -k 3 -n | head -5 > topnew.txt ![](https://i.imgur.com/P3hU8yH.png) [參考Youtube](https://www.youtube.com/watch?v=9j0h3qf73jI) ::: <font color="#8A2BE2">[W1]</font> Command Line Intermediate === # 11. Customizing Bash ![](https://i.imgur.com/zr9gM7X.png) ![](https://i.imgur.com/oRgtj3u.png) :::success mac 的 default 就是shell ::: ```= ls -a ls -al vim .bash_profile //我好像不行X open .bash_profile //開始編輯 ``` ![](https://i.imgur.com/YbNCh3C.png) 課程 ![](https://i.imgur.com/4KPqx7O.png) ![](https://i.imgur.com/QiM69MB.png) ## 我的 open .bash_profile //開始編輯快捷鍵 ![](https://i.imgur.com/2yL3QHo.png) # 12 Diff ```= cat 檔案名.檔案格式 //終端機內開啟檔案 diff a.txt b.txt //比較檔案 diff -p a.txt b.txt //比較檔案 好讀 diff -p a.txt b.txt > a.patch //b蓋過a檔案 patch < a.patch //運行patch cat a.txt ``` 比較檔案 不好讀 diff a.txt b.txt ![](https://i.imgur.com/Sop1u4M.png) 比較檔案 好讀 diff -p a.txt b.txt ![](https://i.imgur.com/OvxRqwD.png) b覆蓋a,a檔案目前的內容 diff -p a.txt b.txt > a.patch patch < a.patch cat a.txt ![](https://i.imgur.com/osU1KSq.png) # 13. Challenge: Undoing a Bad Patch 挑戰目標:patch 後發現不要這些改變,返回之前的patch(undo patch) ![](https://i.imgur.com/VKIigsK.png) ```= patch -R < a.patch //重新回覆 覆蓋之前的patch cat a.txt //重新開啟a.txt檔案 ``` ![](https://i.imgur.com/Dq7F86P.png) # 14. File System | | | | | -------- | -------- | -------- | | ![](https://i.imgur.com/iINW42M.png)|![](https://i.imgur.com/JGsMNEc.png)| | Soft link 有點像是指標 舉例softLink.txt 不像hardLink.txt直接指向 ```= mkdir files //建立資料夾 cd files/ //進入資料夾 touch hi.txt //建立一般純文字檔 hi.txt ls -lh //查看目前 ln -s hi.txt hi_sym.txt //soft link hi.txt檔案,指向hi_sym.txt ll //查看目前 ll是ls -lh 看快捷鍵 ln hi.txt hi2.txt //hard link hi2.txt ls -i //查看細節 ``` :::success ![](https://i.imgur.com/uUiukLh.png) ![](https://i.imgur.com/7kPTZQz.png) ![](https://i.imgur.com/YLpsJE8.png) ::: # 15. File Permissions ## 檔案符號 ![](https://i.imgur.com/xKU5yJ4.png) ## owner read write 別人不行操作 ```= r re permission w write permission x executable permission - do not have permission ``` ```= chmod 600 hi.txt //6 110 rw- 0 ---- 0 ---- ll //-rw------ 二進位的6是 110 二進位的0是 --- 二進位的0是 --- ``` ![](https://i.imgur.com/8nmtBgv.png) ## 每個人都有權限 ```= chmod 777 hi.txt ll //-rwxrwxrwx會看到是可以read write ``` ![](https://i.imgur.com/4y1lpdB.png) ## 改變使用者從bob變成別人 ```= chown joy hi.txt //不允許 sudo chown joy hi.txt //打完密碼,就允許 ll cat hi.txt //因為使用者是別人,-rw------是打不開的 sudo hi.txt //強制打開 ``` ![](https://i.imgur.com/qi5XYBr.png) ![](https://i.imgur.com/rHwF6kY.png) ![](https://i.imgur.com/VYU2Ukq.png) ```= chmod 600 hi.txt //6 110 rw- 0 ---- 0 ---- ll //-rw------不能讀、改寫 chmod 777 hi.txt ll //-rwxrwxrwx 可以讀、改寫 chmod 600 hi.txt //改回不能讀、改寫 chown root hi.txt //改使用者,被電腦駁回 sudo chown root hi.txt //強制改使用者 ll cat hi.txt //不能開啟檔 sudo cat hi.txt //強制開啟檔案,成功,沒有阻止 ``` # 16. Bash Scripting: Tests and Ifs ## 用script印出變數程式碼 ```= 開啟shell vi script.sh ``` ```= script內部做編輯 A=10 //建立A變數 值是10 B=2 //建立B變數 值是2 C="This is interpolation A = $A" //字串與變數 D='This is not B!= $B' //全部都是字串 echo $C //印出C變數 echo $D //印出D變數 ``` ![](https://i.imgur.com/T58149q.png) > 從vim bash script跳出 (先按esc ->:q ->enter鍵) > * 結束: :q > * 存檔後結束: :wq >> 如果出現紅字說No write since last change (add ! to override) 如何退出,如何编辑保存? >>>这种情况下,多半是没有权限造成的,没有write的权限。 > 退出方法:ctrl+z > ```= 再次開啟shell sh script.sh //會印出剛剛的變數C This is interpolation A = 10 //會印出剛剛的變數D This is not B!= $B ``` ![](https://i.imgur.com/KR4uveA.png) ## script印出比大小 ```= E=4 [ $E -gt 2 ] //方法1 gt代表greater echo $? //0 印出0代表成功,印出其他代表失敗 [[ $E -gt 2 ]] //方法2 echo $? //0 印出0代表成功,印出其他代表失敗 ``` ![](https://i.imgur.com/EVf7U3u.png) > 盡量使用[[ ]] 因為單是單一括號[]的修正版 ## script印出 if elif else ```= vi script.sh //再次進入script if [[ -e cattable ]] //if 如果檔案cattable在的話 輸出0 then cat cattable elif [[ $B -gt $A ]] //else if B比A大 then echo "At least $B is greater than $A" else //else echo "All rest have failed" fi //結束 ``` ![](https://i.imgur.com/Y6ceIyB.png) ```= 執行script sh script.sh //上述if elif都不成立進入到else進出 ``` ![](https://i.imgur.com/cGJO2iW.png) # 17. Bash Scripting: Loops and Switches ## for loop範例1 ### vim script 建立五個檔案 ```= vim script.sh for (( i=0; i<5; i++ )) do touch "new_file_$i.txt" //建立出new_file_$0.txt new_file_$1.txt new_file_$2.txt 等等 echo "File number $i was created..." //印出 done ``` ![](https://i.imgur.com/nuLjIUs.png) ### shell run script ```= sh script.sh ls ``` ![](https://i.imgur.com/b7rMewf.png) ## for loop範例2 ### 寫script ```= vi script.sh //再次進入script寫 for new_file in $(ls new_file_*) //loop所有檔案名字是new_file_*的 do echo $new_file //印出所有檔名是 new_file的資料 done ``` ![](https://i.imgur.com/8MfCdSH.png) ### shell run script ```= sh script.sh //執行script ``` ![](https://i.imgur.com/QThjaK9.png) ## While loop範例 ### 寫script ```= vi script.sh while getopts "ab:" opt //getots把所有的選項都傳出來,字串ab,在找a 找b do case "$opt" in a) echo "The -a flag was used" ;; //印出字串 b) echo "The -b flag was used with ${OPTARG}" ;; //印出字串+變數 esac //case反過來寫 esac done ``` > 什麼都不會印出來 ![](https://i.imgur.com/mxvYpIi.png) ### run script ```= sh script.sh sh script.sh -a //印出The -a flag was used sh script.sh -a -b "hi" //印出The -a flag was used //印出The -b flag was used with hi ``` > -a ![](https://i.imgur.com/7XINnGD.png) > -b ![](https://i.imgur.com/gvYfWXW.png) > 全部 ![](https://i.imgur.com/6txYrYN.png) ## getopts 使用參數 :::danger getopts 是用參數的意思 ![](https://i.imgur.com/GiPTYsq.png) ![](https://i.imgur.com/bPavT5q.png) ::: # 18. Bash Scripting: Functions ## 函式範例一 印出函式字串Hi ## shell 進入 script ```= vi function.sh ``` ## bash裡寫 script ```= function hi { echo "Hello from a function!"; } hi ``` ![](https://i.imgur.com/fGVggkt.png) ## shell 執行 script ```= sh function.sh ``` ![](https://i.imgur.com/aBs6Cta.png) ## 函式範例二 印出函式字串並加上參數 Hi Johnny ## shell 進入 script ```= vi functions.sh ``` ## bash裡寫 script ```= function hi { echo "Hello to $1 from a function!"; } hi "Johnny" ``` ![](https://i.imgur.com/cznR3nc.png) ## shell 執行 script ```= sh function.sh ``` ![](https://i.imgur.com/wQZOHcH.png) # 19. Automating Your Job 題目:做一個script packager 目標:把icons檔案夾裡面的照片png檔案,移到packaged_images檔案夾裡面的子檔案夾 ![](https://i.imgur.com/OB6M2LX.png) ## PART1 ### 開啟vi ```= cd automating_your_job/ vi packager.sh ``` ### 寫入script ```= while getopts "d:" opt //d是參數 do case "$opt" in d) DIR=${OPTARG};; //OPTARG是參數賦值給 變數DIR esac done if [[ $DIR == '' ]] //如果使用者沒有傳東西 then echo "Please provide an image directory." //文字告訴 exit 1; fi ``` ### 執行script ```= sh packager.sh ls sh packager.sh icons/ //傳入檔案夾的檔名是icons sh packager.sh -d icons/ //-d參數 icons檔案夾的檔名icons ``` ![](https://i.imgur.com/K0Fxvzu.png) ## PART2 ### 開啟vi ```= vi function.sh ``` ### 寫入script ```= # Create new directory with result RESULTS_DIR="packaged_images" //路徑賦值給 變數RESULTS_DIR mkdir $RESULTS_DIR //建立一個檔案夾檔名是packaged_images for IMAGE in $(ls $DIR) do FIRST_LETTER=${IMAGE:0:1} //檔名0~1之間英文字賦值給 變數FIRST_LETTER TARGET_DIR="$RESULTS_DIR/$FIRST_LETTER" //照片/0-1英文字賦值給 變數TARGET_DIR if [[ -d $TARGET_DIR ]] //如果存在 then echo "Directory $TARGET_DIR already exists." else //如果不存在 mkdir $TARGET_DIR //建立 echo "Created $TARGET_DIR" //印出 fi //finally cp "$DIR/$IMAGE" "$TARGET_DIR/$IMAGE" //可以copy照片 到該處 done ``` ![](https://i.imgur.com/lnPV7oH.png) ## 執行script ```= sh packager.sh -d icons/ //因為沒有資料夾,所以會建立出f h r s t 資料夾 ``` 原本 ![](https://i.imgur.com/Nj0wgE1.png =80%x) 後來 ![](https://i.imgur.com/OtqDued.png =80%x) ## 開啟finder查看 ```= open . ``` # 20. Challenge: Automating Your Job - Refactoring ![](https://i.imgur.com/v7VmrS0.png) ![](https://i.imgur.com/WJsMkV7.png) ![](https://i.imgur.com/f5pkmD2.png) 自行測試<報錯> ![](https://i.imgur.com/07pZ9fk.png) ![](https://i.imgur.com/YYJvsFG.png) ```= function makeDirectoryIfNecessary { if [[ -d $1 ]] //如果存在 then echo "Directory $1 already exists." else //如果不存在 mkdir $1 //建立 echo "Created $1" //印出 } while getopts "d:" opt //d是參數 do case "$opt" in d) DIR=${OPTARG};; //OPTARG是參數賦值給 變數DIR esac done if [[ $DIR == '' ]] //如果使用者沒有傳東西 then echo "Please provide an image directory." //文字告訴 exit 1; fi # Create new directory with result RESULTS_DIR="packaged_images" //路徑賦值給 變數RESULTS_DIR mkdir $RESULTS_DIR //建立一個檔案夾檔名是packaged_images for IMAGE in $(ls $DIR) do FIRST_LETTER=${IMAGE:0:1} //檔名0~1之間英文字賦值給 變數FIRST_LETTER TARGET_DIR="$RESULTS_DIR/$FIRST_LETTER" //照片/0-1英文字賦值給 變數TARGET_DIR makeDirectoryIfNecessary $TARGET_DIR //呼叫函式 fi //finally cp "$DIR/$IMAGE" "$TARGET_DIR/$IMAGE" //可以copy照片 到該處 done ``` :::danger 未使用函式 ![](https://i.imgur.com/WJsMkV7.png) 使用函式 ![](https://i.imgur.com/L4WYU2x.png) ::: ## 刪除已建立檔案重新呼叫一次 ```= rm -rf package_images //刪除 sh packager.sh -d icons/ //執行 open . //finder開啟 ``` # 21. Conclusion | | | | | -------- | -------- | -------- | | ![](https://i.imgur.com/BwoAcgZ.png)|![](https://i.imgur.com/a9Ipk2V.png) | | [胡立學生分享 第一週git](https://yakimhsu.com/project/project_w1_CommandLine.html) ###### tags: `[Kodeco IOS]`
{"metaMigratedAt":"2023-06-17T22:01:13.277Z","metaMigratedFrom":"Content","title":"<font color=\"#8A2BE2\">[W1-1]</font> Command Line Basic","breaks":true,"contributors":"[{\"id\":\"9a3a8952-6722-4409-9c1c-f0a4207b786f\",\"add\":18010,\"del\":1140}]"}
Expand menu