Unix Cheat Sheet

Unix & Linux 相关备忘。

Linux通用

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 操作系统
uname -a
# LSB 和特定版本的相关信息
lsb_release -a
cat /etc/os-release
# cpu
cat /proc/cpuinfo | grep 'core id'
sudo lshw -class processor
sudo dmidecode -t 4
ncpu
lscpu
# 上传下载
sftp user@host # get, put 方法
scp
# 帮助
whatis
man
info
# 设置时间
sudo date --set "2021-07-01 01:00:00"
sudo date -s "2021-07-01 01:00:00"
# IP地址查询
dns-sd -q google.com
# 查找
find
locate
# 进程
ps -eo pid,command # -e 列出全部进程 -o 输出格式
# 按指定列排序

ps aux | awk '{print $1, $2, $3, $11}' | sort -n -k3 -r | head -n 10
# 重定向
ls 1> output.log 2> error.log # 1是标准输出 2是错误
ls &> output_error.log # &标准输出+错误
grep abc < content # 重定向 grep 的标准输入来自 content
grep abc < content &> out_error.log
# 管道
grep xxx < input.txt | sort # input中包含 xxx 的行按字母表排序

# 文本搜索
# -r recursive; -n line number; -w whole word match; -l lower-case -e pattern
grep -rnw 'path/to' -e 'pattern'
# --include/exclude/exclude_dir
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"

# nvidia-smi
nvidia-smi -l 5 # 5秒刷新一次(推荐)
watch -n 1 -d nvidia-smi # 1秒刷新,只改中间数字

# 用户/组
sudo adduser xxx
sudo deluser --remove-home xxx
su xxx # 切换成用户xxx
sudo groupadd yyy # 创建用户组
sudo groupdel yyy
groups [xxx] # 查看组,xxx 表示用户名
id [xxx] # 查看id
/etc/passwd # user:passwd:uid:gid:description:userdir:shell
/etc/group # gname:gpasswd:gid:userlist
# 文件
Owner/OwnerGroup/Other # 三级权限
chown # 改变文件的 Owner 和 OwnerGroup
chmod # 改变文件权限标志;444 表示三类用户权限;4读取 2写入 1执行,求和
# 删除某个日期之前的
find . ! -newermt "2020-05-29 00:00:00" | xargs rm -rf
# 删除某个端口
sudo lsof -i tcp:2222 | grep ssh | awk '{print $2}' | xargs kill -9
sudo fuser -k 5000/tcp
# 隧道
ssh -N -f -L localhost:3307:192.168.1.51:3306 username@host -i file
# firewall防火墙
systemctl status firwalld # 查看状态
firewall-cmd --list-ports # 查看放行端口
firewall-cmd --reload # 重启
firewall-cmd --zone=public --add-port=80/tcp --permanent # 开放端口,参数分别为:作用域、端口格式、永久生效
systemctl start/stop firewalld.service
systemctl disable firewalld.service # 禁止开机启动
# iptable防火墙
service iptables status
service iptables stop # 关闭
service iptables save # 保存规则
iptables -L -n -v # 查看规则
iptables -F # 清空规则
iptables -A INPUT/OUTPUT -p tcp --dport 80 -j REJECT/ACCEPT # 禁止/允许端口80访问/出去的数据包
systemctl start iptabels # 打开
systemctl enable iptables # 开机启动
systemctl disable iptables # 开机关闭

Centos

自定义安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 搜索
yum search
https://pkgs.org/

# 下载
yumdownloader --destdir ~/rpm --resolve vim-common

# 解压/安装
cd /path/to/your/centos && rpm2cpio /path/to/x.rpm | cpio -id
# rpm2cpio outputs the .rpm file as a .cpio archive on stdout.
# cpio reads it from from stdin
# -i means extract (to the current directory)
# -d means create missing directory

# 配置环境变量
export PATH="/usr/tmp/centos/usr/sbin:/var/tmp/centos/usr/bin:/var/tmp/centos/bin:$PATH"
export MANPATH="/var/tmp/centos/usr/share/man:$MANPATH"
L='/lib:/lib64:/usr/lib:/usr/lib64'
export LD_LIBRARY_PATH="/var/tmp/centos/usr/lib:/var/tmp/centos/usr/lib64:$L"

Raspberrypi

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
28
29
30
31
32
33
34
35
36
37
38
# CPU info
lscpu
# 内存
free -h
# 内存和交换内存
free -g
# 列出所有的 PCI 硬件
lspci | grep -e "NVIDIA"
# 磁盘
sudo fdisk -l
# USB
lsusb
# 各种配置
sudo raspi-config
# 更新固件
sudo rpi-update
# 温度
vcgencmd measure_temp
# 核心电压
vcgencmd measure_volts core
# 开机启动 /etc/init.d
vim /etc/init.d/test # 创建脚本
sudo service test start # 脚本切换状态
sudo update-rc.d cron defaults # 软链接
# 蓝牙
bluetoothd -v # 版本
systemctl status[start/stop/enable] bluetooth # 状态/启动/停止/开启启动
bluetoothctl # 进入蓝牙 Shell
list # 显示可用的蓝牙模块
scan on # 开启扫描
devices # 打印扫描到的蓝牙设备和 MAC 地址、名称
sudo hciconfig hci0 up[down] # 启动/关闭蓝牙模块,hci0 指 0号HCI设备(树莓派的蓝牙适配器)
hcidump # 蓝牙设备工作日志
# 摄像头
raspistill -o image.jpg
raspivid -o video.h264 -t 10000 # 10000ms = 10s
# 进程树
pstree

Errors