WSL入門與Linux基礎❤

語言: CN / TW / HK

這是我參與11月更文挑戰的第9天,活動詳情檢視:2021最後一次更文挑戰

cmd基礎命令

linux基礎命令

安裝軟體

WSL入門

來試試 Windows Sub system for Linux 吧!

安裝

啟用WSL功能

  1. Win鍵 -> 輸入控制面板 按回車 -> 左下找到解除安裝程式並點選
  2. 左側選單中點選啟用或關閉 Windows 功能
  3. 向下滾動,勾選適用於 Linux 的 Windows 子系統
  4. 點選右下角的確定
  5. 等待啟用完畢,需要重啟電腦完成安裝,點選立即重新啟動

安裝Ubuntu

Linux的一個發行版

  1. Win鍵 -> 點選右側 Microsoft Store ✌ 搜尋 應用商店

  2. 搜尋 Ubuntu -> 進入詳情頁

  3. 點選獲取

  4. 需要註冊一個微軟賬號,假如你沒有的話

  5. 登入成功後,然後點選上方安裝

  6. 等待安裝完成後,點選啟動

  7. 等待安裝完成

```bash

正在安裝時顯示的東西

Installing, this may take a few minutes...

安裝完成後顯示的東西

Please create a default UNIX user account. The username does not need to match your Windows username. For more information visit: http://aka.ms/wslusers Enter new UNIX username: fzf Enter password: 1234 ```

啟動

  1. 再次點選啟動

  2. 進入視窗後按一下回車

```bash

就能看到如下的歡迎介面啦

Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

  • Documentation: http://help.ubuntu.com
  • Management: http://landscape.canonical.com
  • Support: http://ubuntu.com/advantage

...

fzf@DESKTOP-4UTTTAO:~# ```

!!! note "PS" 安裝完最好重啟一下電腦哦~

更好用的終端

  1. 回到 MicroSoft Store 安裝 Windows Terminal

  2. Win鍵,找到 Windows Terminal

  3. 右鍵點選 -> 更多 -> 選擇 固定到工作列
  4. 在工作列中啟動它

Linux基礎

基礎命令和cmd差不多

檔案系統

Linux的一切都是基於檔案的

頂級目錄叫做/

使用者目錄為~ 路徑為/home/xxx

```bash

看一下當前目錄

pwd

這是哪?

/mnt/c/Users/nmdfzf404

切換到根目錄

cd / ✌ cd ../..

看看根目錄裡有什麼

ls bin dev home lib lib64 media opt root sbin srv tmp var boot etc init lib32 libx32 mnt proc run snap sys usr

回家

cd ~ ✌ cd /home/fzf ✌ cd /home/fzf ```

基礎命令

```bash

檢視當前目錄

pwd

切換目錄

cd

檢視目錄內容

ls # 列出全部目錄 -lah # 詳細資訊|全部|檔案大小

新建檔案

touch

新建目錄

mkdir

移動 | 重新命名

mv

複製

cp

刪除檔案

rm

檢視檔案內容

cat

刪除

rm * # 僅刪除檔案

慎用!!!

rm -rf . # 刪除當前資料夾下的所有東西

搜尋檔案內容

grep hello.txt -e hi

在家目錄外的路徑操作時要加sudo

sudo touch demo.txt

使用管理員許可權

sudo <命令> ```

操作符

```bash

重定向

覆蓋

追加

例子

echo "Some Text" > hello.txt cat hello.txt echo "Some Text2" > hello.txt cat hello.txt echo "Some Text3" >> hello.txt cat hello.txt

管道

|

例子

ls -l / | grep bin # 在根目錄搜尋bin

中轉

tee # 將輸出儲存到檔案後繼續向下

例子

ll /usr/bin | tee software.log | grep python ```

安裝軟體

```bash

修改管理員賬號密碼

sudo passwd

切換為管理員使用者

su root

將軟體下載源改為國內

bash <(curl -sSL http://gitee.com/SuperManito/LinuxMirrors/raw/main/ChangeMirrors.sh)

獲取最新軟體列表

apt update

安裝一個好玩的軟體

apt install sl alias ls=sl # 惡搞

又一個好玩的軟體

apt install cowsay

圖形化檔案瀏覽器

apt install ranger

彩色程序管理

apt install htop

安裝c語言環境

apt install gcc

寫一個c程式

vim demo.c gcc demo.c -o demo ./demo

終止

Ctrl+C ```

連線vscode

  1. 安裝外掛: Remote - WSL

  2. 側邊欄: Remote Explorer

  3. 連線到wsl

  4. 建立工作目錄並進入

```bash

建立目錄

mkdir ~/work-space

使用vscode開啟

code ~/work-space ```

  1. 編寫c語言程式

```c

include

int main() { printf("Hello World"); return 0; } ```

  1. 執行

```bash

編譯

gcc demo.c -o demo

執行

./demo ```

課後

作業

  1. 試一試所有介紹過的命令
  2. 安裝一些好玩的Linux軟體,參考下面的推薦閱讀

推薦閱讀

40個超有趣的Linux命令列彩蛋和遊戲

看完這篇Linux基本的操作就會了