楊育晟(Peter Yang)

嗨, 我叫育晟, 部落格文章主題包含了程式設計、財務金融及投資...等等,內容多是記錄一些學習的過程和心得,任何想法都歡迎留言一起討論。



Email: ycy.tai@gmail.com
LinkedIn: Peter Yang
Github: ycytai

Linux 常用指令紀錄

軟體公司的服務不管是運行在自購的 server 或是用 cloud service,多數會使用 Linux 作為 OS,Linux 有許多發行版,其中又以 Ubuntu 最為普及,目前工作或是教學影片見到的大多使用的也是 Ubuntu。

而即便 Ubuntu 有發行 GUI 版本,但大多 VM 所用的仍是 CLI 版本為主,透過指令而非介面點選來進行操作和管理,作為後端工程師會和 server 有大量接觸,熟悉 Linux 指令也是需要具備的重要技能之一。

pwd 顯示當前路徑

$ pwd
/home/peter_yang

cd 移動到別的路徑

$ cd .. # 移動到上一層
$ pwd
/home

ls 顯示當前路徑的檔案

輸入指令後,會顯示路徑中的檔案,就像是在用 Windows 點入資料夾後看到資料夾所包含的檔案。

$ ls
MI_INDEX.json  image.png  invoice.pdf  lg

加上 -F 來辨識子路徑

$ ls
MI_INDEX.json  image.png  invoice.pdf  lg/

加上 -l 顯示完整資訊

$ ls -l
total 740
-rw-r--r-- 1 peter_yang peter_yang  41556 Aug 30 14:33 MI_INDEX.json
-rw-r--r-- 1 peter_yang peter_yang 607146 Aug 30 14:32 image.png
-rw-r--r-- 1 peter_yang peter_yang  94237 Aug 30 14:34 invoice.pdf
drwxrwxr-x 2 peter_yang peter_yang   4096 Aug 30 14:34 lg

加上 -a 顯示隱藏檔案

$ ls -a
.	      .bashrc	.secret			   .viminfo	  invoice.pdf
..	      .cache	.ssh			   MI_INDEX.json  lg
.bash_lgout  .profile	.sudo_as_admin_successful  image.png

cat 瀏覽檔案

$ cat article.txt
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

less 瀏覽長度較長的檔案

  • f 往下一頁
  • b 往前一頁
  • 方向鍵 上 往前一行
  • 方向鍵 下 往後一行
  • q 結束
$ less MI_INDEX.json
[
    {
        "指數": "寶島股價指數",
        "收盤指數": "17899",
        "漲跌": "+",
        "漲跌點數": "202.00",
        "漲跌百分比": "1.14",
        "特殊處理註記": ""
    },
    {
        "指數": "發行量加權股價指數",
        "收盤指數": "15579",
        "漲跌": "+",
        "漲跌點數": "167.69",
        "漲跌百分比": "1.09",
        "特殊處理註記": ""
    },
    ...

cp 複製檔案

一定要使用選項 -i,碰到同樣檔名時會先詢問,避免悲劇,不然會直接複寫同樣檔名的檔案。

$ cp -i lg_1.txt lg/lg_1.txt
cp: overwrite 'lg/lg_1.txt'?

加上 -r 複製資料夾

$ cp -r -i lg/ lg2/

mv 移動檔案

$ mv image.png lg
$ cd lg
$ ls
image.png  lg_1.txt

更新檔名

$ mv image.png image_new.png
$ ls
image_new.png  lg_1.txt

echo 顯示文字

$ echo 'say hi'
say hi

將文字寫入檔案

$ echo 'say hi' > hi.txt
$ cat hi.txt
say hi
Tags:
# linux