Skip to content

Linux 常用命令

快捷键

# 命令行历史记录搜索
# begins a backward search through command history.
Ctrl + r

# 光标位置
# move to the beginning of line.
Ctrl + a
# move to the end of line.
Ctrl + e

# if you've type something, Ctrl + d deletes the character under the cursor
# else, it escapes the current shell.
Ctrl + d

# delete all text from the cursor to the end of line.
Ctrl + k

# delete all text from the beginning of line to the cursor.
Ctrl + x + backspace

# transpose the character before the cursor with the one under the cursor,
# press Esc + t to transposes the two words before the cursor.
Ctrl + t

# cut the word before the cursor; then Ctrl + y paste it
Ctrl + w
# cut the line before the cursor; then Ctrl + y paste it
Ctrl + u

# launch editor defined by $EDITOR to input your command.
# Useful for multi-line commands.
Ctrl + x + Ctrl + e

# 终端输出
# to stop output to terminal.
Ctrl + s

#to resume output to terminal after Ctrl + s.
Ctrl + q

# equivalent to clear.
Ctrl + l
sh
openssl rand -base64 32

压缩

bash
# 压缩
zip FileName.zip DirName
  • -A:调整可执行的自动解压缩文件;
  • -b<工作目录>:指定暂时存放文件的目录;
  • -c:替每个被压缩的文件加上注释;
  • -d:从压缩文件内删除指定的文件;
  • -D:压缩文件内不建立目录名称;
  • -f:此参数的效果和指定“-u”参数类似,但不仅更新既有文件,如果某些文件原本不存在于压缩文件内,使用本参数会一并将其加入压缩文件中;
  • -F:尝试修复已损坏的压缩文件;
  • -g:将文件压缩后附加在已有的压缩文件之后,而非另行建立新的压缩文件;
  • -h:在线帮助;
  • -i<范本样式>:只压缩符合条件的文件;
  • -j:只保存文件名称及其内容,而不存放任何目录名称;
  • -J:删除压缩文件前面不必要的数据;
  • -k:使用 MS-DOS 兼容格式的文件名称;
  • -l:压缩文件时,把 LF 字符置换成 LF+CR 字符;
  • -ll:压缩文件时,把 LF+cp 字符置换成 LF 字符;
  • -L:显示版权信息;
  • -m:将文件压缩并加入压缩文件后,删除原始文件,即把文件移到压缩文件中;
  • -n<字尾字符串>:不压缩具有特定字尾字符串的文件;
  • -o:以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同;
  • -q:不显示指令执行过程;
  • -r:递归处理,将指定目录下的所有文件和子目录一并处理;
  • -S:包含系统和隐藏文件;
  • -t<日期时间>:把压缩文件的日期设成指定的日期;
  • -T:检查备份文件内的每个文件是否正确无误;
  • -u:更换较新的文件到压缩文件内;
  • -v:显示指令执行过程或显示版本信息;
  • -V:保存 VMS 操作系统的文件属性;
  • -w:在文件名称里假如版本编号,本参数仅在 VMS 操作系统下有效;
  • -x<范本样式>:压缩时排除符合条件的文件;
  • -X:不保存额外的文件属性;
  • -y:直接保存符号连接,而非该链接所指向的文件,本参数仅在 UNIX 之类的系统下有效;
  • -z:替压缩文件加上注释;
  • -$:保存第一个被压缩文件所在磁盘的卷册名称;
  • -<压缩效率>:压缩效率是一个介于 1~9 的数值。
bash
# 解压
unzip FileName.zip
  • -c:将解压缩的结果显示到屏幕上,并对字符做适当的转换;
  • -f:更新现有的文件;
  • -l:显示压缩文件内所包含的文件;
  • -p:与-c 参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换;
  • -t:检查压缩文件是否正确;
  • -u:与-f 参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中;
  • -v:执行时显示详细的信息;
  • -z:仅显示压缩文件的备注文字;
  • -a:对文本文件进行必要的字符转换;
  • -b:不要对文本文件进行字符转换;
  • -C:压缩文件中的文件名称区分大小写;
  • -j:不处理压缩文件中原有的目录路径;
  • -L:将压缩文件中的全部文件名改为小写;
  • -M:将输出结果送到 more 程序处理;
  • -n:解压缩时不要覆盖原有的文件;
  • -o:不必先询问用户,unzip 执行后覆盖原有的文件;
  • -P<密码>:使用 zip 的密码选项;
  • -q:执行时不显示任何信息;
  • -s:将文件名中的空白字符转换为底线字符;
  • -V:保留 VMS 的文件版本信息;
  • -X:解压缩时同时回存文件原来的 UID/GID;
  • -d<目录>:指定文件解压缩后所要存储的目录;
  • -x<文件>:指定不要处理.zip 压缩文件中的哪些文件;
  • -Z:unzip-Z 等于执行 zipinfo 指令。

tar

对当前路径下所有的.txt后缀的文件进行归档,并使用gzip对归档文件进行压缩。

sh
tar -czvf test.tar.gz test/

使用下面这条命令,我们可以把一个gzip压缩的归档文件进行解压缩。

sh
tar -zxvf test.tar.gz
选项参数长选项说明
-c—create创建一个新的归档文件
-r—append往归档文件末尾追加文件
-u—update将更新的文件更新到归档中
-x—get, —extract将归档中解压文件
-d—diff, —compare对归档和文件系统中的文件进行差异比较
—delete从归档文件中删除指定文件
-t—list查看归档文件中的内容列表
-v—verbose显示执行过程
-z—gzip使用 gzip 进行压缩
-f—file指定备份文件名称

zip

sh
# First cd to the directory, they run:
zip -r -D ../myzipfile .
# you will see the myzipfile.zip in the parent directory (cd ..)

unzip myzipfile.zip

Grep

grep 的全写是 global regular expression print (全局正则表达式打印)。 在文件中进行搜索,或者与管道符配合使用

sh
grep document.getElementById index.md

less index.md | grep -n document.getElementById
sh
grep -nC 2 document.getElementById index.md
-n显示行号
-C相应行的前后各输出 2 行,以提供更多的上下文。
-v反转结果
-i搜索默认是区分大小写的。使用参数 -i 可以使其不再区分。

find

find 命令可以用来按特定的模式搜索文件和文件夹。它以递归的方式进行搜索。

find 命令的参数含义
-type指定搜索的文件类型:d 目录文件,b 块设备,c 字符设备,l 符号链接
-size指定文件大小:n(单位 k
-empty只搜索空的文件或空目录
-ctime指定搜索创建时间在 n 小时前创建的文件

查找当前目录树下所有扩展名为 .js 的文件

sh
find . -name '*.js'

磁盘空间

磁盘已使用空间

要查看磁盘空间,我们可以使用 df 命令,这个命令的英文全称是 disk free。

sh
# df -h
文件系统          容量   已用  可用  已用% 挂载点
devtmpfs        909M     0  909M    0% /dev
tmpfs           919M     0  919M    0% /dev/shm
tmpfs           919M  732K  919M    1% /run
tmpfs           919M     0  919M    0% /sys/fs/cgroup
/dev/vda1        40G   13G   25G   35% /

df 不加任何参数,默认会以千字节为单位进行信息展示。但这种方式对于我们来说并不直观。-h 的全称是 human readable。

参数长参数说明
-a—all统计全部类型
-B—block-size=SIZE改变显示的单位,如: -BM 以 MB 为单位打印磁盘容量
-h—human-readable以易读的方式显示单位(以 1024 的 n 次方)
-H—si以 1000 字节为单位换算
-i—inodes列出索引节点信息而不是块使用情况
-k—block-size指定 block 块的大小,如 1K
-l—local限制只统计本地文件系统信息
—no-sync在获取使用情况信息之前不调用同步(默认)
—sync在获取使用情况信息之前调用同步
—output[=FIELD_LIST]定义输出格式,默认使用的 FIELD_LIST,省略 FIELD_LIST 则打印全部字段
-P—portability使用 POSIX 的输出格式
—total
-t—type=TYPE指定要统计的文件类型
-T—print-type显示文件类型
-x—exclude-type=TYPE排除文件的类型
-v(忽略)
—help显示帮助
—version显示版本信息

查看日志文件大小

sh
# du -sh /var/log/* |sort -rn |head -10
220K	/var/log/secure-20220508
216K	/var/log/btmp
172K	/var/log/cloud-init.log
152K	/var/log/cron-20220515
152K	/var/log/cron-20220508
148K	/var/log/cron-20220501
148K	/var/log/cron-20220424
140K	/var/log/messages-20220501
136K	/var/log/wtmp
116K	/var/log/cron

-s 或--summarize 仅显示总计。

扩容

sh
yum install gdisk -y

yum install -y cloud-utils-growpart

growpart /dev/vda 1

xfs_growfs /dev/vda1

下载

网络

IP

将域名解析为 ip 地址

sh
$ dig +short www.baidu.com
36.152.44.96
36.152.44.95

$ host www.baidu.com
www.baidu.com has address 36.152.44.95
www.baidu.com has address 36.152.44.96
www.baidu.com is an alias for www.a.shifen.com.
sh
$ host -t txt www.biadu.com
www.biadu.com has no TXT record

$ dig -t txt www.baidu.com

; <<>> DiG 9.10.6 <<>> -t txt www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15058
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.			IN	TXT

;; ANSWER SECTION:
www.baidu.com.		732	IN	CNAME	www.a.shifen.com.

;; AUTHORITY SECTION:
a.shifen.com.		600	IN	SOA	ns1.a.shifen.com. baidu_dns_master.baidu.com. 2205160004 5 5 2592000 3600

;; Query time: 113 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Thu May 19 09:28:38 CST 2022
;; MSG SIZE  rcvd: 115

ping

发送一个有限 TTL 为 10 的 ping (TTL: Time-To-Live)这是数据包在被丢弃之前在互联网上可以通过的最大跳数。

sh
 $ ping www.baidu.com -t 10

PING www.baidu.com (36.152.44.96): 56 data bytes
64 bytes from 36.152.44.96: icmp_seq=0 ttl=53 time=44.691 ms
64 bytes from 36.152.44.96: icmp_seq=1 ttl=53 time=45.623 ms
64 bytes from 36.152.44.96: icmp_seq=2 ttl=53 time=44.885 ms
64 bytes from 36.152.44.96: icmp_seq=3 ttl=53 time=36.249 ms
64 bytes from 36.152.44.96: icmp_seq=4 ttl=53 time=39.770 ms
64 bytes from 36.152.44.96: icmp_seq=5 ttl=53 time=49.801 ms
64 bytes from 36.152.44.96: icmp_seq=6 ttl=53 time=37.266 ms
64 bytes from 36.152.44.96: icmp_seq=7 ttl=53 time=44.180 ms
64 bytes from 36.152.44.96: icmp_seq=8 ttl=53 time=34.913 ms
64 bytes from 36.152.44.96: icmp_seq=9 ttl=53 time=43.040 ms

--- www.baidu.com ping statistics ---
10 packets transmitted, 10 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 34.913/42.042/49.801/4.539 ms

检查主机连接

sh
$ nc -vw5 baidu.com 80

found 0 associations
found 1 connections:
     1:	flags=82<CONNECTED,PREFERRED>
	outif en0
	src 172.20.238.10 port 62762
	dst 220.181.38.148 port 80
	rank info not available
	TCP aux info available

Connection to baidu.com port 80 [tcp/http] succeeded!
sh
# install nmap
brew install nmap

#notice that some companies might not like you using nmap
nmap -sT -O localhost

# check port 0-65535
nmap  -p0-65535 localhost

sudo 权限

sh
# 登录 root
su - root

# 添加 blade 用户的 sudo 权限
sudo usermod -a -G wheel blade

# 查看是否添加成功
getent group wheel
wheel:x:10:xixi

系统

文件执行权限

bash
chmod +x ./deploy.sh
./deploy.sh

复制文件

sh
scp -r directoryname user@ip:/path/to/send

其他

tree

bash
$ tree
home/
└── project
    ├── 1
    ├── 2
    ├── 3
    ├── 4
    └── 5

设置文件夹展示的层级

bash
tree -L 1
home/
 └── project

设置忽略的文件和文件夹

bash
tree -I node_modules

tree -I 'node_modules|cache|test_*'

Nginx

Nginx 的命令在控制台中输入 nginx -h 就可以看到完整的命令,这里列举几个常用的命令:

sh
nginx -s reload  # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen	 # 重启 Nginx
nginx -s stop    # 快速关闭
nginx -s quit    # 等待工作进程处理完成后关闭
nginx -T         # 查看当前 Nginx 最终的配置
nginx -t -c <配置路>    # 检查配置是否有问题,如果已经在配置目录,则不需要-c

systemctl 是 Linux 系统应用管理工具 systemd 的主命令,用于管理系统,我们也可以用它来对 Nginx 进行管理,相关命令如下:

sh
systemctl start nginx    # 启动 Nginx
systemctl stop nginx     # 停止 Nginx
systemctl restart nginx  # 重启 Nginx
systemctl reload nginx   # 重新加载 Nginx,用于修改配置后
systemctl enable nginx   # 设置开机启动 Nginx
systemctl disable nginx  # 关闭开机启动 Nginx
systemctl status nginx   # 查看 Nginx 运行状态

Reference