WSL入门与Linux基础❤

这是我参与11月更文挑战的第10天,活动详情查看: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. 等待安装完成

# 正在安装时显示的东西
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: https://aka.ms/wslusers
Enter new UNIX username: fzf
Enter password: 1234
复制代码

启动

  1. 再次点击启动

  2. 进入窗口后按一下回车

# 就能看到如下的欢迎界面啦
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

...

fzf@DESKTOP-4UTTTAO:~#
复制代码

!!! note "PS"
安装完最好重启一下电脑哦~

更好用的终端

  1. 回到 MicroSoft Store 安装 Windows Terminal

  2. Win键,找到 Windows Terminal

  3. 右键点击 -> 更多 -> 选择 固定到任务栏

  4. 在任务栏中启动它

Linux基础

基础命令和cmd差不多

文件系统

Linux的一切都是基于文件的

顶级目录叫做/

用户目录为~ 路径为/home/xxx

# 看一下当前目录
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
复制代码

基础命令

# 查看当前目录
pwd

# 切换目录
cd <path>

# 查看目录内容
ls <path> # 列出全部目录
  -lah # 详细信息|全部|文件大小

# 新建文件
touch <file_name>

# 新建目录
mkdir <folder_name>

# 移动 | 重命名
mv <source> <target>

# 复制
cp <source> <target>

# 删除文件
rm <file_name>

# 查看文件内容
cat <file_name>

# 删除
rm *				# 仅删除文件
# 慎用!!!
rm -rf .		# 删除当前文件夹下的所有东西

# 搜索文件内容
grep hello.txt -e hi

# 在家目录外的路径操作时要加sudo
sudo touch demo.txt

# 使用管理员权限
sudo <命令>
复制代码

操作符

# 重定向
> # 覆盖
>> # 追加

# 例子
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
复制代码

安装软件

# 修改管理员账号密码
sudo passwd

# 切换为管理员用户
su root

# 将软件下载源改为国内
bash <(curl -sSL https://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. 创建工作目录并进入

# 创建目录
mkdir ~/work-space
# 使用vscode打开
code ~/work-space
复制代码
  1. 编写c语言程序
#include <stdio.h>

int main() {
printf("Hello World");
return 0;
}
复制代码
  1. 运行
# 编译
gcc demo.c -o demo
# 运行
./demo
复制代码

课后

作业

  1. 试一试所有介绍过的命令
  2. 安装一些好玩的Linux软件,参考下面的推荐阅读

推荐阅读

40个超有趣的Linux命令行彩蛋和游戏

看完这篇Linux基本的操作就会了