Linux 基础速查笔记

目标:Ubuntu 22.04 开发环境 + 嵌入式 Linux(Zynq / PolarFire SoC)
标记说明:

  • 【必记】:应形成条件反射
  • 【理解】:重点掌握原理,不建议死背
  • 【记忆方法】:英文全称、缩写或联想
  • 【嵌入式关联】:与后续 Zynq / PolarFire SoC 开发相关

1. ls -l 权限字符串

示例:

-rwxr-xr-x

一共 10 个字符:

- | rwx | r-x | r-x
↑    ↑      ↑      ↑
类型 user  group  others

1.1 文件类型

【必记】

字符 英文 含义
- regular file 普通文件
d directory 目录
l symbolic link 符号链接
c character device 字符设备
b block device 块设备
p pipe / named pipe 管道 / 命名管道
s socket 套接字

【记忆方法】

d → directory
l → link
c → character
b → block
p → pipe
s → socket

- 外,基本都来自英文关键词首字母。


2. 字符设备与块设备

Character Device

c = character device = 字符设备

典型例子:

/dev/ttyUSB0
/dev/ttyS0
/dev/ttyPS0

常见于 UART / 串口等流式设备。

【嵌入式关联】

UART Hardware
    ↓
Linux Driver
    ↓
/dev/ttyPS0
    ↓
用户态程序

Block Device

b = block device = 块设备

典型设备:

例如:

/dev/mmcblk0
/dev/mmcblk0p1

【记忆方法】

block = 数据按“块”组织和访问

3. Symbolic Link

l = symbolic link = 符号链接

简称:

symlink

例如:

python -> python3

可暂时类比 Windows 快捷方式,但两者并不完全相同。


4. Pipe

p = pipe = 管道
FIFO = First In, First Out = 先进先出

用途:

IPC = Inter-Process Communication
    = 进程间通信

Shell 中:

command1 | command2

数据流:

command1 stdout
       ↓
      pipe
       ↓
command2 stdin

例如:

dmesg | grep uart

表示:

dmesg stdout → grep stdin

5. Socket

s = socket = 套接字

可以理解为:

程序之间通信的端点。

常见两类:

Network Socket
Unix Domain Socket

Network Socket:

应用
 ↓
socket
 ↓
TCP / UDP
 ↓
IP
 ↓
Ethernet

Unix Domain Socket:

用于同一 Linux 系统上的进程通信。

可能看到:

/run/example.sock

6. Linux 权限三组身份

u = user   = owner = 所有者
g = group  = 所属组
o = others = 其他用户
a = all    = u + g + o

示例:

drwxr-x---
 │   │  │
 u   g  o

7. r / w / x

r = read
w = write
x = execute

7.1 普通文件权限

r → 读取文件内容
w → 修改文件内容
x → 执行文件

例如:

-rwxr-xr-x

owner 拥有:

read + write + execute

7.2 目录权限

目录上的 r/w/x 和普通文件不同。

【必记】

r → 读取/枚举目录项
w → 修改目录项
x → search / traverse
    查找、进入、穿过目录

【记忆方法】

r = 看
w = 改
x = 走

r

可以知道目录里面有哪些名字。

read directory entries

x

可以:

w

允许修改目录项,例如:


8. 文件权限 vs 父目录权限

【必记】

修改文件内容 → 看文件本身的 w
删除文件     → 看父目录的 w + x

例如:

dr-x------ project/
-rwxrwxrwx project/app

可以修改:

project/app

但是不能删除它,因为父目录没有 w

【记忆方法】

文件决定“能不能改里面的东西”;
目录决定“能不能增删里面的名字”。


9. 数字权限

r = 4 = 100
w = 2 = 010
x = 1 = 001

【必记】

r w x
4 2 1
数字 权限
0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx

例如:

chmod 755 myapp

表示:

user   = rwx
group  = r-x
others = r-x

即:

-rwxr-xr-x

常见:

755 → rwxr-xr-x
644 → rw-r--r--
750 → rwxr-x---

10. chmod 符号模式

结构:

对象 + 操作 + 权限

对象:

u = user
g = group
o = others
a = all

操作:

+ = add
- = remove
= = set

权限:

r / w / x

示例:

chmod u+x myapp
chmod g-w myapp
chmod o+r file
chmod a-x myapp
chmod ug+rw file
chmod go-rwx myapp

11. Owner 与 Group

示例:

-rwxr-x--- 1 alice developers ... myapp
             ↑       ↑
           owner    group

权限匹配逻辑:

是 owner?
   ↓ 是
使用 user 权限

否则属于 group?
   ↓ 是
使用 group 权限

否则
使用 others 权限

三组权限不是累加关系。


12. chown / chgrp

chown = change owner
chgrp = change group

修改 owner:

sudo chown bob test.txt

同时修改 owner 和 group:

sudo chown alice:fpga project.bin

格式:

owner:group

例如:

owner = alice
group = fpga

13. root / UID / GID

root = superuser
UID = User ID
GID = Group ID

【必记】

root 的 UID = 0

例如:

uid=1000(alice)

表示:

1000  → UID
alice → username

内核更加核心地使用数字 UID/GID,而不是用户名字符串。


14. id 命令

例如:

uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo)

含义:

uid    → 用户身份
gid    → primary group
groups → 用户所属的所有组

术语:

primary group       = 主组
supplementary group = 附加组

15. usermod

usermod = user modify

给用户增加附加组:

sudo usermod -aG dialout alice

其中:

-a = append
-G = supplementary Groups

【必记】

-aG = append Groups

不要轻易写成:

usermod -G ...

因为可能覆盖用户原来的附加组。

例如:

sudo usermod -aG dialout $USER

常用于让 Ubuntu 普通用户访问串口设备。

修改组后通常需要:

注销 → 重新登录

再通过:

id

确认。


16. 串口权限实例

例如:

crw-rw---- 1 root dialout ... /dev/ttyUSB0

表示:

类型   = character device
owner  = root
group  = dialout

user   = rw-
group  = rw-
others = ---

如果当前用户属于 dialout

使用 group 权限 → rw-

如果既不是 root,又不属于 dialout:

使用 others → ---

17. Linux 路径

/

/ = root directory = 根目录

整个 Linux 文件系统从 / 开始。


~

~ = current user's home directory

例如:

alice → /home/alice
bob   → /home/bob
root  → /root

...

.  = current directory
.. = parent directory

例如:

cd ..

进入父目录。

./myapp

表示当前目录中的 myapp


18. 绝对路径与相对路径

Absolute Path

absolute path = 绝对路径

【必记】

/ 开头。

例如:

/home/bob/project/main.c
/etc/hosts
/usr/bin/gcc

Relative Path

relative path = 相对路径

依赖当前 working directory。

例如:

project/main.c

19. pwd

pwd = print working directory

作用:

显示当前工作目录

例如:

pwd

输出:

/home/bob/project

20. ls

ls = list

常见:

ls
ls -a
ls -l
ls -h
ls -alh

选项:

-a = all
-l = long listing format
-h = human-readable

常用:

ls -alh /var/log

命令结构:

command options arguments

例如:

ls      -alh      /var/log
↑         ↑           ↑
命令      选项        参数

21. mkdir

mkdir = make directory

创建目录:

mkdir project

递归创建父目录:

mkdir -p build/output/bin
-p = parents

22. touch

touch hello.txt

如果文件不存在:

创建空文件

如果文件已存在:

不清空内容
更新时间戳

touch 的核心含义:

更新 timestamp

23. cp

cp = copy

文件复制:

cp source.txt backup.txt

目录递归复制:

cp -r project backup

其中:

-r = recursive

24. mv

mv = move

移动:

mv main.c src/

重命名:

mv old.txt new.txt

25. rm

rm = remove

删除文件:

rm file.txt

递归删除:

rm -r build

强制模式:

rm -f file.txt
-f = force

【必记】

-f 并不意味着绕过 Linux 权限。

更准确地理解:

force = 不询问确认,直接尝试执行

高风险组合:

rm -rf ...

一定谨慎检查路径。


26. rmdir

rmdir = remove directory

只能删除空目录:

rmdir test

处理父目录:

rmdir -p project/src
-p = parents

rmdir -p 仍然只会删除变为空目录的层级。


27. cat

cat = concatenate

常用于显示文件:

cat file.txt

但本质是“串接内容”。

例如:

cat a.txt b.txt

按顺序输出:

a.txt 内容
b.txt 内容

28. 标准输入输出

Linux 进程通常有三个标准流:

stdin  = standard input
stdout = standard output
stderr = standard error

对应文件描述符:

0 = stdin
1 = stdout
2 = stderr

【记忆方法】

0 进
1 出
2 报错

29. 输出重定向

>

> = output redirection

覆盖写入:

echo hello > test.txt

>>

>> = append redirection

追加写入:

echo hello >> test.txt

【必记】

>  → overwrite
>> → append

30. stderr 重定向

myapp 2> error.log

表示:

stderr → error.log

追加:

myapp 2>> error.log

31. stdout 与 stderr 分开

myapp > out.log 2> error.log

结果:

stdout → out.log
stderr → error.log

32. 2>&1

myapp > all.log 2>&1

表示:

stdout → all.log
stderr → stdout 当前的位置 → all.log

最终:

stdout ─┐
        ├→ all.log
stderr ─┘

【记忆方法】

2>&1
→ 让 2 跟着 1 当前的位置走

33. 重定向顺序

Shell 从左到右处理重定向。

myapp > all.log 2>&1

最终:

stdout → all.log
stderr → all.log

而:

myapp 2>&1 > all.log

处理过程:

2>&1
stderr → stdout 当前的位置 → terminal

> all.log
stdout → all.log

最终:

stdout → all.log
stderr → terminal

34. 输入重定向

< = input redirection

例如:

myapp < input.txt

数据流:

input.txt
   ↓
stdin
   ↓
myapp

等价概念:

<  = 0<

35. Pipe 与重定向组合

例如:

dmesg | grep uart > uart.log

数据流:

dmesg
  ↓ stdout
pipe
  ↓ stdin
grep uart
  ↓ stdout
uart.log

36. grep

grep 名字来源:

g/re/p
≈ global regular expression print

核心作用:

搜索匹配内容,默认输出匹配内容所在的整行。

基础格式:

grep PATTERN FILE

例如:

grep uart kernel.log

其中:

uart       → pattern
kernel.log → file

37. grep 常见选项

-i

-i = ignore case

忽略大小写:

grep -i error app.log

-n

-n = line number

显示行号:

grep -n error app.log

-o

-o = only matching

只输出匹配部分。

例如:

ERROR: UART timeout

执行:

grep -o UART app.log

只输出:

UART

-v

-v = invert match

反向匹配:

grep -v error app.log

输出不匹配 error 的行。


-r

-r = recursive

递归搜索文件内容:

grep -r uart .

注意:

grep 搜索的是文件内容,不是文件名。


38. grep 常用组合

grep -in uart kernel.log
-i → ignore case
-n → line number
grep -inv error app.log
-i → ignore case
-n → line number
-v → invert match

39. find

find = 查找文件 / 目录

基础结构:

find WHERE CONDITIONS

例如:

find . -name "uart.c"

其中:

.     → 当前目录
-name → 按名称匹配

40. find -name / -iname

find . -name "*.c"

按名称匹配,区分大小写。

find . -iname "*.c"

忽略大小写。

【记忆方法】

i → ignore case

41. find -type

-type f → regular file
-type d → directory

例如:

find . -type f -name "*.c"

表示:

从当前目录递归查找以 .c 结尾的普通文件。


42. find 深度

最大深度

-maxdepth N
= maximum depth

例如:

find . -maxdepth 1 -type f

只处理当前目录这一层。


最小深度

-mindepth N
= minimum depth

例如:

find . -mindepth 2 -type f

只考虑 depth ≥ 2。


深度例子

.               → depth 0
./file.txt      → depth 1
./src/main.c    → depth 2
./src/a/test.c  → depth 3

组合:

find . -mindepth 2 -maxdepth 3 -type f

表示:

只匹配 depth 2 ~ 3

43. head / tail

head = 头
tail = 尾

查看开头:

head file.txt
head -n 20 file.txt

查看末尾:

tail file.txt
tail -n 50 file.txt

其中:

-n = number

实时跟踪日志:

tail -f app.log
-f = follow

【嵌入式关联】

tail -f /var/log/myapp.log

经常用于观察板端程序日志。


44. less

用于分页浏览大文件:

less kernel.log

常用按键:

q       quit
/uart   搜索 uart
n       下一个匹配
N       上一个匹配

相比:

cat huge.log

大文件通常更适合:

less huge.log

45. file

file myapp

作用:

判断文件实际的数据类型、可执行文件架构等。

例如可能显示:

ELF 64-bit ... x86-64
ELF 64-bit ... ARM aarch64
ELF 64-bit ... RISC-V

【嵌入式关联|重要】

交叉编译之后应学会检查:

file myapp

确认目标架构:

x86_64
ARM
AArch64
RISC-V

46. which

which gcc

用于查看当前 shell 会使用哪个命令程序。

例如:

/usr/bin/gcc

【记忆方法】

which gcc
→ “到底是哪一个 gcc?”

以后可能同时存在:

gcc
aarch64-linux-gnu-gcc
riscv64-linux-gnu-gcc

所以确认实际工具链路径很重要。


47. Linux 文件系统目录树初步认识

Linux 使用统一目录树:

/
├── bin
├── boot
├── dev
├── etc
├── home
├── lib
├── proc
├── sys
├── tmp
├── usr
└── var

不像 Windows 常见的:

C:\
D:\

Linux 所有文件系统最终都挂载到 / 这棵目录树中。

这对后续理解:

非常重要。


48. /dev

dev = device

/dev 主要包含:

device nodes = 设备节点

例如:

/dev/ttyUSB0
/dev/ttyPS0
/dev/mmcblk0

后续重点继续学习:

/dev
/proc
/sys
/etc
/boot
/lib

这些目录与:

Linux Kernel
Device Tree
Drivers
Root Filesystem
U-Boot
Embedded Linux

都有直接关系。


49. 当前最重要的速记表

文件类型:
- regular file
d directory
l symbolic link
c character device
b block device
p pipe
s socket
权限对象:
u user
g group
o others
a all
权限:
r read
w write
x execute
数字权限:
r = 4
w = 2
x = 1
标准流:
0 stdin
1 stdout
2 stderr
重定向:
>    stdout 覆盖
>>   stdout 追加
2>   stderr 覆盖
2>>  stderr 追加
<    stdin 输入
2>&1 stderr 跟随 stdout 当前目标
|    stdout → stdin
常用命令:
pwd     print working directory
ls      list
mkdir   make directory
cp      copy
mv      move
rm      remove
rmdir   remove directory
cat     concatenate
grep    搜索内容
find    搜索文件/目录
head    看头部
tail    看尾部
less    分页查看
file    判断文件真实类型
which   查找实际执行的命令

50. 面向嵌入式 Linux 的知识链

后续学习主线:

Linux CLI / 文件系统
        ↓
Shell / 权限 / 进程
        ↓
C/C++ + GCC + Makefile
        ↓
系统调用
        ↓
文件描述符 / IPC
        ↓
设备文件 / proc / sysfs
        ↓
交叉编译
        ↓
ARM / AArch64 / RISC-V
        ↓
U-Boot
        ↓
Linux Kernel
        ↓
Device Tree
        ↓
Kernel Module
        ↓
Driver
        ↓
Buildroot / Yocto
        ↓
Zynq / PolarFire SoC

目标不是背 Linux 命令,而是逐渐形成:

看到问题
  ↓
判断属于:
文件?
权限?
进程?
系统调用?
动态库?
内核?
驱动?
硬件?
  ↓
选择对应工具定位