[總覽] Ubuntu
===
###### tags: `OS / Ubuntu`
###### tags: `OS`, `Ubuntu`, `linux`, `command`, `apt`, `grep`, `find`, `paste`, `xargs`
<br>
[TOC]
<br>
# Ubutu Application
## System (Ubuntu 18.04)
- Desktop shortcut
- [How do I hide all windows in Ubuntu 18.04 by one click?](https://askubuntu.com/questions/1072896/how-do-i-hide-all-windows-in-ubuntu-18-04-by-one-click)
> Settings -> Devices -> Keyboard -> Navigation -> Hide all normal windows
>
> [](https://i.imgur.com/GFk6oTm.png)
- [Add ‘Show Desktop’ button in Ubuntu 18.10, 18.04](https://ubuntuhandbook.org/index.php/2018/10/add-show-desktop-button-ubuntu-18-10-18-04/)
- 測試 Super+d
- 安裝 xdotool
```bash
$ sudo apt-get install xdotool
```
- 新增桌面捷徑 (desktop shortcut)
```
gedit ~/.local/share/applications/show-desktop.desktop
```
```
[Desktop Entry]
Type=Application
Name=Show Desktop
Icon=desktop
Exec=xdotool key --clearmodifiers Super+d
```
並在應用程式清單中,搜尋 show desktop

並加到 Favorite

<br>
- [Chrome Extensions](https://chrome.google.com/webstore/category/extensions?hl=en-US) / [[Ubuntu 外掛] GNOME Shell integration](https://chrome.google.com/webstore/detail/gnome-shell-integration/gphhapmejobijbbhgpjhcjognlahblep/related?hl=en-US)
- [Essential Guide: How to Install GNOME Extensions on Ubuntu 20.04 LTS](https://www.omgubuntu.co.uk/install-gnome-extensions-on-ubuntu)
- 裝了 Chrome App 後,點了就會導向到 [GNOME Shell Extensions](https://extensions.gnome.org/)
- web 界面管理
- 亦可在 show application 輸入 tweaks

- 有些不錯的 Gnome App 可選:
- [OpenWeather](https://extensions.gnome.org/extension/750/openweather/) (天氣資訊)
- [Dash to Panel](https://extensions.gnome.org/extension/1160/dash-to-panel/) (仿 Windows 外觀)
- Position: 控制工作列的位置
[](https://i.imgur.com/8rcHZV1.png)
- Style: 控制工作列的外觀
- Behavior
- 取消 App 群組(ungroup)

- [How to use Dash to Panel to make Gnome Shell look like Windows](https://www.youtube.com/watch?v=PxCMHGcE9Cs)
- create-text shortcut
- [Add “New Document” back to the right-click menu in Ubuntu 18.04](https://vitux.com/add-new-document-back-to-the-right-click-menu-in-ubuntu-18-04/)
```$ touch ~/Templates/"Untitled Document"```
<br>
<hr>
<br>
# Linux knowledge
## Q&A
- ### Memory
- [linux SWAP大小與記憶體的關係](https://www.itread01.com/content/1547656575.html)
> 交換分割槽並不是等到實體記憶體用盡了才使用的
> 是否儘量的使用或不使用swap,在核心空間有一個引數控制
- Ubuntu 16.04 Desktop 好像關不掉

- docker 和主機是連動的

- VM 也是會有自己的 swap (即使主機關掉 swapoff)

- [How to Permanently Disable Swap in Linux](https://www.tecmint.com/disable-swap-partition-in-centos-ubuntu/)

<br>
# Linux Server
## 憑證位置
- 取得憑證
```
$ curl-config --ca
/etc/ssl/certs/ca-certificates.crt
```
- [Git 透過 HTTPS 連線失敗:server certificate verification failed 解決方式](https://officeguide.cc/git-https-server-certificate-verification-failed-solution/)
<br>
# Linux Architecture
## /dev (device, 設備)
> 訪問外部裝置的埠
-
### /dev/null (空設備)
- 將錯誤訊息,導入到 /dev/null
```bash
$ find / something
...
find: ‘/var/opt/ds_agent’: Permission denied
find: ‘/var/lib/docker’: Permission denied
find: ‘/var/lib/udisks2’: Permission denied
...
```
將 Permission denied 這種錯誤訊息導入到空設備
```bash
$ find / something 2 > /dev/null # 3 表示 standard Error
```
- [/dev/null与/dev/zero详解](http://www.51testing.com/html/38/225738-235339.html)
> /dev/null 它是空设备,也称为位桶(bit bucket)或者黑洞(black hole)。你可以向它输入任何数据,但任何写入它的数据都会被抛弃。
<br>
## /etc (etcetera)
> 用來放置雜項:.conf 配置文件, .xml, .rule
- [Linux directory structure explained:/etc folder](https://www.linux.com/training-tutorials/linux-directory-structure-explainedetc-folder/)
- `/etc/passwd`
- `/etc/group`
<br>
## /usr (Unix System Resource)
- ### [Linux /usr/bin与/usr/local/bin使用区别](https://phoenixnap.com/kb/how-to-install-python-3-ubuntu)
- usr 指 Unix System Resource,而不是User
- /usr/bin 系統預先安裝的執行檔
- /usr/local/bin 使用者自行安裝的執行檔
- 若將執行檔安裝到 /usr/bin ,帶來的風險是:
系統升級時,可能覆蓋掉你的程式
- ### [Linux系統中的usr目錄有哪些](https://www.itread01.com/articles/1475925648.html)
- /usr/bin
- 所有可執行文件,如 gcc,Firefox 等(指不包含在 /sbin 和 /bin 內的);
- /usr/include/xxx.h
- 各種頭文件,編譯文件等時需要使用
- /usr/include/'package-name'
- 程序特定的頭文件
- /usr/lib
- 所以可執行文件所需要的庫文件
- ### [Linux系統/opt目錄和/usr/local目錄區別](https://mintrabbitplus.com/2020/10/16/linux%E7%B3%BB%E7%B5%B1-opt%E7%9B%AE%E9%8C%84%E5%92%8C-usr-local%E7%9B%AE%E9%8C%84%E5%8D%80%E5%88%A5/)
- #### `/opt` 目錄
- 可以理解為 Windows 中的 `D:/Software`
- 資料、函式庫,都放在相同目錄下
- 不需要時,直接 `rm -rf`
- #### `/usr/local` 目錄
- `/usr`:可以理解為 Windows 中的 `C:/Windows/`
- `/usr/lib`:可以理解為 Windows 中的`C:/Windows/System32`
- `/usr/local`:可以理解為 Windows 中的 `C:/Progrem Files/`
- 存放手動安裝的軟體 (不是通過 `apt-get` 安裝的軟體)
- ### [linux下面/usr/local和opt目錄有何區別](https://www.796t.com/content/1548490536.html)
<br>
## /var (system logs)
### `/var/log/auth.log`
```
$ sudo cat /var/log/auth.log | grep reboot -A 5 -B 5
Dec 5 10:28:14 stage-kube01 sudo: pam_unix(sudo:session): session opened for user root by tj_tsai(uid=0)
Dec 5 10:28:18 stage-kube01 sudo: pam_unix(sudo:session): session closed for user root
Dec 5 10:28:32 stage-kube01 sudo: tj_tsai : TTY=pts/10 ; PWD=/home/tj_tsai ; USER=root ; COMMAND=/usr/bin/apt upgrade
Dec 5 10:28:32 stage-kube01 sudo: pam_unix(sudo:session): session opened for user root by tj_tsai(uid=0)
Dec 5 10:31:02 stage-kube01 sudo: pam_unix(sudo:session): session closed for user root
Dec 5 10:31:27 stage-kube01 sudo: tj_tsai : TTY=pts/10 ; PWD=/home/tj_tsai ; USER=root ; COMMAND=/bin/systemctl reboot
Dec 5 10:31:27 stage-kube01 sudo: pam_unix(sudo:session): session opened for user root by tj_tsai(uid=0)
Dec 5 10:31:27 stage-kube01 sudo: pam_unix(sudo:session): session closed for user root
Dec 5 10:31:27 stage-kube01 systemd-logind[2856]: System is rebooting.
Dec 5 10:31:27 stage-kube01 sshd[3553]: Received signal 15; terminating.
Dec 5 10:37:28 stage-kube01 systemd-logind[2890]: New seat seat0.
Dec 5 10:37:28 stage-kube01 systemd-logind[2890]: Watching system buttons on /dev/input/event1 (Power Button)
Dec 5 10:37:28 stage-kube01 systemd-logind[2890]: Watching system buttons on /dev/input/event0 (Power Button)
Dec 5 10:37:35 stage-kube01 sshd[3714]: Server listening on 0.0.0.0 port 22.
```
<br>
## 其他參考資料
- [[鳥哥] Linux 檔案配置](https://linux.vbird.org/linux_basic/redhat6.1/linux_05file.php)
<br>
# Linux commands - basic
## 標準輸入 (standard in)
- [What does dash “-” at the end of a command mean?](https://unix.stackexchange.com/questions/41828/what-does-dash-at-the-end-of-a-command-mean)
<br>
## 建立使用者
- ### useradd vs adduser
- useradd 手動模式,設定要自己來
- adduser 互動模式,一次搞定
- [Cannot login to newly created user account](https://askubuntu.com/questions/159878/)
> If you created the user account with `useradd`, ++**you have to set up everything for it manually**++.
>
> This is why, when creating user accounts from the command-line, ++**it is recommended to use `adduser` in Ubuntu**++ (and Debian, and other Debian-based systems) instead.
>
> You might just want to remove the user with `userdel` or `deluser` and recreate it with `adduser`. Otherwise...
- [Linux 新增使用者 useradd 指令用法教學與範例](https://blog.gtwang.org/linux/linux-useradd-command-tutorial-examples/)
> 有些 Linux 發行版(例如 Ubuntu)則還會有一個 adduser 指令,這個指令是一個 Perl 指令稿,將新增使用者帳號的動作包裝起來,可讓管理者更容易使用,但其實它內部一樣是呼叫 useradd 指令來新增使用者。
- ### [How to add users to Docker container?](https://stackoverflow.com/questions/27701930)
```dockerfile=
# -m, --create-home
# create the user's home directory
# -s, --shell SHELL
# login shell of the new account
RUN useradd -ms /bin/bash newuser
```
- ### [Linux 新增使用者 useradd 指令用法教學與範例](https://blog.gtwang.org/linux/linux-useradd-command-tutorial-examples/)
- ### [Adding Users to Linux Groups and User Management](https://www.pluralsight.com/blog/tutorials/linux-add-user-command) :+1: :100:
- `/etc/passwd`

- `/etc/group`

- 主題
- Add a user to Linux
- Delete a user in Linux
- Add a new group to Linux
- Add a user to a group in Linux
- Change a password
- Modify a user in a Linux group
<br>
## 刪除使用者
- [移除 Linux 使用者](https://tw.godaddy.com/help/remove-a-linux-user-19158)
```bash=
# -r, --remove
# remove home directory and mail spool
userdel -r tj_tsai
```
<br>
## 取得使用者資訊
```bash
$ echo USER:$USER, uid:$(id -u), gid:$(id -g)
USER:tj, uid:1000, gid:1000
```
- 將檔案擁有者,變成當前的使用者
```
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
```
或是
```
$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
```
### 取得群組資訊
- 列出所有群組
```
cat /etc/group
```
- [將當前 user 加入到 group,避免一直要輸入 sudo](http://andy51002000.blogspot.com/2019/02/docker-permission-denied.html)
```bash
$ cat /etc/group | tail
...
docker:x:999:
```
```bash
# 將目前使用者,加入到 docker 群組
# -a, --append
# append the user to the supplemental GROUPS
# mentioned by the -G option without removing
# him/her from other groups
# -G, --groups GROUPS
# new list of supplementary GROUPS
$ sudo usermod -aG docker $USER
```
```bash
ubuntu@alprworker-1203417-iaas:~/tj_tsai/parabricks/dataset$ cat /etc/group | tail
...
docker:x:999:ubuntu
```
<br>
### 變更權限
- ### [Unix file permissions cheat sheet](https://www.bigbridge.nl/blog/unix-file-permissions-cheat-sheet/)

- user
- chmod u+r
- chmod u+w
- chmod u+x
- group
- chmod g+r
- chmod g+w
- chmod g+x
- others
- chmod o+r
- chmod o+w
- chmod o+x
- [How can I give write-access of a folder to all users in linux?](https://superuser.com/questions/19318)
<br>
## 重新導向
### 範例1
```bash=
# date.sh
echo '>>>'
echo 'e>>>' >&2
date +%T
echo '<<<'
echo 'e<<<' >&2
```
```bash
(sh date.sh 2>&1) > `date +%Y%m%d-%H%M`.log
```
- 沒有 `( ... )` 無法重導錯誤
```bash
sh date.sh 2>&1 > `date +%Y%m%d-%H%M`.log
```
### 參考資料
- [“> /dev/null 2>&1” 的意思](https://www.opencli.com/linux/dev-null-2-and-1-meanning)
- [[Linux] tee 指令:將結果同時輸出到螢幕和檔案](https://www.onejar99.com/linux-command-tee/)

- [In the shell, what does “ 2>&1 ” mean?](https://stackoverflow.com/questions/818255/)
- `2>1`
redirect stderr to a file named 1
- `2>&1`
- `&` indicates that what follows and precedes is a file descriptor and not a filename
- redirect stderr to the file descriptor 1
- [echo that outputs to stderr](https://stackoverflow.com/questions/2990414)
<br>
## 快捷鍵(shortcut)
- [在command line下移動游標(cursor)](https://medium.com/@herb123456/66b3655db0a9)
- [Shortcuts to move faster in Bash command line](http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/)
- 往前刪除一個 word
`Ctrl + w`, w: word
<br>
## alias
```bash=
# 設定簡短指令 (指令別名)
$ alias ku=kubectl
# 測試 ku 指令
$ ku version
# 移除指令別名
$ unalias ku
```
- 參考資料
- [kubectl 額外補充](https://medium.com/@C.W.Hu/kubernetes-helm-chart-tutorial-fbdad62a8b61)
- [How do I remove an alias?](https://askubuntu.com/questions/325368/how-do-i-remove-an-alias)
<br>
<br>
## base64
```bash=
$ echo -n "root" | base64
cm9vdA==
$ echo -n "password" | base64
cGFzc3dvcmQ=
```
```bash=
$ echo -n "root" | base64 | base64 -d
root
$ echo -n "password" | base64 | base64 -d
password
```
- [How can I decode a base64 string from the command line?](https://askubuntu.com/questions/178521/how-can-i-decode-a-base64-string-from-the-command-line)
<br>
## basename
> #basename, dirname
```bash
$ basename /a/b/c
c
$ basename a/b/c
c
$ basename /a/b/c/
c
$ basename a/b/c/
c
```
```bash
$ echo $(basename /a/b/c)
c
$ echo $(basename a/b/c)
c
```
```bash
$ dirname /a/b/c
/a/b
$ dirname a/b/c
a/b
$ dirname /a/b/c/
/a/b
$ dirname a/b/c/
a/b
```
<br>
## cat
- ### [How does "cat << EOF" work in bash?](https://stackoverflow.com/a/21549836/4359712)
```
$ cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF
```
- 範例
```bash
(cat <<-EOF
1
22
333
44
5
EOF
) > 12345.txt
```
```
$ cat 12345.txt
1
22
333
44
5
```
<br>
## chgrp
- [Give Read/Write Access to Group](https://askubuntu.com/questions/751264/)
- To make the directory owned by the group "webgroup", just do:
```
sudo chgrp webgroup /var/www
```
- [Allowing a group Read-Write Access to a directory](https://askubuntu.com/questions/488485/)
## chmod
- [Give Read/Write Access to Group](https://askubuntu.com/questions/751264/)
- To give the directory's group read/write access, just do:
```
sudo chmod g+rw /var/www
```
- If you want this to apply to accessing and creating directories as well, use the 'X' option, e.g.:
```
sudo chmod g+rwX /var/www
```
- if you want it to apply to all the folders and subfolders, use the 'recursive' option '-R', e.g.:
```
sudo chmod g+rwX /var/www -R
```
- [Allowing a group Read-Write Access to a directory](https://askubuntu.com/questions/488485/)
<br>
## command
```bash=
$ command curl
curl: try 'curl --help' or 'curl --manual' for more information
$ command -p curl
curl: try 'curl --help' or 'curl --manual' for more information
$ command -v curl
/usr/bin/curl
$ command -V curl
curl is hashed (/usr/bin/curl)
```
- `-p`
use a default value for PATH that is guaranteed to find all of the standard utilities
- `-v`
print a description of COMMAND similar to the `type' builtin
- `-V`
print a more verbose description of each COMMAND
<br>
## crontab
- [Linux 設定 crontab 例行性工作排程教學與範例](https://blog.gtwang.org/linux/linux-crontab-cron-job-tutorial-and-examples/)
| 欄位 | 說明 | 可設定的值 |
|------|-----|-----------|
| MIN | 分鐘 | 0 到 59 |
| HOUR | 小時 | 0 到 23 |
| DOM | 日 | 1 到 31 |
| MON | 月份 | 1 到 12,此欄位亦可用英文簡稱取代,例如一月也可以寫 Jan。 |
| DOW | 星期幾 | 0(週日)到 6(週六),7 也代表週日。此欄位亦可用英文簡稱取代,例如週日也可以寫 Sun。 |
| CMD | 要定期執行的指令 | 任何可執行的程式或指令稿(包含參數),例如 /path/to/cmd --your --parameter。 |
<br>
## cut
### `$ cut --help`
```
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-b, --bytes=LIST select only these bytes
-c, --characters=LIST select only these characters
-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter
-f, --fields=LIST select only these fields; also print any line
that contains no delimiter character, unless
the -s option is specified
-n (ignored)
--complement complement the set of selected bytes, characters
or fields
-s, --only-delimited do not print lines not containing delimiters
--output-delimiter=STRING use STRING as the output delimiter
the default is to use the input delimiter
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
```
### 範例1
```bash=
# -d: delimiter
# -f: field index
$ python --version | cut -d " " -f 1
python
$ python --version | cut -d " " -f 2
3.6.12
```
- 等效 awk 指令
```bash
python --version | awk '{print $1}'
python --version | awk '{print $2}'
```
### 範例2
```bash=
# -d: delimiter
# -f: field index
$ echo filename.txt | cut -d '.' -f 1
filename
$ echo filename.txt | cut -d '.' -f 2
txt
```
- 等效 awk 指令
```bash
echo filename.txt | awk -F . '{print $1}'
echo filename.txt | awk -F . '{print $2}'
```
- `-F fs`: `--field-separator fs`
<br>
## bc (calculate) & expr (expression)
### $(( expression )) 內建指令
- 操作範例
```bash
# 透過 $((運算式))
$ echo $(((1+2)*3))
9
$ echo $((5/3))
1
```
```bash
$ s=1
$ e=12
$ echo $(( e - s ))
11
$ echo $(( ( e - s ) / 10 ))
1
$ echo $(( ( e - s ) / 5 ))
2
```
- 時間戳記 timestamp
```baash
date && start_time=`date +%s` && echo "start_time:" $start_time
sleep 12 # do something
date && end_time=`date +%s` && echo "end_time:" $end_time
total=$((end_time - start_time))
time_h=$((total/3600))
time_m=$(( (total - time_h*3600) / 60 ))
time_s=$(( total - time_h*3600 - time_m*60 ))
echo "Total time: ${time_h}h ${time_m}m ${time_s}s"
```
<br>
### 透過 python
```bash
date && start_time=`date +%s` && echo "start_time:" $start_time
sleep 4
date && end_time=`date +%s` && echo "end_time:" $end_time
echo "print(\'Total\ time:\',"$end_time"-"$start_time",\'(sec)\')" | xargs python3 -c
```
<br>
### bc
- ### 安裝 bc
```
sudo apt install bc # Debian/Ubuntu
```
- ### [Linux 計算機 bc 指令用法教學與範例](https://blog.gtwang.org/linux/linux-bc-command-tutorial-examples/)
```bash
$ echo "(1+2)*3"|bc
9
```
```
$ echo 5/3 | bc
1
$ echo 5/3 | bc -l
1.66666666666666666666
```
- 時間戳記 timestamp
```bash=
printElapsedTime(){
label=$1
start_time=$2
end_time=$3
elapsed_time=`echo $end_time-$start_time | bc`
hour=`echo $elapsed_time/3600 | bc`
minute=`echo "($elapsed_time-$hour*3600)/60" | bc`
second=`echo $elapsed_time-$hour*3600-$minute*60 | bc`
echo "[$label] elapsed_time: ${hour}h ${minute}m ${second}s (${elapsed_time} sec)"
}
export start_time=`date +%s`
echo start_time=$start_time
sleep 1
export end_time=`date +%s`
echo end_time=$end_time
printElapsedTime "MyTask" $start_time $end_time
```
- [Can I export a variable to the environment from a Bash script without sourcing it?](https://stackoverflow.com/questions/16618071/)
### expr
- ### [How to do Basic Math in Linux Command Line](https://vitux.com/how-to-do-basic-math-in-linux-command-line/)
```
$ expr \( 1 + 2 \) \* 3 / 9
1
```
<br>
## echo
- ```-n```
do not output the trailing newline
不要印出行尾换行
- ```-e```
enable interpretation of backslash escapes
啟用背斜線跳脫解釋
- 跳脫 `'` (單引號, single quote)
```sh
echo 'ab'
echo 'a''b'
echo 'a'""'b'
echo 'a'"'"'b'
echo 'a'"x"'b'
echo "ab"
echo "a""b"
echo "a\"b"
echo "a\'b"
echo "a\tb"
echo "a'b"
```
執行結果:
```
ab
ab
ab
a'b
axb
ab
ab
a"b
a\'b
a b
a'b
```
- [How to escape single quotes within single quoted strings](https://stackoverflow.com/questions/1250079)
<br>
## egrep
- ### case1: https://stackoverflow.com/a/11568973/4359712
```bash=
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | egrep '[0-9]+G[0-9]+'
This is 02G05 a test string 20-Jul-2012
Hello 09G09
# -o, --only-matching
# show only the part of a line matching PATTERN
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | egrep -o '[0-9]+G[0-9]+'
02G05
09G09
```
- ### case2: 取得 Ubuntu 版本資訊
```bash
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
# -r, --release
# show release number of this distribution
$ lsb_release -r
Release: 16.04
# -o, --only-matching
# show only the part of a line matching PATTERN
$ lsb_release -r | egrep -o [0-9.]+
16.04
$ version=`lsb_release -r | egrep -o [0-9.]+`
$ echo $version # or echo ${revision}
16.04
```
- ### 備註
- 不能使用 `\d`, 要用 `[0-9]`
- 不能使用 `/t`, 要用 `\s` (?)
- ### 參考資料
- [Pattern matching digits does not work in egrep?](https://stackoverflow.com/questions/3185457)
- `egrep` doesn't recognize `\d`
- use `[0-9]`
<br>
## exec
- [What does this command do? "exec bash -l"](https://stackoverflow.com/questions/26747455/)
`exec` executes a specified command, replacing the current process rather than starting a new subprocess.
```
bash -l
```
- the `-l` makes it a login shell
<br>
## find
- `find / -type d -name "enroot"`
- 從跟目錄下找目錄,目錄名稱為 enroot
- ```find -not -path "./public/*" -type f -name "*.js" -exec grep -H "K8SCluster" {} \;```
- ```-not -path "./public/*"```
- 排除子目錄,及其子目錄
- ```-type f```
- 檔案類型為 file (非目錄)
- ```-name "*.js"```
- 檔名以 .js 結尾
- ```-exec COMMAND {}```
- 為動作(actions)指令
- 此處執行 grep "K8SCluster"
- ```-H``` 表示印出檔案路徑
```-H, --with-filename print the file name for each match```
- `-H`
- 印出檔案名稱
<br>
- ```-not -path "./public/*"``` 等效於
- ```find -path ./public -prune -o -type f -name "*.js" -exec grep -H "K8SCluster" {} \;```
- ```-o``` 表示 'or' (find 的運算子)
- 'or' 去串接後面的指令(?)
- ```find -path ./public -prune -or -type f -name "*.js" -exec grep -H "K8SCluster" {} \;```
- 參考資料
- [How to explicitly exclude directory from find command's search](https://linuxconfig.org/how-to-explicitly-exclude-directory-from-find-command-s-search)
- [What is the meaning of “\;” in “find” command?](https://unix.stackexchange.com/questions/439291/what-is-the-meaning-of-in-find-command)
- 分號 `\;` 告訴 `find` 指令,`-exec` 指令已經結束
- 以下皆可
> `find . '(' -type f -exec basename {} ';' ')' -o '(' -type d -exec ls {} ';' ')'`
>
> `find . '(' -type f -exec basename {} \; ')' -o '(' -type d -exec ls {} \; ')'`
- [What does “{} \;” mean in a Linux command?](https://askubuntu.com/questions/339015/what-does-mean-in-a-linux-command)
- 分號 `\;` 視為 `find` 指令的,而不是 `-exec` 指令的
- ```\;``` 每個檔案執行一次 exec 指令
- ```+``` 全部檔案做完,再執行 exec 指令
<br>
- ```find -maxdepth 1 -name '*.hdf5' -name '*--0.000?.hdf5' -regextype sed -regex '.*[5-9].hdf5'```
- sample:
```
./M97.char8-CRNN-augmented-colorful-Adadelta--189--0.0461--0.0011.hdf5
./M97.char8-CRNN-augmented-colorful-Adadelta--103--0.0569--0.0111.hdf5
./M97.char8-CRNN-augmented-colorful-Adadelta--179--0.0427--0.0005.hdf5
./M97.char8-CRNN-augmented-colorful-Adadelta--187--0.0491--0.0006.hdf5
./M97.char8-CRNN-augmented-colorful-Adadelta--209--0.0412--0.0007.hdf5
./M97.char8-CRNN-augmented-colorful-Adadelta--329--0.0424--0.0008.hdf5
```
- doc
> positional options (always true): -daystart -follow -regextype
<br>
- [How can I exclude all “permission denied” messages from “find”?](https://stackoverflow.com/questions/762348/how-can-i-exclude-all-permission-denied-messages-from-find)
```find / 2>/dev/null | grep cudam```
- [Find number of files in folder and sub folders?](https://askubuntu.com/questions/34099/find-number-of-files-in-folder-and-sub-folders)
```
find . -type f | wc -l
```
- 遞迴刪除目錄中的 非 `.py` 檔
```
$ find ./workspace/ -type f -not -name '*.py' -exec rm '{}' \;
```
- 遞迴刪除目錄中的 `__pycache__` 目錄
```
$ find ./workspace/ -type d -name '__pycache__' -exec rm -rf {} \;
```
- 排解
```
find: ‘./docker/workspace/model/__pycache__’: No such file or directory
find: ‘./docker/workspace/model/featurizer/__pycache__’: No such file or directory
```
一直會有 No such file or directory
主要是因為目錄已經被移除
導致 find 接下來,要印出該「檔案路徑」會找不到檔案

- [What does “{} \;” mean in a Linux command?](https://askubuntu.com/questions/339015/what-does-mean-in-a-linux-command)
- ```\;``` 每個檔案執行一次 exec 指令
- ```+``` 全部檔案做完,再執行 exec 指令
- 正確作法
```
$ find ./workspace/ -type d -name '__pycache__' -exec rm -rf {} +
```
### find a directory
- ### [How To Find a Directory On Linux Based System](https://www.cyberciti.biz/faq/howto-find-a-directory-linux-command/)
```
find / -type d -name "apt" -print 2>/dev/null
```
- `2>/dev/null`: 忽略 Permission denied 訊息
`find: ‘/path’: Permission denied`
- `-print`: 好像沒作用
### find a keyword and replace it
- 範例1:找出 TwsRequestOperation 置換成 OneaiRequestOperation
```bash
find -name '*.ipynb' -exec sed -i 's/Tws/Oneai/g' {} \;
```
- 參考資料
- [Find and replace text within a file using sed command](https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/)
- `sed -i 's/old-text/new-text/g' input.txt`
- `-i[SUFFIX], --in-place[=SUFFIX]`
edit files in place (makes backup if SUFFIX supplied)
- s: the substitute command of sed for find and replace
<br>
## grep
- ### [How to return both file name and line number with find ... -exec grep?](https://unix.stackexchange.com/questions/53072/)
- 差異比較
```
$ grep with main4.py
with open("pairs.txt", "r") as f:
$ grep -Hn with main4.py
main4.py:7:with open("pairs.txt", "r") as f:
```
- 多了 `main4.py:7:`
- ### [How To Search Multiple Words / String Pattern Using grep Command on Bash shell](https://www.cyberciti.biz/faq/searching-multiple-words-string-using-grep/)
- ### [How can I grep hidden files?](https://stackoverflow.com/questions/10375689)
```
grep -r search * .[^.]*
```
- ### [How can I exclude directories from grep -R?](https://stackoverflow.com/questions/6565471)
- ### [Error: grep: find: Is a directory](https://stackoverflow.com/questions/45046873)
```
grep -d skip alex /etc/*
```
- ### [Display filename before matching line](https://stackoverflow.com/questions/15432156/)
- `-H, --with-filename`
Print the file name for each match. This is the default when there is more than one file to search.
- `-n, --line-number`
Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)
<br>
### regular expression
- [grep 使用 Regex 找出相符字元的語法](https://blog.longwin.com.tw/2018/11/linux-grep-regex-match-keyword-2018/)
grep 要使用 regex 來比對字串,主要是使用 -P 的參數
```bash
grep -P '\d\d' filename # 找出符合兩個數字的
grep -P '[\d\d|全滿天]' filename # 找出符合兩個數字或「全」、「滿」、「天」任一字元都印出來
grep -P '(full|keyword)' filename # 找出 full 或 keyword 完全比對的關鍵字
```
<br>
## getent
- ### 我要如何查 uid = tjtsai 所對應的 label
要查詢 `uid = tjtsai` 對應的使用者名稱或標籤,您可以使用以下 Linux 指令:
1. **查詢對應的使用者名稱:**
使用 `getent passwd` 命令來查詢特定的使用者資訊:
```
$ getent passwd tjtsai
```
這會顯示與 `tjtsai` 相關的使用者資訊,包含使用者名稱、UID、GID、主目錄和登入 shell。
2. **查詢 UID 的對應名稱:**
如果您知道 `uid` 而不是 `tjtsai`,可以使用以下命令來反向查詢對應的使用者名稱:
```
$ getent passwd <UID>
```
例如,若您要查詢 UID `15444` 的對應名稱:
```
$ getent passwd 15444
tjtsai:x:15444:200231:tjtsai:/home/tjtsai:/bin/bash
```
這將列出對應該 UID 的使用者資訊,包括使用者名稱和相關的其他詳細資訊。
如果這些帳號資訊儲存在 LDAP 或 NIS 中,`getent passwd` 也會查詢這些外部目錄服務,並且不僅限於本地系統的 `/etc/passwd`。
- ### getent 是什麼縮寫?
`getent` 是 **"get entries"** 的縮寫。它是 Linux/Unix 系統中的一個命令,用來從系統資料庫中檢索條目(entries),這些資料庫可能是本地的 `/etc` 文件(如 `/etc/passwd`、`/etc/group`),也可能是來自網絡目錄服務(如 LDAP、NIS)。
`getent` 可以從多種系統資料庫中檢索條目,包括:
- **passwd**:用來檢索使用者帳戶資訊。
- **group**:用來檢索群組資訊。
- **hosts**:用來檢索主機名和 IP 地址。
- **services**:用來檢索網路服務名稱和對應的端口號。
- **protocols**:網絡協議。
- **networks**:網絡名和網絡號。
這個命令的好處在於它會根據系統的配置查詢多個來源(如 `/etc` 檔案、LDAP、NIS 等),而不僅限於本地系統。
```
getent passwd u1234567
```
```
getent passwd <UID>
```
<br>
## gzip
- ### [How to Unzip (Open) Gz File](https://linuxize.com/post/how-to-unzip-gz-file/)
```
gzip -dk file.gz
```
- `-d, --decompress`
decompress
- `-k, --keep`
keep (don't delete) input files
- `.tar.gz`, `.tar` 請使用 tar 指令
<br>
## ipython
### 安裝
```
$ python -m pip install ipython
```
### 衡量時間 | 時間差 | 時間戳記
- ### case 1
```
%%time
!sleep 1.5
```

- ### case 2
```
%%time
!docker login
```

<br>
## json
> #json
### `json_pp` (to json)
```
$ echo '{"name": "tj", "age": 25, "hobbies":["play", "exercise", "swim"]}' | json_pp
{
"age" : 25,
"hobbies" : [
"play",
"exercise",
"swim"
],
"name" : "tj"
}
```
- [On man json_pp clarify that "pp" stands for "pure perl" not "pretty printer".](https://github.com/makamaka/JSON-PP/issues/43)
- pp = pure perl
### `jq` (to json)
> `sudo apt install jq`
```
$ echo '{"name": "tj", "age": 25, "hobbies":["play", "exercise", "swim"]}' | jq
{
"name": "tj",
"age": 25,
"hobbies": [
"play",
"exercise",
"swim"
]
}
```
- jq: JSON Query
<br>
## kill
- ### [kill与kill -9的区别](https://blog.csdn.net/u010486679/article/details/78415666)

- kill 发送SIGTERM(15)信号给进程,告诉进程,你需要被关闭,请自行停止运行并退出。
- kill -9 发送SIGKILL信号给进程,告诉进程,你被终结了,请立刻退出。
- TERM(或数字9)表示“无条件终止”;
- 因此 kill - 9 表示强制杀死该进程;
- 与SIGTERM相比,这个信号不能被捕获或忽略,同时接收这个信号的进程在收到这个信号时不能执行任何清理。
<br>
## md5sum
> Also see:
> - [sha256sum](#sha256sum)
```
$ md5sum WGS-LIS-AI018A_R1.fastq.gz
59444a39ffe845d390e110be16c4430a WGS-LIS-AI018A_R1.fastq.gz
$ md5sum WGS-LIS-AI018A_R2.fastq.gz
a3f1d78c156761f6a27bb74c0a4b87b1 WGS-LIS-AI018A_R2.fastq.gz
```
<br>
## nohup
- [Linux 的 nohup 指令使用教學與範例,登出不中斷程式執行](https://blog.gtwang.org/linux/linux-nohup-command-tutorial/)
> 當 Linux 使用者登出系統時,其所執行的每一個程式都會接收到一個 SIGHUP(hangup)這個信號,正常的程式收到這個信號之後,就會馬上停止執行。
>
> 如果想讓程式可以在離線或登出之後繼續執行,可以使用 nohup 這個指令來執行程式,這個指令可以讓程式忽略 SIGHUP 這個信號,所以當使用者登出或是斷線後,程式也可以正常執行,不會受到任何影響。
- [Linux nohup command](https://www.computerhope.com/unix/unohup.htm)
<br>
## openssl
```
$ openssl rand -base64 14
Nnvyng0+ygUU/hkc1Cw=
```
<br>
## logout
- [ubuntu關機、重啟、登出命令列指令](https://www.itread01.com/content/1548532466.html)
```
$ logout
Connection to 203.145.218.3 closed.
```
<br>
## less
- 顯示壓縮檔中的檔案清單
```bash=
$ less zipfile
# or
$ unzip -l zipfile
```
- [View list of files in ZIP archive on Linux](https://superuser.com/questions/216617)
<br>
## ln
### symbolic link
```
$ ln -s test.sh a/b/test.sh
ln: failed to create symbolic link 'a/b/test.sh': No such file or directory
```
- target 的路徑必須事先建立
```
$ mkdir -p a/b
$ ln -s test.sh a/b/test.sh
$ sh a/b/test.sh
sh: 0: Can't open a/b/test.sh
```
- source 不能是相對路徑
```
$ ls a/b/test.sh
ls: cannot access 'a/b/test.sh': Too many levels of symbolic links
$ ls -ls a/b/test.sh
0 lrwxrwxrwx 1 tj tj 7 一 21 12:16 a/b/test.sh -> test.sh
```
<br>
## ls
- 範例1
```bash
$ ls -ls
```
- ```-s, --size```
- print the allocated size of each file, in blocks
- ```-l```
- use a long listing format
- 範例2
```bash
$ ls
ebook software workplace
$ $ ls {ebook,software}
ebook:
...
software:
...
```
<br>
## paste
- `$ echo -e 'A\nBB\nCCC' | paste -s -d ','`
- `$ echo -e 'A\nBB\nCCC' | paste -s -d','`
- `$ echo -e 'A\nBB\nCCC' | paste -sd ','`
- `$ echo -e 'A\nBB\nCCC' | paste -sd','`
- 執行結果
```
A,BB,CCC
```
- `-d, --delimiters=LIST`: reuse characters from LIST instead of TABs
- `-s, --serial`: paste one file at a time instead of in parallel
- `$ echo -e 'A\nBB\nCCC' | paste`
```
A
BB
CCC
```
- `$ echo -e 'A\nBB\nCCC' | paste -s`
`A BB CCC`
(TABs)
<br>
## pdf
- [How to merge several PDF files?](https://askubuntu.com/questions/2799)
```bash
$ sudo apt install pdftk-java
# or
$ sudo snap install pdftk
$
pdftk file1.pdf file2.pdf cat output mergedfile.pdf
```
<br>
## passwd
- 配置使用者帳號/密碼
```
$ useradd ubuntu
$ passwd ubuntu
$ usermod -aG sudo ubuntu
```
- `-a` 表示使用者可以同時存在於多個 group
- 沒有 `-a` 的話,user 加入新的 group ,舊的 group 就會被移除
- [Alternative to passwd --stdin in Ubuntu](https://askubuntu.com/questions/1115795)
```
$ echo "username:cleartext_password" | sudo chpasswd
```
<br>
## pgrep
- https://github.com/ollama/ollama/issues/690
```
$ pgrep ollama
74877
$ kill 74877
```
<br>
## ps
- [How to test what shell I am using in a terminal?](https://unix.stackexchange.com/questions/9501)
```bash=
$ ps -p$$
PID TTY TIME CMD
66048 pts/12 00:00:00 bash
$ ps -p$$ -ocmd
CMD
-bash
$ ps -p$$ -ocmd=
/bin/bash
```
```shell=
# ps -p$$ -ocmd=
/bin/sh -l
```
```bash=
$ dash
$ ps -p$$ -ocmd=
dash
```
也可以這麼做:
```
$ echo $0
bash
```
```
$ echo $0
/bin/sh
```
- [#!/bin/bash和#!/bin/sh的區別,source命令和exec命令](https://www.itread01.com/content/1546346172.html)
- [How can I show a terminal shell's process tree including children?](https://unix.stackexchange.com/questions/264522)
```
$ ps -aef --forest
```
- `-a`
Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
- `-e`
Select all processes. Identical to -A.
- `-f`
Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the format keyword comm.
- [Showing memory usage in Linux by process and user](https://www.networkworld.com/article/3516319) :+1: :+1: :+1: :100:
```
$ ps aux | sort -rnk 4 | head -5
```
- `-r` (reverse)
- `-n` (numeric)
- `-k` (key)
- [Sorting down processes by memory usage](https://unix.stackexchange.com/questions/92493/)
```
ps aux --sort -rss
```
```
ps aux --sort -rss | head -n15
```
- 如何查啟動該 process 所對應的執行檔
- case1
```
$ ps -aux | grep py
...
tj 647513 0.0 0.0 21640 6036 pts/1 S+ 十一22 0:00 python main4.py
tj 3117291 0.0 0.0 16756 4528 pts/17 S+ 12:59 0:00 grep --color=auto py
```
- 只知道 `python main4.py`
<br>
```
$ pwdx 647513
647513: /home/tj/Asus/workplace/english-chinese
```
- 知道工作目錄:`/home/tj/Asus/workplace/english-chinese`
<br>
```
$ lsof -p 647513
lsof: WARNING: can't stat() tracefs file system /sys/kernel/debug/tracing
Output information may be incomplete.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 647513 tj cwd DIR 8,2 4096 26542221 /home/tj/Asus/workplace/english-chinese
...
```
- 參考資料
- [How can I know the absolute path of a running process?](https://superuser.com/questions/103309)
<br>
## realpath
- 回到當前目錄上一層,避免直接使用 `..`,會有 path traversal 問題
```
echo $(realpath $(pwd)"/..")
```
- case
```
~$ echo $(realpath $(pwd)"/..")
/home
```
<br>
## reset
```
$ reset
```
- [How to really clear the terminal?](https://askubuntu.com/questions/25077)
<br>
## sed
### 刪除整行
用法:`sed -i '/要找的字串/d' 目標檔案`
- `-i` 表示 inplace
- [使用sed -i --和sed -i -e 搜尋與取代文字](https://balian-ear.medium.com/x-74808dc91bb2)
- [[Nvidia][forums]](https://forums.developer.nvidia.com/t/the-repository-https-developer-download-nvidia-com-compute-cuda-repos-ubuntu1804-x86-64-release-is-not-signed/193764/11?u=tj_tsai)
`sudo sed -i '/developer\.download\.nvidia\.com\/compute\/cuda\/repos/d' /etc/apt/sources.list`
<br>
### [How to split a string by pattern into tokens using sed or awk](https://stackoverflow.com/questions/32590201/how-to-split-a-string-by-pattern-into-tokens-using-sed-or-awk)
- input
```
filename.b.c
```
- output
```
filename
b
c
```
- command1
```bash
$ echo filename.b.c | sed 's/\./\n/g'
```
- ```s/~/ /g```
> substitute everything that matches the regular expression ~ with a space, globally (on the whole input line) ([取自](https://unix.stackexchange.com/questions/484659/what-does-this-command-do-sed-s-g))
- command2
```
$ echo filename.b.c | awk -F '.' '{for (i = 1; i <= NF; i++) print $i}'
```
- [command3](https://unix.stackexchange.com/questions/484659/what-does-this-command-do-sed-s-g)
```
$ echo filename.b.c | tr '\.' '\n'
```
- tr - translate or delete characters
### 應用
- 在 notebook cell 裡,與 shell 交互應用
#jupyterlab + notebook cell + python + dir + bash shell + echo + interactive
```python
x = dir(...)
!echo $x | sed 's/,/\n/g' | grep find
```

<br>
### misc
- case1
```bash=
# lsb_release
# -r, --release
# show release number of this distribution
#
# sed
# -n, --quiet, --silent
# suppress automatic printing of pattern space
# -r, --regexp-extended
# use extended regular expressions in the script.
# p: print the substitution
$ lsb_release -r | sed -rn 's/.*?\s+([0-9]+)/\1/p'
16.04
# 若沒有 sed 的 -n
$ lsb_release -r | sed -r 's/.*?\s+([0-9]+)/\1/p'
16.04
16.04
# 若沒有 sed 的 p (不要印出匹配到的字串)
$ lsb_release -r | sed -r 's/.*?\s+([0-9]+)/\1/'
16.04
# 若沒有 sed 的 -r
# 則 reg-exp 部份運算子,需要使用逸出表示
# (...) -> \(...\)
# + -> \+
# ? -> \?
# * -> 不需要
$ lsb_release -r | sed -n 's/.*\?\s\+\([0-9]\+\)/\1/p'
16.04
```
- case2: https://stackoverflow.com/a/11568973/4359712 ???
```bash=
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | sed '/[0-9]\+G[0-9]\+/p'
This is 02G05 a test string 20-Jul-2012 # src
This is 02G05 a test string 20-Jul-2012 # dest
Hello 09G09 # src
Hello 09G09 # dest
# -n, --quiet, --silent
# suppress automatic printing of pattern space
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | sed -n '/[0-9]\+G[0-9]\+/p'
This is 02G05 a test string 20-Jul-2012 # dest
Hello 09G09 # src
```
```bash=
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | sed 's/.*\([0-9]\+G[0-9]\+\).*/\1/p'
2G05
2G05
9G09
9G09
$ echo -e "This is 02G05 a test string 20-Jul-2012\nHello 09G09" | sed -n 's/.*\([0-9]\+G[0-9]\+\).*/\1/p'
2G05
9G09
```
<br>
## set
- [学习笔记: shell 中的 set -e , set +e 用法](https://blog.csdn.net/xiaofei125145/article/details/39345331)
> `set -e` 可以保证后续命令正常执行
> 可见设置"set -e",在脚本开发过程中可能很有帮助,
>
> 而在开发完成后,
> 特别是对于后期可能有升级的脚本,则可能是埋下了安全隐患。
```bash
# 如果出现了返回值为 非零 ,整个脚本就会立即退出
# Exit the script if an error happens
set -e
脚本A
```
```bash
# 如果出现了返回值为 非零 ,整个脚本就会立即退出
# back to regular "bail out on error" mode
set -e
脚本B
```
```bash
# 如果出现了返回值为 非零 ,将会继续执行下面的脚本 C
# don't bail out of bash script if cache doesn't exist
set +e
脚本C
```
- `$ set --help`
```
Set or unset values of shell options and positional parameters.
Change the value of shell attributes and positional parameters, or
display the names and values of shell variables.
Options:
-e Exit immediately if a command exits with a non-zero status.
```
<br>
## sha256sum
> Also see:
> - [md5sum](#md5sum)
```
$ sha256sum Miniconda3-py39_4.10.3-Linux-x86_64.sh
1ea2f885b4dbc3098662845560bc64271eb17085387a70c2ba3f29fff6f8d52f Miniconda3-py39_4.10.3-Linux-x86_64.sh
```
<br>
## sleep
- 永久卡住 process
```
$ sleep infinity
```
- [What does #(nop) mean in docker history?](https://stackoverflow.com/a/63927713/4359712)
<br>
## sort
### sort by a column
- DEMO1
```bash
$ echo -e '3\t3c\n11\t11k\n2\t2b\n1\t1a'
3 3c
11 11k
2 2b
1 1a
```
```bash
$ echo -e '3\t3c\n11\t11k\n2\t2b\n1\t1a' | sort -t $'\t' -k1,2
11 11k
1 1a
2 2b
3 3c
```
```bash
$ echo -e '3\t3c\n11\t11k\n2\t2b\n1\t1a' | sort -t $'\t' -k1,2n
1 1a
2 2b
3 3c
11 11k
```
```bash
$ echo -e '3\t3c\n11\t11k\n2\t2b\n1\t1a' | sort -t $'\t' -k1,1n -k2,2
1 1a
2 2b
3 3c
11 11k
```
- 參考資料
- [all coding regions .bed file hg38 Whole Genome Sequencing](https://www.biostars.org/p/459269/)
`sort -T . -t $'\t' -k1,1 -k2,2n`
<br>
## sudo
### 如何導入密碼
- [pipe password to `sudo` and other data to `sudo`ed command](https://unix.stackexchange.com/questions/391796/)
```
{ echo 'mypassword'; echo 'some text'; } | sudo -k -S tee -a /etc/test.txt &>/dev/null
```
- `-S, --stdin`: read password from standard input
- `-k, --reset-timestamp`: invalidate timestamp file
ignore user's cached credential if it's used recently.
- `-a, --append`: append to the given FILEs, do not overwrite
<br>
## tar, untar
- 解壓縮 xxx.tar.gz 到其他地方
```
$ tar -xzf /opt/parabricks/release-v3.1.1.tar.gz -C /opt/parabricks
```
- [檢視壓縮檔的內容](http://www.vixual.net/blog/archives/127)
```
$ tar -tvf 壓縮檔名稱.tar
$ tar -tzvf 壓縮檔名稱.tgz
```
- `-t, --list`
list the contents of an archive
- 遞迴壓縮
- [Recursive tar compression?](https://askubuntu.com/questions/834717/)
- ### --strip-components=1 作用為何?
- ### gpt-5.2 (2025/12/22)
**解壓縮時把每個檔案路徑最前面那一層目錄「剝掉」**,再寫入目的地。
---
### 用你的例子直覺理解
很多 `.tgz` 裡面會長這樣(都有一個頂層資料夾):
* `slurm-1.0.0/README.md`
* `slurm-1.0.0/src/...`
* `slurm-1.0.0/etc/...`
如果你解到 `-C "slurm-${SLURM_VERSION}"`:
- #### 沒有 `--strip-components=1`
會變成「再套一層」:
* `slurm-1.0.0/slurm-1.0.0/README.md`
* `slurm-1.0.0/slurm-1.0.0/src/...`
- #### 有 `--strip-components=1`
會把最前面那個 `slurm-1.0.0/` 拿掉,直接平鋪到目的地:
* `slurm-1.0.0/README.md`
* `slurm-1.0.0/src/...`
---
### 一句話總結
`--strip-components=N` = 解壓時 **把路徑開頭的 N 層目錄丟掉**,常用來避免「解出一個頂層資料夾 → 目的地裡又多包一層」的問題。
- ### [GNU / Linux 各種壓縮與解壓縮指令| 凍仁的筆記](http://note.drx.tw/2008/04/command.html)
<br>
## tr
> translate or delete characters
### 範例
1. 字元轉換(轉換小寫為大寫)
將輸入中的所有小寫字母轉換為大寫字母:
```
echo "hello world" | tr 'a-z' 'A-Z'
```
輸出:
```
HELLO WORLD
```
2. 字元轉換(大寫轉換為小寫)
將輸入中的所有大寫字母轉換為小寫字母:
```
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
```
輸出:
```
hello world
```
3. 刪除指定字元
刪除輸入中的所有數字:
```
echo "a1b2c3d4" | tr -d '0-9'
```
輸出:
```
abcd
```
4. 壓縮重複字元
將重複的空格壓縮為單一空格:
```
echo "This is a test" | tr -s ' '
```
輸出:
```
This is a test
```
5. 字元替換
將輸入中的冒號(:)替換為空格:
```
echo "one:two:three" | tr ':' ' '
```
輸出:
```
one two three
```
6. 字元類
將輸入中的所有數字替換為#號碼:
```
echo "My phone number is 123-456-7890" | tr '0-9' '#'
```
輸出:
```
My phone number is ###-###-####
```
<br>
## w
> Show who is logged on and what they are doing.
```bash
$ w
11:16:56 up 41 min, 4 users, load average: 3.39, 0.75, 0.31
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tj_tsai pts/8 10.78.153.133 10:39 4:32 0.21s 0.21s -bash
tj_tsai pts/9 10.78.153.10 10:47 3:04 0.18s 0.18s -bash
tj_tsai pts/10 10.78.153.133 11:16 4.00s 0.09s 0.09s -bash
tj_tsai pts/11 10.78.153.144 11:13 0.00s 0.12s 0.01s w
```
<br>
## wc
- [Fast Linux file count for a large number of files](https://stackoverflow.com/questions/1427032/)
> 快速統計檔案數量
>
`ls -f | wc -l`
`ls -fR | wc -l`
- `-f`
do not sort, enable -aU, disable -ls --color
- `-a, --all`
do not ignore entries starting with .
- `-U`
do not sort; list entries in directory order
- `-R, --recursive`
list subdirectories recursively
<br>
## wget
- `-O, --output-document=FILE`
write documents to FILE (overwrite if it exists)
如果沒有此參數,會用流水號命名
- `xxx.sh`
- `xxx.sh.1`
- `xxx.sh.2`
- ### Q & A
- [wget: what does this trailing dash represent/do?](https://stackoverflow.com/questions/34005959/wget-what-does-this-trailing-dash-represent-do)
```wget http://ipinfo.io/ip -qO -```
- [How can I show the wget progress bar only? ](https://stackoverflow.com/questions/4686464)
- [How do you redirect wget response to standard out?](https://superuser.com/questions/321240)
```bash
$ wget -O - -n https://deb.nodesource.com/setup_16.x
```
<br>
## xargs
> 對輸入資料的每一行,進行處理
- [Execute a command once per line of piped input?](https://unix.stackexchange.com/questions/7558/execute-a-command-once-per-line-of-piped-input)
```
... | xargs command
```
### 對 input 進行處理
- ### batch
```bash
$ echo -e "a1.txt\nb2.txt\nc3.txt" | xargs echo "rm"
rm a1.txt b2.txt c3.txt
```
- ### for-each
```bash
$ echo -e "a1.txt\nb2.txt\nc3.txt" | xargs -n 1 echo "rm"
rm a1.txt
rm b2.txt
rm c3.txt
```
- ### for-two
```bash
$ echo -e "a1.txt\nb2.txt\nc3.txt" | xargs -n 2 echo "rm"
rm a1.txt b2.txt
rm c3.txt
```
<br>
## zip & unzip
- 壓縮:
```
$ zip -r FileName.zip DirName
```
- 解壓縮:
```
$ unzip FileName.zip
```
- ### [GNU / Linux 各種壓縮與解壓縮指令| 凍仁的筆記](http://note.drx.tw/2008/04/command.html)
- ### [Extract only a specific file from a zipped archive to a given directory](https://unix.stackexchange.com/questions/14120)
`unzip -p myarchive.zip path/to/zipped/file.txt > file.txt`
- ### [How to decompress a ZIP file with specified file/directory name character encoding?](https://superuser.com/questions/872596/)
```
$ unzip -O <encoding> <filename> -d <target_dir>
# or
$ unzip -I <encoding> <filename> -d <target_dir>
choosing between -O or -I according to instructions here:
```
```
$ unzip -h
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
...
-O CHARSET specify a character encoding for DOS, Windows and OS/2 archives
-I CHARSET specify a character encoding for UNIX and other archives
...
```
- 範例
`$ unzip -O big5 "10組(共20題).zip" -d tmp/`
- `tmp` 無一定要存在
<br>
<hr>
<br>
# Linux commands - package
## apt-key
:::warning
:warning: **[[Nvidia][forums] ](https://forums.developer.nvidia.com/t/the-repository-https-developer-download-nvidia-com-compute-cuda-repos-ubuntu1804-x86-64-release-is-not-signed/193764/11?u=tj_tsai)**
The apt-key command is deprecated and will be removed in Debian 12 and Ubuntu 22.10, see: [DebianRepository/UseThirdParty - Debian Wiki](https://wiki.debian.org/DebianRepository/UseThirdParty)
:::
- [APT 系統中的數位簽章機制](https://www.ubuntu-tw.org/modules/newbb/viewtopic.php?viewmode=compact&type=&topic_id=16902&forum=2)
> 由於使用金鑰加密、解密時,其流程中的密文體積以及演算過程的時間會隨著明文的體積成正比成長,因此在 APT 系統中**將整個軟體套件進行金鑰加密、解密則顯的太過不切實際**。所以在 APT 系統中軟體套件或原始碼本身都是以明文的方式存放於伺服器上並提供所有的 APT 的 Client 程式下載,套件維護者只會對軟體套件先進行 MD5 演算產生 128 位元的碼串,再針對該碼串進行金鑰加密。
>
> 接收端在進行套件清單更新時(apt-get update)會下載所有 deb 套件的 MD5 碼串以及其加密後的密文,此時 APT Client 則必須擁有該套件發佈者的公開金鑰才有辦法正確的將接收到的密文還原成 MD5 的碼串,還原出該碼串後即可跟下載回來的碼串進行比對以確定該 MD5 碼串是可靠的,之後再對所有下載回來要安裝的軟體套件進行一次 MD5 演算以產生一個新的碼串,並將新碼串拿來跟可靠的 MD5 碼串比對,藉以確認軟體套件本身是未被篡改的。
- ### [作法一](https://hackmd.io/bMasy0__T3-lqFnNFklgvw#%E6%9F%A5%E7%9C%8B-GPU-%E8%B3%87%E8%A8%8A)
```bash=
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
```
- ### [作法二](https://hackmd.io/52Xj-oMnRXKMjP3JOZ__Yg#%E5%AE%89%E8%A3%9D-docker)
```bash=
#讓系統信賴 Docker 安裝倉庫
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
```
- ### 查看 apt-key 清單
```bash=
$ sudo apt-key fingerprint
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2016-06-24 [SC]
AE09 FE4B BD22 3A84 B2CC FCE3 F60F 4B3D 7FA2 AF80
uid [ unknown] cudatools <cudatools@nvidia.com>
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
...
```
- 同 `sudo apt-key list`
- ### 移除某個 apt-key
```bash
$ sudo apt-key list
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2016-06-24 [SC]
AE09 FE4B BD22 3A84 B2CC FCE3 F60F 4B3D 7FA2 AF80 <---
uid [ unknown] cudatools <cudatools@nvidia.com>
/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-archive.gpg
------------------------------------------------------
pub rsa4096 2012-05-11 [SC]
790B C727 7767 219C 42C8 6F93 3B4F E6AC C0B2 1F32
uid [ unknown] Ubuntu Archive Automatic Signing Key (2012) <ftpmaster@ubuntu.com>
```
填入後 8 碼,不分大小寫
```
$ sudo apt-key del 7fa2af80 # 7FA2 AF80
```
觀察結果:
```bash
$ sudo apt-key list
/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-archive.gpg
------------------------------------------------------
pub rsa4096 2012-05-11 [SC]
790B C727 7767 219C 42C8 6F93 3B4F E6AC C0B2 1F32
uid [ unknown] Ubuntu Archive Automatic Signing Key (2012) <ftpmaster@ubuntu.com>
```
- [How can I remove gpg key that I added using apt-key add -?](https://askubuntu.com/questions/107177)
- ### 更新過期的 apt-key
- [Fix Ubuntu/Debian apt-get “KEYEXPIRED: The following signatures were invalid”](https://futurestud.io/tutorials/fix-ubuntu-debian-apt-get-keyexpired-the-following-signatures-were-invalid)
```
$ sudo apt-key list | grep -A 1 expired
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys <KEY>
```
```
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 51312F3F
Executing: /tmp/tmp.3aYqjV8GP0/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv-keys
51312F3F
gpg: requesting key 51312F3F from hkp server keyserver.ubuntu.com
gpg: key 51312F3F: "GitLab B.V. (package repository signing key) <packages@gitlab.com>" 2 new signatures
gpg: Total number processed: 1
gpg: new signatures: 2
```
<br>
<hr>
<br>
## apt-get
### 套件清單
```
$ apt list
```
執行過濾:
```bash
$ apt list | grep -i policykit
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
lxqt-policykit-l10n/focal,focal 0.14.1-1ubuntu2 all
lxqt-policykit/focal 0.14.1-1ubuntu2 amd64
policykit-1-doc/focal-updates,focal-updates,focal-security,focal-security 0.105-26ubuntu1.1 all
policykit-1-gnome/focal,now 0.105-7ubuntu2 amd64 [installed]
policykit-1/focal-updates,focal-security,now 0.105-26ubuntu1.1 amd64 [installed]
policykit-1/focal-updates,focal-security 0.105-26ubuntu1.1 i386
policykit-desktop-privileges/focal,focal,now 0.21 all [installed]
```

升版後,再執行過濾:

- policykit 已經從 `0.105-26ubuntu1.1` 升到 `0.105-26ubuntu1.2`
<br>
### 查詢版本
```bash
$ apt show <packagename>
```
```
$ apt show policykit-1
```
<br>
### 安裝
- .deb file
```
$ sudo apt install -y xxx.deb
```
or
```
$ sudo dpkg -i package_file.deb
```
- 參考資料
- [Adding, Removing and Updating Applications
](https://help.ubuntu.com/kubuntu/desktopguide/C/manual-install.html)
<br>
### 升級, 升版, 更新, upgrade, update
- ### 更新套件索引
```
$ apt update # Fetch package index
```
- [Ubuntu upgrade or update a single package using apt-get](https://www.cyberciti.biz/faq/ubuntu-upgrade-update-single-package-using-apt-get/)
- ### 查看可升級套件
```bash
$ sudo apt update # Fetch package index
$ apt list --upgradable | grep policy
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
policykit-1/focal-updates,focal-security 0.105-26ubuntu1.2 amd64 [upgradable from: 0.105-26ubuntu1.1]
# 升級中...
$ sudo apt upgrade policykit-1
# or:
$ sudo apt install --only-upgrade policykit-1
# 升級後
$ apt list --upgradable
Listing... Done
```
- ### 對單一套件升級
```
$ sudo apt-get install --only-upgrade <packagename>
```
- [How to upgrade a single package using apt-get?](https://askubuntu.com/questions/44122)
- [Ubuntu upgrade or update a single package using apt-get](https://www.cyberciti.biz/faq/ubuntu-upgrade-update-single-package-using-apt-get/)
- ### 對 `policykit-1` 套件升級
> Linux原生套件polkit有本機提權漏洞,CVSS分數為7.8分
> 國外網站發布全系列Linux原生套件polkit有本機提權漏洞,CVSS分數為7.8分
> 使用到Linux平台全部都需要進行評估儘早進行弱點修補
> [USN-5252-1: PolicyKit vulnerability](https://ubuntu.com/security/notices/USN-5252-1)
>
```
$ sudo apt upgrade policykit-1
# or:
$ sudo apt install --only-upgrade policykit-1
```
- 升級後再升級,訊息顯示:已經是最新版本

- ### [APT upgrade 和 dist-upgrade 的差別](https://blog.longwin.com.tw/2008/03/debian_ubuntu_apt_dist_upgrade_difference_2008/)
- **upgrade**
系統將現有的 Package 升級, 如果有相依性的問題, 而此相依性 需要安裝其它新的 Package 或 影響到其它 Package 的相依性時, 此 Package 就不會被升級, 會保留下來.
- **dist-upgrade**
可以聰明的解決相依性的問題, 如果有相依性問題, 需要 安裝/移除 新的 Package, 就會試著去 安裝/移除 它. (所以通常這個會被認為是有點風險的升級)
<br>
### 移除, 反安裝
- ### 移除不必要的?(尚未驗證, 2022/02/07)
```
$ sudo apt autoremove
```
- ### [APT 的 clean 與 autoclean 差異](https://blog.longwin.com.tw/2012/05/apt-clean-autoclean-diff-2012/)
<br>
### 儲存庫
- ### [What can I do if a repository/PPA does not have a Release file?](https://askubuntu.com/questions/866901)
- 移除(尚未驗證, 2022/02/07)
```
sudo apt-get install ppa-purge
sudo ppa-purge ppa:mc3man/trusty-media
```
or
```
sudo add-apt-repository --remove ppa:mc3man/trusty-media
```
- 新增(尚未驗證, 2022/02/07)
```
sudo add-apt-repository ppa:mc3man/xerus-media
```
<br>
### 修復
- [apt-get –fix-missing做什麽以及什麽時候有用?](https://ubuntuqa.com/zh-tw/article/6398.html)
- [解决方法:Linux报错Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?解决方法](https://blog.csdn.net/weixin_45317486/article/details/111148560)
- Error报的信息显示是这个包向source里的资源下载失败了。从而得出是网络的问题。
<br>
## gpg key
- [[介紹] Instructions to connect to a third-party repository](https://wiki.debian.org/DebianRepository/UseThirdParty) (待消化)
<br>
## 建立全域路徑
- 方法1:
```
$ export PATH="<PATH-TO-CONDA>:$PATH"
```
- 方法2:如果是單一檔案,最適合底下這種
```
$ mv xxx /usr/local/bin/
```
<br>
<hr>
<br>
# Linux commands - storage (disk)
## stat
- [How to display modification time of a file?](https://askubuntu.com/questions/803149/)
```
$ stat auto-test-tmp.html
File: auto-test-tmp.html
Size: 1187303 Blocks: 2320 IO Block: 4096 regular file
Device: 802h/2050d Inode: 26483015 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ tj) Gid: ( 1000/ tj)
Access: 2022-12-07 15:15:23.882132925 +0800
Modify: 2022-10-05 10:21:24.265820963 +0800
Change: 2022-10-05 10:21:35.757538402 +0800
Birth: -
```
<br>
## df (disk file system, 磁碟的檔案系統)
> report file system disk space usage
```
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 89G 0 89G 0% /dev
tmpfs 18G 1.4M 18G 1% /run
/dev/vda1 97G 35G 63G 36% /
tmpfs 89G 0 89G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 89G 0 89G 0% /sys/fs/cgroup
/dev/vda15 105M 3.6M 101M 4% /boot/efi
/dev/vdb 2.0T 62G 1.9T 4% /mnt
tmpfs 18G 0 18G 0% /run/user/1000
```
- ```-h, --human-readable```: print sizes in powers of 1024 (e.g., 1023M)
- ```-H, --si```: print sizes in powers of 1000 (e.g., 1.1G)
- [[Linux] 如何隱藏 df 指令顯示的 /dev/loop* 裝置](https://clay-atlas.com/blog/2020/04/13/linux-cn-note-hide-df-h-dev-loop-device/)
<br>
## du (disk usage, 磁碟的使用 )
- ### [總覽] 找出最大的檔案或目錄,由大到小排列
```
sudo du -ahx / | sort -rh | head -n 20
```
- 參數說明:
- `-a`:列出所有文件和目錄的使用量。
- 沒有 `-a` 表示只有目錄
- 有 `-a` 表示有目錄和檔案
例如:
```
148M .
145M ./output <-- 目錄
2.6M ./benchmarks-20241125.tgz <-- 檔案
148K ./script <-- 目錄
```
- `-h`:顯示人類可讀格式(如 KB、MB)。
- `-x`:限制搜索範圍,不跨越其他檔案系統(避免進入掛載的磁碟)。
- 有可能 `~/.models` 會掛載其他檔案系統
- `sort -rh`:按數值從大到小排序。
- `head -n 20`:只顯示前 20 個項目。
- ### [總覽] 統計指定目錄下的資料夾&檔案大小 (#disk storage du df directory folder file size tree --max-depth -L)
```bash
$ sudo du --max-depth=3 -h | egrep "[GT]\s+" | sort -n
```
- 其他方式
- 當前目錄統整
```
$ du -sh *
```
等效於 `$ du -h --max-depth=1`
- [Print size of directory content with tree command in tree 1.5?](https://unix.stackexchange.com/questions/45828/)
```bash
$ tree --du -h . | grep G]
```

- [統計指定目錄下的資料夾&檔案大小](https://success.docker.com/article/error-message-no-space-left-on-device-in-default-machine)
```
$ find /home/tj/ -maxdepth 1 -mindepth 1 | xargs sudo du -sch
```
- ```-maxdepth 1``` 只顯示子資料夾一層
- ```-mindepth 1``` 不顯示自身資料夾,i.e. 會忽略```.```
- ```-s, --summarize```
display only a total for each argument
不要顯示子資料夾資訊,只顯示根目錄資訊
- ```-c, --total```
produce a grand total
最後顯示一行全部加總的大小
- ```-h, --human-readable```
print sizes in human readable format (e.g., 1K 234M 2G)
以人類可讀的方式顯示
- 執行結果
```
8.0K /home/tj/Videos
72K /home/tj/.anydesk
4.0K /home/tj/.xsession-errors
12K /home/tj/.presage
24K /home/tj/.gconf
4.0K /home/tj/.xsession-errors.old
697M /home/tj/.SynologyDrive
4.0K /home/tj/.wget-hsts
811M /home/tj/Downloads
...
```
- [Linux tip: du --max-depth=1](https://www.peterbe.com/plog/du-max-depth)
```
$ du -h --max-depth=1 /
```
<br>
## lsblk
```
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2.6T 0 disk /mnt/ssdraid
sda 8:0 0 7.3T 0 disk
├─sda4 8:4 0 238.4G 0 part
├─sda2 8:2 0 477M 0 part /boot
├─sda5 8:5 0 4.3T 0 part /var/lib/docker
├─sda3 8:3 0 2.7T 0 part /
└─sda1 8:1 0 1M 0 part
```
只顯示硬碟空間資訊
```
# -d, --nodeps don't print slaves or holders
$ lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2.6T 0 disk /mnt/ssdraid
sda 8:0 0 7.3T 0 disk
```
顯示掛載區塊位置
```
# -d, --nodeps don't print slaves or holders
# -o, --output <list> output columns
$ lsblk -d -o name,rota
NAME ROTA
sdb 1
sda 1
```
上述結果同:
```
$ cat /sys/block/sda/queue/rotational
1
$ cat /sys/block/sdb/queue/rotational
1
```
<br>
## lshw
```
$ sudo lshw -short
$ sudo lshw -businfo
```
### 過濾出儲存體
```
$ sudo lshw -class volume
*-volume:2
description: EXT4 volume
vendor: Linux
physical id: 3
bus info: scsi@0:2.0.0,3
logical name: /dev/sda3
logical name: /
logical name: /var/lib/kubelet/pods/beaab264-a5f3-4ba6-9734-ba8aeb8a6370/volume-subpaths/fluentconfig/fluentd/2
logical name: /var/lib/kubelet/pods/cb61e0f9-c538-499f-b015-bd638f923f7f/volume-subpaths/pv-results-service-volume/clara/0
logical name: /var/lib/kubelet/pods/cb61e0f9-c538-499f-b015-bd638f923f7f/volume-subpaths/results-service-config/clara/1
logical name: /var/lib/kubelet/pods/74c93330-a7f0-4d9b-a922-df40d2ef92e5/volume-subpaths/pv-dicom-adapter-volume/dicom-adapter/0
logical name: /var/lib/kubelet/pods/74c93330-a7f0-4d9b-a922-df40d2ef92e5/volume-subpaths/dicom-server-config/dicom-adapter/1
logical name: /var/lib/kubelet/pods/74c93330-a7f0-4d9b-a922-df40d2ef92e5/volume-subpaths/dicom-server-config/dicom-adapter/2
logical name: /var/lib/kubelet/pods/af5f7ce6-7041-4d80-928b-bd03c78dacfa/volume-subpaths/config/grafana/0
logical name: /var/lib/kubelet/pods/af5f7ce6-7041-4d80-928b-bd03c78dacfa/volume-subpaths/config/grafana/3
version: 1.0
serial: 6035ff9d-ae84-41d8-a12e-8cb327121c8a
size: 2793GiB
capabilities: journaled extended_attributes large_files huge_files dir_nlink recover extents ext4 ext2 initialized
configuration: created=2019-01-16 16:33:54 filesystem=ext4 lastmountpoint=/ modified=2020-10-17 09:52:13 mount.fstype=ext4 mount.options=rw,relatime,errors=remount-ro,stripe=64,data=ordered mounted=2020-10-17 09:53:03 state=mounted
...
*-disk:1
description: EXT4 volume
product: AsustekPIKE3108
vendor: Linux
physical id: 2.1.0
bus info: scsi@0:2.1.0
logical name: /dev/sdb
logical name: /mnt/ssdraid
version: 1.0
serial: 2e02a84a-fd51-4396-a856-4cc381ebfae0
size: 2679GiB
capabilities: journaled extended_attributes large_files huge_files dir_nlink recover extents ext4 ext2 initialized
configuration: ansiversion=5 created=2019-07-16 14:10:15 filesystem=ext4 lastmountpoint=/mnt/ssdraid logicalsectorsize=512 modified=2020-10-19 15:24:03 mount.fstype=ext4 mount.options=rw,relatime,stripe=256,data=ordered mounted=2020-10-19 15:24:03 sectorsize=4096 state=mounted
```
- 邏輯磁區資訊來源
```
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 2.6T 0 disk /mnt/ssdraid
sda 8:0 0 7.3T 0 disk
├─sda4 8:4 0 238.4G 0 part
├─sda2 8:2 0 477M 0 part /boot
├─sda5 8:5 0 4.3T 0 part /var/lib/docker
├─sda3 8:3 0 2.7T 0 part /
└─sda1 8:1 0 1M 0 part
```
<br>
### 過濾出顯卡 (顯示卡, display, GPU)
```
$ sudo lshw -C display
```
- 參考資料
- [Installing Multiple CUDA & cuDNN Versions in Ubuntu](https://towardsdatascience.com/fcb6aa5194e2)
<br>
## cat 系統資訊
- [Linux 判斷系統硬碟是 SSD 還是傳統硬碟](https://blog.longwin.com.tw/2019/06/linux-howto-check-ssd-or-hdd-2019/)
```
$ cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 02 Id: 00 Lun: 00
Vendor: AVAGO Model: AsustekPIKE3108 Rev: 4.65
Type: Direct-Access ANSI SCSI revision: 05
Host: scsi0 Channel: 02 Id: 01 Lun: 00
Vendor: AVAGO Model: AsustekPIKE3108 Rev: 4.65
Type: Direct-Access ANSI SCSI revision: 05
```
- [[Linux] 判斷硬碟是否為 SSD 的方法](https://clay-atlas.com/blog/2020/04/17/linux-cn-note-how-to-check-disk-is-ssd-or-hdd/)
- [How to know if a disk is an SSD or an HDD](https://unix.stackexchange.com/questions/65595/how-to-know-if-a-disk-is-an-ssd-or-an-hdd)
<br>
## smartctl
- ### [Is there a command to view hard drive specs](https://askubuntu.com/questions/466366/is-there-a-command-to-view-hard-drive-specs)
```
$ sudo smartctl -i /dev/sda
smartctl 6.5 2016-01-24 r4214 [x86_64-linux-4.12.14-041214-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF INFORMATION SECTION ===
Vendor: AVAGO
Product: AsustekPIKE3108
Revision: 4.65
User Capacity: 8,001,020,755,968 bytes [8.00 TB]
Logical block size: 512 bytes
Physical block size: 4096 bytes
Logical Unit id: 0x600112f850000cd023d1a69e0a788515
Serial number: 001585780a9ea6d123d00c0050f81201
Device type: disk
Local Time is: Mon Nov 9 12:14:30 2020 CST
SMART support is: Unavailable - device lacks SMART capability.
'''
'''
$ sudo smartctl -i /dev/sdb
smartctl 6.5 2016-01-24 r4214 [x86_64-linux-4.12.14-041214-generic] (local build)
Copyright (C) 2002-16, Bruce Allen, Christian Franke, www.smartmontools.org
=== START OF INFORMATION SECTION ===
Vendor: AVAGO
Product: AsustekPIKE3108
Revision: 4.65
User Capacity: 2,877,359,652,864 bytes [2.87 TB]
Logical block size: 512 bytes
Physical block size: 4096 bytes
Logical Unit id: 0x600112f850000cd024c00afa077d5e36
Serial number: 00365e7d07fa0ac024d00c0050f81201
Device type: disk
Local Time is: Mon Nov 9 12:14:33 2020 CST
SMART support is: Unavailable - device lacks SMART capability.
```
<br>
## 延伸硬碟綜合問題
- [Linux 判斷系統硬碟是 SSD 還是傳統硬碟](https://blog.longwin.com.tw/2019/06/linux-howto-check-ssd-or-hdd-2019/)
- [How to know if a disk is an SSD or an HDD](https://unix.stackexchange.com/questions/65595/how-to-know-if-a-disk-is-an-ssd-or-an-hdd)
## RAID
- ### :+1: :100: [RAID 0 和 RAID 1 和 RAID 5 的差別](https://dotblogs.com.tw/jiajhou/2011/12/22/63091)
- RAID 0 的結果為 在系統內看到一顆硬碟容量為 20+20=40G
- RAID 1 的結果為 在系統內看到一顆硬碟容量為 20+20=20G (Mirror鏡射)
- RAID 5 的組成一定是3顆以上的硬碟,其容量的計算是(n-1)顆
<br>
## tester, performance, benchmark
> - bw: bandwidth
> - IOPS (Input/Output Per Second)即每秒的輸入輸出量(或讀寫次數)
> - [怎麼評判硬碟的好壞,IOPS和吞吐量](https://kknews.cc/news/y3gxz8j.html)
>
- [[fio] $ fio (Flexible I/O Tester)](/EiQSrD2ZRXuyQp-Hs4EYrw)
<br>
<hr>
<br>
# Linux commands - system
## Upgrade 升級
- ### [如何將Ubuntu從18.04升級到20.04版本](https://kknews.cc/code/g42eqp8.html)
```bash=
# Step1: 更新你的Ubuntu伺服器 (若 fail,多 try 幾次就成功)
sudo apt update
sudo apt dist-upgrade
# Step2: Ubuntu提供了一個名為Update Manager的重要工具,可以將系統升級到新版本。
# 正常情況下,它應該已經安裝了
sudo apt install update-manager-core
# Step3: 升級Ubuntu伺服器到一個新的LTS版本
do-release-upgrade
```
- ### Prompt is set to never so upgrading is not possible.
```
$ sudo do-release-upgrade
Checking for a new Ubuntu release
In /etc/update-manager/release-upgrades Prompt
is set to never so upgrading is not possible.
```


**Notify me of a new Ubuntu version:** 不要設定為 `Nerver`
<br>
## Basic
### 登出 logout
```bash=
$ sudo logout
```
### 關機 shutdown
- [How to use shutdown command under Ubuntu Linux](https://www.cyberciti.biz/faq/shutdown-ubuntu-linux-computer/)
```bash=
sudo shutdown -h now
# or
sudo shutdown -h 0
```
## OS Versoin 作業系統版本
```bash
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
# -r, --release
# show release number of this distribution
$ lsb_release -r
Release: 16.04
```
```
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
```
- 參考資料
- [lsb_release: command not found in latest Ubuntu Docker container](https://stackoverflow.com/questions/58395566)
```
# bash: lsb_release: command not found
$ apt-get install -y lsb-release
```
- 完整指令
```
$ apt-get update && apt-get install -y lsb-release && apt-get clean all
```
- 或是透過
```
$ cat /etc/os-release
```
## 螢幕大小/解析度
- [我的螢幕解析度是多少?](https://tw.piliapp.com/what-is-my/screen-resolution/)
- [顯示器尺寸](https://tw.piliapp.com/actual-size/what-is-my-monitor-size/)
- Toshiba (13.3")

## Kernel 核心
```bash
# uname: print system information
# -a, --all
# print all information,
$ uname -a
Linux tj-M52AD-M12AD-A-F-K31AD 4.15.0-122-generic
#124~16.04.1-Ubuntu SMP Thu Oct 15 16:08:36 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# -r, --kernel-release
# print the kernel release
$ uname -r
4.15.0-122-generic
```
```bash=
$ hostnamectl
Static hostname: tj-M52AD-M12AD-A-F-K31AD
Icon name: computer-desktop
Chassis: desktop
Machine ID: bd471bfd28d144a8b3addf2acd2a88ad
Boot ID: 8e9500627178478f96029785fbac34d4
Operating System: Ubuntu 16.04.7 LTS
Kernel: Linux 4.15.0-122-generic <---
Architecture: x86-64
$ cat /proc/version
Linux version 4.15.0-122-generic (buildd@lcy01-amd64-010) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12)) #124~16.04.1-Ubuntu SMP Thu Oct 15 16:08:36 UTC 2020
```
- [How To Check Kernel Version In Linux In Command Line](https://phoenixnap.com/kb/check-linux-kernel-version)
## CPU
- ### [【linux命令】lscpu、etc/cpuinfo詳解](https://www.itread01.com/content/1542338669.html) (CPU 資訊的解讀) :+1: :100:
- socket 就是主機板上插 cpu 的槽的數目,也就是可以插入的物理CPU的個數。
- core 就是我們平時說的“核“,每個物理CPU可以雙核,四核等等。
- thread 就是每個core的硬體執行緒數,即超執行緒
- 計算方式
```bash
# ① 檢視物理CPU的個數
$ cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
2
# ② 檢視邏輯CPU的個數
$ cat /proc/cpuinfo | grep "processor" | wc -l
24
# ③ 檢視CPU是幾核
$ cat /proc/cpuinfo | grep "cores" | uniq
6
```
- 實際例子
- TWCC VM (203.145.218.3)
```
$ cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
2
$ cat /proc/cpuinfo |grep "processor"|wc -l
8
$ cat /proc/cpuinfo |grep "cores"|uniq
cpu cores : 4
```
- 邏輯 CPU
- 2(插槽) x 4(核/插槽) x 1(超執行緒/核) = 8
- ESC4000 (10.78.26.241)
```
$ cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
2
$ cat /proc/cpuinfo |grep "processor"|wc -l
88
$ cat /proc/cpuinfo |grep "cores"|uniq
cpu cores : 22
```
- 邏輯 CPU
- 2(插槽) x 22(核/插槽) x 2(超執行緒/核) = 88
- ### [How to find the number of CPU cores including virtual?](https://askubuntu.com/questions/724228/how-to-find-the-number-of-cpu-cores-including-virtual)
- 以 TWCC VM 為例子
- solution1
```bash
cat /proc/cpuinfo
cat /proc/cpuinfo | grep processor -c
```
執行結果
```
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 85
model name : Intel Xeon Processor (Skylake, IBRS)
stepping : 4
microcode : 0x1
cpu MHz : 2499.998
cache size : 16384 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat pku ospke avx512_vnni md_clear
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs taa itlb_multihit
bogomips : 4999.99
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:
...
8
```
- solution2
```bash
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel Xeon Processor (Skylake, IBRS)
Stepping: 4
CPU MHz: 2499.998
BogoMIPS: 4999.99
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
L3 cache: 16384K
NUMA node0 CPU(s): 0-7
Flags: fpu vme de ...
```
- solution3
```bash
$ nproc
8
```
- ESC4000 (10.78.26.241)
- solution2
```bash
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 88
On-line CPU(s) list: 0-87
Thread(s) per core: 2
Core(s) per socket: 22
Socket(s): 2
```
- socket=2 兩個物理 CPU 插槽
- core=22 每顆 CPU 有 22 核
- thread 每核有支援 2 條超執行緒
- ### [Ubuntu查看CPU占用和使用情况](https://blog.csdn.net/Box_clf/article/details/79166803)
```
sudo apt install htop
htop
```
[](https://i.imgur.com/VPMNg5N.png)
- [[doc] htop - interactive process viewer](https://manpages.ubuntu.com/manpages/bionic/en/man1/htop.1.html)
```
$ htop --sort-key help
$ htop --sort-key IO_READ_RATE
$ htop --sort-key IO_WRITE_RATE
$ htop --sort-key IO_RATE
```
- [htop, show cpu side by side](https://unix.stackexchange.com/questions/181972/)
- ### [How To Check CPU Usage from Linux Command Line](https://phoenixnap.com/kb/check-cpu-usage-load-linux)
- `top -i`
- `-i` (Idle-process toggle)
Starts top with the last remembered `i' state reversed. When this toggle is Off, tasks that have not used any CPU since the last update will not be displayed. For additional information regarding this toggle see topic 4c. TASK AREA Commands, SIZE.
- `M` – sort task list by memory usage
- `P` – sort task list by processor usage
- `N` – sort task list by process ID
- `T` – sort task list by run time
- mpstat Command to Display CPU Activity
sar Command to Show CPU Utilization
iostat Command for Average Usage
<br>
## memory
- 系統有多少記憶體
```
$ cat /proc/meminfo
MemTotal: 16296176 kB
MemFree: 1695928 kB
MemAvailable: 10154740 kB
```
```
$ lsmem
RANGE SIZE STATE REMOVABLE BLOCK
0x0000000000000000-0x0000002d7fffffff 182G online no 0-90
Memory block size: 2G
Total online memory: 182G
Total offline memory: 0B
```
- [5 Ways to Check Available Memory in Ubuntu 20.04](https://vitux.com/5-ways-to-check-available-memory-in-ubuntu/)
```free -m```
<br>
## GPU
- 使用 nvidia-smi 列出 GPU 資訊
- TWCC VM(203.145.218.3)
```
$ nvidia-smi
Mon Dec 7 11:58:57 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.39 Driver Version: 418.39 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla V100-SXM2... On | 00000000:00:06.0 Off | 0 |
| N/A 33C P0 52W / 300W | 17354MiB / 32480MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 1 Tesla V100-SXM2... On | 00000000:00:07.0 Off | 0 |
| N/A 34C P0 55W / 300W | 31110MiB / 32480MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
```
- ESC4000 (10.78.26.241)
```
$ nvidia-smi
Mon Dec 7 12:03:47 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.95.01 Driver Version: 440.95.01 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:02:00.0 Off | N/A |
| 27% 23C P8 8W / 250W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 1 GeForce GTX 108... Off | 00000000:03:00.0 Off | N/A |
| 20% 25C P8 8W / 250W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 2 GeForce GTX 108... Off | 00000000:82:00.0 Off | N/A |
| 20% 30C P8 8W / 250W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
| 3 GeForce GTX 108... Off | 00000000:83:00.0 Off | N/A |
| 20% 27C P8 8W / 250W | 0MiB / 11178MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
```
- [使用 lspci 列出 GPU 資訊](https://youtu.be/lw0c1Ah-c-E?t=341)
```
apt install pciutils
lspci | grep -i nvidia
```
- TWCC VM(203.145.218.3)
```
$ lspci | grep -i nvidia
00:06.0 3D controller: NVIDIA Corporation GV100GL [Tesla V100 SXM2 32GB] (rev a1)
00:07.0 3D controller: NVIDIA Corporation GV100GL [Tesla V100 SXM2 32GB] (rev a1)
```
- ESC4000 (10.78.26.241): **GeForce GTX 1080 Ti**
```bash
$ lspci | grep -i nvidia
02:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
02:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
03:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
03:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
82:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
82:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
83:00.0 VGA compatible controller: NVIDIA Corporation GP102 [GeForce GTX 1080 Ti] (rev a1)
83:00.1 Audio device: NVIDIA Corporation GP102 HDMI Audio Controller (rev a1)
```
- [NVIDIA Driver Downloads](https://www.nvidia.com/Download/index.aspx?lang=en-us) (下載 NVIDIA 驅動程式)

<br>
**查詢結果:**
- 更早的資訊

- 2021/08/19:

- SUPPORTED PRODUCTS
> **GeForce 10 Series:**
> GeForce GTX 1080 Ti, GeForce GTX 1080, GeForce GTX 1070 Ti, GeForce GTX 1070, GeForce GTX 1060, GeForce GTX 1050 Ti, GeForce GTX 1050, GeForce GT 1030, GeForce GT 1010
<br>
## Systemd, systemctl
- docker
`FROM jrei/systemd-ubuntu:18.04`
- [如何修復 Linux 中的「Systemctl Command Not Found」錯誤](https://0xzx.com/zh-tw/2021110114071845459.html)
- [Linux systemd 系統服務管理基礎教學與範例](https://blog.gtwang.org/linux/linux-basic-systemctl-systemd-service-unit-tutorial-examples/)
- [How to create systemd service unit in Linux](https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux)
<br>
## App
- ### 如何安裝 app
```bash
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
```
- 參考資料
- [minikube start](https://minikube.sigs.k8s.io/docs/start/)
<br>
## fonts
### 安裝方式
- [Ubuntu安裝新字型及移除多餘字型](http://ada-dreamer.blogspot.com/2014/04/ubuntu1404_22.html)
### 微軟正黑體
- [微軟正黑體.ttf](http://fontsfile.com/Microsoft-Fonts/42201.html)
- [下載點](http://d.xiazaiziti.com/fonts/weiruan/%E5%BE%AE%E8%BD%AF%E6%AD%A3%E9%BB%91%E4%BD%93.ttf)
- [在ubuntu上安裝微軟正黑體](http://addiyang.blogspot.com/2007/12/ubuntu.html)
```
$ mkdir -p /usr/share/fonts/truetype/ttf-microsoft/
$ cp 微軟正黑體.ttf /usr/share/fonts/truetype/ttf-microsoft/
$ fc-cache -f -v
```
執行結果
```
$ fc-cache -f -v
...
/usr/share/fonts/truetype/ttf-microsoft: caching, new cache contents: 1 fonts, 0 dirs
...
fc-cache: succeeded
```
### 其他字型
- [Microsoft YaHei](https://fontsdata.com/594/microsoft-yahei.htm)
- [Ubuntu環境下,一些字型套件](https://samwhelp.github.io/book-ubuntu-basic-skill/book/content/font/font-package.html)
- fonts-moe-standard-song - 「教育部標準宋體」
- fonts-moe-standard-kai - 「教育部標準楷體」
- fonts-cns11643-sung - 「全字庫正宋體」
- fonts-cns11643-kai - 「全字庫正楷體」
- fonts-arphic-ukai -「文鼎楷書體」
- fonts-arphic-uming -「文鼎明體」
- fonts-arphic-bkai00mp -「文鼎 PL 中楷」
- fonts-arphic-bsmi00lp -「文鼎 PL 細上海宋」
- fonts-arphic-gbsn00lp -「文鼎 PL 簡報宋」
- fonts-arphic-gkai00mp -「文鼎 PL 簡中楷」
- fonts-cwtex-ming -「cwTeX 明體」
- fonts-cwtex-kai -「cwTeX 楷體」
- fonts-cwtex-heib -「cwTeX 粗黑體」
- fonts-cwtex-yen -「cwTeX 圓體」
- fonts-cwtex-fs -「cwTeX 仿宋體」
- fonts-droid -「Droid Sans Fallback」
- fonts-wqy-microhei -「文泉驛微米黑」
- fonts-wqy-zenhei -「文泉驛正黑體」
- xfonts-wqy - 「文泉驛 X11 字體」
- ttf-mscorefonts-installer -「微軟英文字型」 這個我尚未確定有沒有授權的爭議,請斟酌是否使用!
- 安裝指令
> $ sudo apt-get install fonts-moe-standard-song fonts-moe-standard-kai fonts-cns11643-sung fonts-cns11643-kai fonts-arphic-ukai fonts-arphic-uming fonts-arphic-bkai00mp fonts-arphic-bsmi00lp fonts-arphic-gbsn00lp fonts-arphic-gkai00mp fonts-cwtex-ming fonts-cwtex-kai fonts-cwtex-heib fonts-cwtex-yen fonts-cwtex-fs fonts-cwtex-docs fonts-droid fonts-wqy-microhei fonts-wqy-zenhei xfonts-wqy fonts-hanazono
<br>
## troubleshooting
### kswapd0
[](https://i.imgur.com/JWfsCoq.png)
```
$ top
top - 11:59:26 up 1:39, 3 users, load average: 44.36, 54.25, 46.65
Tasks: 340 total, 6 running, 334 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.4 us, 43.0 sy, 0.0 ni, 0.0 id, 53.2 wa, 0.0 hi, 2.5 si, 0.0 st
MiB Mem : 3859.9 total, 100.0 free, 3501.6 used, 258.3 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 43.9 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
104 root 20 0 0 0 0 R 95.5 0.0 18:57.79 kswapd0
85319 root 20 0 473336 8300 0 D 24.4 0.2 0:12.51 snapd
134 root 0 -20 0 0 0 I 5.6 0.0 1:15.38 kworker/0:1H-kblockd
3955 root 20 0 1103080 272552 0 S 3.9 6.9 9:11.74 kube-apiserver
83652 root 20 0 2446960 56912 0 S 3.9 1.4 0:54.57 kubelet
```
- ### [kswapd程序cpu使用高導致宕機](https://www.itread01.com/content/1547767283.html)
> **二. Swap配置對效能的影響**
>
> 分配太多的Swap空間會浪費磁碟空間,而Swap空間太少,則系統會發生錯誤。如果系統的實體記憶體用光了,系統就會跑得很慢,但仍能執行;
### Authentication Required
> Polkit Policy
- [**`Authentication is required to create a color profile`**](https://devanswers.co/how-to-fix-authentication-is-required-to-create-a-color-profile-managed-device-on-ubuntu-20-04-20-10/)
`sudo nano /etc/polkit-1/localauthority.conf.d/02-allow-colord.conf`
- [**`Authentication is required to create a color managed device`**](https://devanswers.co/how-to-fix-authentication-is-required-to-create-a-color-profile-managed-device-on-ubuntu-20-04-20-10/)
`sudo nano /etc/polkit-1/localauthority.conf.d/02-allow-colord.conf`
### "blocked for more than 120 seconds", diskless
- [Server hung with "blocked for more than 120 seconds", diskless](https://serverfault.com/questions/456980)
`$ dmesg`
<br>
<hr>
<br>
# Linux packages
## Java
- [Ubuntu Linux 安裝 Oracle 或 OpenJDK 的 Java JRE 與 JDK 步驟教學](https://blog.gtwang.org/linux/how-to-install-java-with-apt-get-on-ubuntu-linux/)
- 若只是要執行一般的 Java 應用程式,就只要安裝 JRE 即可
```
$ sudo apt install default-jre
$ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
```
<br>
## Image
### Shutter
> #screenshot #截圖
- [How to Install Shutter Screenshot Tool in Ubuntu 20.04](https://itsfoss.com/install-shutter-ubuntu/)
```
$ sudo add-apt-repository -y ppa:linuxuprising/shutter
$ sudo apt install shutter
```
- `sudo apt --fix-broken install`
<br>
## 其他參考
### touch
- [The touch Command](http://www.linfo.org/touch.html)
### ASUS
- [2021年01月 資安公告](https://asus.sharepoint.com/sites/OCIS/_layouts/15/Doc.aspx?sourcedoc={2661b20d-76d4-46f4-8035-93b98c8e8e4b}&action=edit)
- [SSH Keypair + 白名單網段](https://hackmd.io/@aes/Hy2QTjY2v)
- [系統帳號密碼規定](https://hackmd.io/y37T0mySQ6yjFPX1AidnfA?view)