changed 8 months ago
Linked with GitHub

計算機概論Lab-11

LINUX-2

image

https://hackmd.io/@IMOK/Lab11


講師: 賴昱有


HOW TO 魔改 TERMINAL


Bash vs Z Shell


  • login shell 改成 zsh
    • chsh -s /usr/bin/zsh
  • 安裝 oh-my-zsh
    • sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  • 安裝 zsh theme (我用 Powerlevel10k)
    • git clone depth=1 https://github.com/romkatv/powerlevel10k.git \({ZSH_CUSTOM:-\)HOME/.oh-my-zsh/custom}/themes/powerlevel10k

  • 編輯 ~/.zshrc 修改 theme
    • ZSH_THEME="powerlevel10k/powerlevel10k"
  • 重整 shell
    • exec $SHELL
  • 安裝 zsh-syntax-highlighting
    • git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
    • echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
  • 載入設定檔
    • source ~/.zshrc

image


SSH

用法 : ssh {user}@{host}
e.g. ssh b0135XXXX@linux.aclab.cool

or 使用 GUI (Bitvise SSh Client)


VIM

文字介面編輯器


分成三種模式

  • normal mode
  • edit mode
  • visual mode

一開始是 normal mode 按 i 進入 edit mode

ESC 回到 normal mode


在 normal mode 打冒號可以輸入指令

Command Description
:w 儲存
:wq 儲存並退出
:q! 不儲存退出

vim 是個很強大的編輯器
在 normal mode 可以用的指令還有連技超多
如果想了解的可以看看 這個Vim

推薦要學 只能用文字介面的時候非常好用


JOE

另一個文字介面編輯器

joe filename.txt

operation description
Ctrl+K,H show help
Ctrl+K,F search text
Ctrl+K,L go to line
Ctrl+K,S save file
Ctrl+K,X save file and exit editor
Ctrl+Y delete a line

COMMANDS

如果不知道指令怎麼用 請參考
command help
man command


Quick search table


command usage description
chsh chsh /bin/tcsh 修改預設登入 shell
passwd passwd 修改密碼
chmod chmod 664 file 修改檔案權限
cd cd directory 進入資料夾
pwd pwd 顯示目前的目錄位置
echo echo "hello world" 輸出文字
cat cat file 檢視檔案內容
less less file 另開分頁查看內容

command usage description
head head -n 10 file 檢視檔案前10行
tail tail -n 10 file 檢視檔案最後10行
grep grep "apple" example.txt 擷取有關鍵字的那一行
cp cp file file_copy 複製檔案
mv mv file file_moved 移動、重新命名檔案
mkdir mkdir directory 創建資料夾
rmdir rmdir directory 刪除空目錄
touch touch file 創建檔案
rm rm file 刪除檔案

command usage description
chsh chsh /bin/tcsh 修改預設登入 shell
ypchsh ypchsh /bin/tcsh 在 NIS 機器上修改預設登入 shell
passwd passwd 修改密碼
yppasswd yppasswd 在 NIS 機器上修改密碼
ls ls -l 列出當前目錄下的檔案
ll ll ls -l的縮寫

command usage description
vim vim file 使用 vim 編輯檔案
df df -h 列出磁碟剩餘空間
du du –max-depth 1 -h . 列出資料夾所占空間
free free -h 列出記憶體使用狀況
who who 查看目前登入使用者
gcc gcc main.c 編譯 c 語言檔案
g++ g++ main.cpp 編譯 c++ 語言檔案

command description
gcc file.c 編譯 file.c,生成默認執行檔 a.out
gcc -o output file.c 編譯 file.c,輸出執行檔為 output
gcc -c file.c 只編譯生成目標檔案 file.o 不連結
gcc -E file.c -o file.i 只進行預處理,輸出為 file.i
gcc -S file.c 只產生組合語言檔案 file.s
gcc -v file.c 顯示詳細編譯過程


DF

列出磁碟剩餘空間
e.g.df -h

DU

列出資料夾所占空間
e.g. du max-depth 1 -h .

FREE

列出記憶體使用狀況
e.g. free -h


GCC

編譯 c 語言檔案
e.g. gcc main.c

G++

編譯 c++ 語言檔案
e.g. g++ main.cpp


PROCESSES AND JOBS


在指令後面加上 & 可以放到背景執行
e.g. sleep 60 &

ps 列出當前所有 process
jobs 可以看到正在背景執行的 process


執行程式時可以用組合鍵 Ctrl+c 傳送停止訊號

或是用 Ctrl+z 將程式暫停後放到背景

放到背景後可以用 fg %1 把它放到前景繼續執行

或是 bg %1 讓他在背景繼續執行


可以使用 kill %1 對程式發送訊號
kill 並不是只能發送停止訊號


REDIRECTION

overwrite:echo "string" > file.txt
append:echo "string2" >> file.txt


PIPE

cat file.txt | grep "apple"
cat /proc/meminfo | grep Pages | sort
du -d 1 -h | sort -h


WILDCARDS

ls *.txt
cat file *


ARCHIVING

GNU / Linux 各種壓縮與解壓縮指令 - 凍仁的筆記

  • 打包
    • tar cvf FileName.tar DirName
  • 解包
    • tar xvf FileName.tar
  • 打包且壓縮
    • tar zcvf FileName.tar.gz DirName
  • 解包
    • tar zxvf FileName.tar.gz

作業網站

作業做完請上傳到 http://140.121.197.13/tutorial
並且注意上傳時間限制、檔名、其他規範

上課時提前做完可以直接給助教 demo 登記
就不需要上傳 demo 過的部分


Question 1

Count the disk usage of your wwwdirectory.

Question 2

Write a program to find the prime numbers
between 1 and 10000 and compile it using gcc or g++.


Question 3

Execute the file and redirect the output to the file primes.txt.


Question 4

Insertion sort intro

Allow the user to input 10 numbers, then sort them using the insertion sort and
save the sorted result to a file isort10.txt.


Question 5

Complete the 99 multiplication table using only one loop
and output the result to a file named 1loop99.txt.
image

Select a repo