linux命令行

Linux命令行

why:docker是在Linux的概念上构建的,很多教程里面用的都是Linux命令行

Linux分身

Linux是一个开源的应用。不同公司为了运行服务器等不同目的,开发了不同的Linux版本

Distros: built for specific usage

  • Ubuntu
  • Debian
  • Alpine
  • Fedora
  • CentOS

运行Linux

1
2
3
4
5
6
% docker run ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
f4bb4e8dca02: Pull complete
Digest: sha256:77906da86b60585ce12215807090eb327e7386c8fafb5402369e421f44eff17e
Status: Downloaded newer image for ubuntu:latest

本地没有安装的话,会先下载下来再运行,但是没有进入运行就停止了,因为需要交互式运行Ubuntu

1
2
3
% docker ps -a  # 列出所有的容器,包括不在运行的
% docker run -it ubuntu # 交互式运行Ubuntu(interactively)
root@38e8564fa5b8:/#

当交互式运行Ubuntu时,我们进入了shell界面。shell就是一个程序,可以把指令传达给操作系统。bash=

root表示现在是root用户,@后面是所用机器的名字,/表示文件系统,#表示最高的优先级,如果是普通用户会是$

可以通过exit退出交互式ubuntu操作系统

输入以下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@38e8564fa5b8:/# whoami # 现在的用户
root
root@38e8564fa5b8:/# echo hello # 打印字段
hello
root@38e8564fa5b8:/# echo $0 # shell所在的位置
/bin/bash
root@38e8564fa5b8:/# history #历史指令
1 whoami
2 echo hello
3 echo $0
4 history
root@38e8564fa5b8:/# !2 # 重新执行历史指令2
echo hello
hello

可以通过键盘上下健执行最近的历史指令

bash的意思是Bourne-Again SHell,Bourne是unix shell的作者

管理软件包

软件包管理

  • npm
  • yarn
  • pip
  • NuGet

ubuntu中我们常用apt来安装各种软件包。apt=advanced package tool

1
2
apt install nano 
apt list

第一次安装可能会失败,可以通过list检查目前安装包数据库都有哪些包,还有一些包没有加载的可以通过更新从Ubuntu网址获得安装包。通常安装前都先更新。

1
2
3
4
5
6
apt update
more lists

apt install nano
nano
apt remove nano

输入nano可以进入编辑界面, ctrl+x可以退出

通过remove移除安装包后验证是否成功

1
2
3
root@38e8564fa5b8:/# nano
bash: /usr/bin/nano: No such file or directory

linux文件系统

/

  • bin binary文件
  • boot
  • dev devices设备
  • etc editable text configuration配置文件
  • home 用户的主目录
  • root root用户的主目录
  • lib libary软件依赖的库
  • var variable变量,例如经常变化的日志文件
  • proc running processes运行进程

文件系统导航

如何进入各种文件路径
pwd/ls

  • pwd 当前的工作路径the path of the current working directory
  • ls 列举当前所在路径下的文件list
1
2
root@38e8564fa5b8:/# ls usr
bin games include lib libexec local sbin share src

ls后面直接跟当前子路径的名称,可以看到当前子路径下有什么文件

  • ls -1 另一种视图 长列表列举出文件权限/用户等信息
  • cd 改变路径change directory

后面可加上相对路径或者绝对路径

1
2
3
4
5
6
root@38e8564fa5b8:/usr# cd /home  
root@38e8564fa5b8:/home# cd usr # 相对路径不用加/,相对于当前的路径,home下面没有usr路径所以报错
bash: cd: usr: No such file or directory
root@38e8564fa5b8:/home# cd /usr # 绝对路径加上/表示是从根目录开始
root@38e8564fa5b8:/usr#

tab键可以自动补全,如果当前路径下面有前缀重复的会列举出来,可以手动多输入一些后再用tab自动补全

cd .. 到上一层的路径,cd ../..可以到上两层的路径,cd /root或者cd ~(tilde)是到用户home路径的方法

文件和路径操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@38e8564fa5b8:/# cd root
root@38e8564fa5b8:~# ls # 当前事空的
root@38e8564fa5b8:~# mkdir test # 新建一个路径test
root@38e8564fa5b8:~# ls
test
root@38e8564fa5b8:~# mv test docker # 重命名
root@38e8564fa5b8:~# ls
docker
root@38e8564fa5b8:~# cd docker
root@38e8564fa5b8:~/docker# touch hello.txt #新建一个文件
root@38e8564fa5b8:~/docker# ls
hello.txt
root@38e8564fa5b8:~/docker# touch 1.txt 2.txt # 新建多个文件
root@38e8564fa5b8:~/docker# ls
1.txt 2.txt hello.txt
root@38e8564fa5b8:~/docker# mv hello.txt hello-docker.txt
root@38e8564fa5b8:~/docker# ls
1.txt 2.txt hello-docker.txt
root@38e8564fa5b8:~/docker# rm 1.txt 2.txt # 移除多个文件
root@38e8564fa5b8:~/docker# ls
hello-docker.txt
root@38e8564fa5b8:~/docker# cd ..
root@38e8564fa5b8:~# rm docker/
rm: cannot remove 'docker/': Is a directory
root@38e8564fa5b8:~# rm -r docker/ # 删除路径需要加上recursively
root@38e8564fa5b8:~# ls
root@38e8564fa5b8:~#

mv test docker把当前路径下的test重命名为docker,如果想把当前路径的test移动到/home路径下可以mv test /home

编辑和浏览文件

  • nano file1.txt 用nano编辑文件
  • cat 查看一个文件的内容,通常较短文件比较方便concatenate/combine
  • more /etc/adduser.conf 可以查看较长的文件

空格/enter/上下键可以看下一页,鼠标没办法上下滑动看,q退出

  • less 一般需要安装

apt install less安装后使用,空格可以看下一页,enter/上下键看下一行,鼠标上下滑动看,q退出

  • head -n 4 file 看前几行,n=number
  • tail -n 4 file 看后几行,n=number

重定向

  • cat file1.txt 查看文件数据输出字符

  • cat file1.txt > file2.txt 把文件1的数据输出到文件2保存,文件2不存在会直接新建一个

  • cat file1.txt file2.txt 两个文件的数据加一起输出

  • cat file1.txt file2.txt > combined.txt 两个文件的数据加一起输出到combined文件

  • echo hello > hello.txt 将hello写入文件,一般是一行内容写入文件

  • ls -l /etc > files.txt 列举/etc下面的文件都有什么并写入一个文件

参考链接

https://www.youtube.com/watch?v=pTFZFxd4hOI&t=3s


linux命令行
https://hexwhat.top/2024/03/31/linux-command-line/
作者
Leah
发布于
2024年3月31日
更新于
2024年5月1日
许可协议