Udacity Data Scientist Nanodegree : Prerequisite — Command Line Essentials

Joe Chao
3 min readApr 18, 2021

Command line

Terminal Cheatsheet for Mac (Basics)

echo — get the Shell to print messages

  • 驚嘆號有特殊意義,如果不想要用它的話就用單引號括起來

ls — list

  • ls . = ls
  • ls ~ :主目錄
  • ls -l:-l(long)
  • ls -l Documents/*.pdf:列出 Documents 中所有 pdf 檔

cd — change directory

  • cd .. 回到上一個資料夾(..:父目錄,.:當前目錄)
  • 分號:可以讓一行執行兩個命令,由左至右執行

pwd— print working directory

mkdir — make directory

mv — move

  • mv ‘Documents/1911.epub’ Documents/Books/ :把檔案移到後面的資料夾中
  • 可以用*一次大量移動,如果是 path 不用加單括號,檔案就要

curl — c url = see url

  • is used in command lines or scripts to transfer data.`
  • 如果是直接用 curl 加網站,只會顯示源代碼
  • curl -L 表重定向,加上這個才會真的導到網站
  • 下載文件:curl -o google.html -L ‘http://google.com’
  • 基本上網站都放進去單引號比較好,以防萬一被特殊字元用出問題

cat — catenate(concatenate)

  • 能讀取文件並輸出內容,能讀取任意數量的文件

less

  • 功能同cat,但是顯示的比較少
  • 可以按上下鍵去看,然後按q離開

rm — remove

  • 後面加文件,會直接刪掉文件,rm -i 可以避免這件事情
  • rmdir — remove directories

grep — global regular expression print

  • 在文本中搜索,加單詞跟檔案名
  • grep shell dictionary.txt | less:在文本中搜尋shell,並回傳給less指令
  • grep -c 也可以達成wc

wc — world count

  • wc -l 計算列數

Shell and environment variables

  • 可以創造變數,e.g. numbers=’123' (不能有空白)
  • refer to the variable:put a dollar sign, e.g. echo $numbers
  • 不過 shell 有兩種變數,一個是 shell 本身,另一個是環境變數,通常與你執行的程式有關
  • 環境變數的例子:$PATH,告訴你你的程式路徑在哪
  • PATH=$PATH將路徑導到你在的這個路徑。後面加資料夾就是你現在可運行的資料夾,但是只能維持到shell關閉以前

還有自定義shell(暫時用不到,需要再回來看)

--

--

Joe Chao

會計背景,但目前在管顧實習。喜歡很多事情,於是都選擇把它一一記錄下來。