> For the complete documentation index, see [llms.txt](https://stb11816.gitbook.io/python_note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stb11816.gitbook.io/python_note/ubuntu/chang-yong-zhi-ling.md).

# 常用指令

## vim

### 複製貼上

參考連結：\
<http://unclecode.blogspot.tw/2012/05/vim.html>

### 暫存檔問題

```
#查看所有檔案，包含隱藏檔案
ls -al

#手動刪除
rm -r [filename]
```

參考連結：\
<http://linux.vbird.org/linux_basic/0310vi.php>

## 檔案與資料夾操作

#### 刪除

```
#砍掉整個資料夾問都不問
rm -rf [dirname or file name]

#會問
rm -i [dirname or file name]
```

參考連結：\
<https://ithelp.ithome.com.tw/articles/10159398>

#### 複製

```
#-f=覆蓋
cp -r -f [input] [output]
```

#### 狀態查詢

```
# 查看建立時間、大小等
stat [filename or dirname]

# 查看容量大小
du -sh [filename or dirname]
```

參考連結：\
<https://blog.longwin.com.tw/2017/01/linux-cli-stat-find-file-time-2017/>

#### 查詢檔案或資料夾

```
# 尋找家目錄底下名稱包含 test(不分大小寫) 的資料夾
find ~ -iname "*test*" -type d

# 尋找家目錄底下名稱包含 test(不分大小寫) 的資料夾或檔案
find ~ -iname "*test*"

# 尋找家目錄底下的 test 資料夾或檔案
find ~ -name "test"

# 列出所有檔案與資料夾(包含隱藏)
ls -a

# 找出隱藏檔案與資料夾
find /path/to/ -name ".*" -print
```

參考連結：\
<https://blog.camel2243.com/2016/09/21/linux-find-指令，搜尋檔案資料夾名稱與全文搜尋/>\
<https://www.ubuntudoc.com/show-hidden-files-folders-ubuntu/?lang=zh-tw>

### 查看使用記憶體

```
cat /proc/meminfo
```

```
top
```

### 壓縮檔操作

```
#製作壓縮檔
tar -zcvC [output dir] -f [output tar file name] [input dirname]

#製作壓縮檔2
tar -zcf [output_file_name] [input dirname] [input dirname2]

#解壓縮
tar -zxvf [file_name] -C [output_path]

#解壓縮 (gz)
gunzip FileName.gz
gzip -d FileName.gz

.zip
壓縮：zip FileName.zip DirName
解壓：unzip FileName.zip
```

參考連結:\
<https://dotblogs.com.tw/may05005/2015/12/24/160605>\
<http://idobest.pixnet.net/blog/post/22040782-[轉貼]-linux解壓-tar-命令>\
<http://note.drx.tw/2008/04/command.html>

### 使用金鑰進行ssh連線與傳輸檔案

```
cp [key file] ~
chmod 400 [key file]

#傳輸
scp -i [key file] [input file] ubuntu@[ip]:[output dir]

#連線
ssh -i [key file] ubuntu@[ip]

scp –r [file or dir] [name of ubuntu]:[dirname]
```

### 中止程式

```
# 關閉執行中的process
kill –TERM [process_id]
或
kill -9 [process_id]
```

參考連結：\
<https://blog.gtwang.org/linux/linux-kill-killall-xkill/>

### 查看使用中的port

```
netstat –ntlp |grep LISTEN
或
sudo netstat -tuap
```

查詢功能

```
#查詢包含sh名稱的pid
ps aux|grep sh
```

參考資料：\
<https://blog.csdn.net/LY_Dengle/article/details/53997450>

### 查看log

```
# 查看系統的log
sudo cat /var/log/syslog


# 動態監聽某個程式所產生的log檔
```

### 測試連線

```
#時間間隔和次數限制的ping

ping -c 10 -i 0.5 192.168.120.206
```

參考連結：\
[http://jashliao.pixnet.net/blog/post/163943597-每天一個linux指令--ping指令(用來測試與目標主](http://jashliao.pixnet.net/blog/post/163943597-每天一個linux指令--ping指令%28用來測試與目標主)

### 開關機紀錄

```
# 關機記錄
last -x shutdown

# 重新開機記錄
last -x reboot
```

### 設定使用者密碼

```
sudo passwd [使用者名稱]
```

參考連結：\
<https://stackoverflow.com/questions/35016795/get-root-password-for-google-cloud-engine-vm>

### 權限設定

三種身份：

* u: 自己(user)
* g: 和自己同一組的人(group)
* o: 其它人 (other)

每種身份皆有四種可能的權限:

* r : 讀取權 (read)
* w : 寫入權 (write)
* x : 執行權 (execute)

```
# 如果要一次設定資料夾下所有子檔案和子資料夾加上 -R
sudo chown [擁有者]:[群組] [檔案或資料夾]

# 開啟所有權限
sudo chmod -R 777 [檔案或料夾]

# 啟用sh
sudo chmod +x [sh檔案名稱]

# 啟用所有sh檔案
sudo chmod +x *.sh
```

參考連結：\
<https://blog.gtwang.org/linux/linux-chown-command-tutorial/>\
<http://s2.naes.tn.edu.tw/~kv/file.htm>

### Failed to lock the file

當vm當機或被強迫關機，再度開啟時會出現的錯誤

```
解決方法1：
進到 VMware 的 Guest OS 資料夾，將所有的 *.lck 資料夾或檔案刪除
```

參考資料：\
<http://www.vixual.net/blog/archives/842>

### share folder

無法正常顯示資料夾時可重新安裝vmware tools

參考資料：\
<https://snippetinfo.net/mobile/media/2004>\
<http://jimmu-jimmu.blogspot.com/2012/12/vmwareguesthost.html>

### 執行程式被kill

<https://blog.csdn.net/ktigerhero3/article/details/80004315>

### 關閉ssh連線時，保留欲執行的程式

```
sudo nohup [command] & >/dev/null 2>&1
```

<https://blog.csdn.net/stonesharp/article/details/28904529>

test:

<https://dotblogs.com.tw/newmonkey48/2012/06/28/73115>

<http://linuxdiary.blogspot.com/2007/10/blog-post_30.html>

### ctrl+c vs ctrl+z

c是kill，z是掛起

z掛起後可以先做其他事，可用bg查看被掛起來的程式，再用fg把掛起的程式叫回來

<https://blog.csdn.net/mylizh/article/details/38385739>

### 排程設定

`分鐘, 小時, 日期, 月份, 星期`

```
# vm 排程每五分鐘執行一次start.sh，*/5要連在一起，總共5個時間點

*/5 * * * * /home/start.sh

0 10 * * * ~/test/test.sh >/dev/null 2>&1
```

參考連結：\
<https://code.kpman.cc/2015/02/11/利用-crontab-來做-Linux-固定排程/>

### MTA錯誤

```
# 查看crontab log
sudo cat /var/sys/syslog

# 加上>/dev/null 2>&1
* * * * * ./temp.sh >/dev/null 2>&1
```

參考資料：\
<https://blog.csdn.net/win_turn/article/details/53000899>\
<https://blog.csdn.net/disappearedgod/article/details/43191693>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/ubuntu/chang-yong-zhi-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
