文件管理与IO重定向实战:从目录结构到管道魔法

1 阅读8分钟

文件管理与IO重定向实战:从目录结构到管道魔法

发布日期:2026-10-16 | 标签:Linux文件管理、IO重定向、管道、inode、硬链接


目录导航


一、Linux文件系统目录结构

1.1 FHS标准目录

Linux遵循FHS(Filesystem Hierarchy Standard)标准,目录结构有明确分工:

ls -la /

真实输出:

total 100
drwxr-xr-x  24 root root  4096 Oct 16 22:35 .
drwxr-xr-x  24 root root  4096 Oct 16 22:35 ..
lrwxrwxrwx   1 root root     7 Apr 22  2024 bin -> usr/bin
drwxr-xr-x   3 root root  4096 Jul 21 17:33 boot
drwxr-xr-x  19 root root  3940 Oct 16 22:35 dev
drwxr-xr-x 118 root root 12288 Oct 16 22:35 etc
drwxr-xr-x   2 root root  4096 Jul 21 17:26 home
lrwxrwxrwx   1 root root     7 Apr 22  2024 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Apr 22  2024 lib64 -> usr/lib64
drwx------   2 root root 16384 Jul 21 16:51 lost+found
drwxr-xr-x   2 root root  4096 Apr 23  2024 media
drwxr-xr-x   2 root root  4096 Apr 23  2024 mnt
drwxr-xr-x   2 root root  4096 Apr 23  2024 opt
dr-xr-xr-x 231 root root     0 Oct 16 22:35 proc
drwx------   4 root root  4096 Jul 21 17:34 root
drwxr-xr-x  31 root root   900 Oct 16 22:55 run
lrwxrwxrwx   1 root root     8 Apr 22  2024 sbin -> usr/sbin
drwxr-xr-x   2 root root  4096 Apr 23  2024 srv
dr-xr-xr-x  13 root root     0 Oct 16 22:35 sys
drwxrwxrwt  13 root root  4096 Oct 16 22:35 tmp
drwxr-xr-x  12 root root  4096 Apr 23  2024 usr
drwxr-xr-x  13 root root  4096 Jul 21 17:25 var

1.2 核心目录说明

目录作用说明
/bin基本命令软链接到 usr/bin
/boot启动文件内核、GRUB等
/dev设备文件硬件设备抽象
/etc配置文件系统和应用配置
/home用户家目录普通用户文件
/rootroot家目录管理员家目录
/proc进程信息虚拟文件系统
/tmp临时文件重启可能清空
/usr用户程序应用软件位置
/var可变数据日志、缓存等

二、文件类型识别

2.1 file命令

file /etc/passwd /etc/shadow /bin/ls /dev/null

真实输出:

/etc/passwd:   ASCII text
/etc/shadow:   ASCII text
/bin/ls:       ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, stripped
/dev/null:     character special (1/3)

2.2 Linux七种文件类型

代号类型说明
-普通文件文本、二进制等
d目录目录文件
l软链接符号链接
b块设备硬盘、U盘
c字符设备终端、串口
p管道命名管道
s套接字网络通信

三、文件操作命令实战

3.1 创建文件和目录

touch /tmp/test_file.txt
mkdir -p /tmp/test_dir/subdir
ls -laR /tmp/test_dir/

真实输出:

/tmp/test_dir/:
total 12
drwxr-xr-x  3 root root 4096 Oct 16 22:56 .
drwxrwxrwt 14 root root 4096 Oct 16 22:56 ..
drwxr-xr-x  2 root root 4096 Oct 16 22:56 subdir

/tmp/test_dir/subdir:
total 8
drwxr-xr-x 2 root root 4096 Oct 16 22:56 .
drwxr-xr-x 3 root root 4096 Oct 16 22:56 ..

3.2 复制、移动、删除

cp /etc/passwd /tmp/test_copy.txt
ls -la /tmp/test_copy.txt
mv /tmp/test_copy.txt /tmp/test_moved.txt
ls -la /tmp/test_moved.txt
rm /tmp/test_file.txt
ls -la /tmp/test_file.txt 2>&1

真实输出:

-rw-r--r-- 1 root root 1863 Oct 16 22:56 /tmp/test_copy.txt
-rw-r--r-- 1 root root 1863 Oct 16 22:56 /tmp/test_moved.txt
ls: cannot access '/tmp/test_file.txt': No such file or directory

3.3 stat命令

stat /etc/passwd

真实输出:

  File: /etc/passwd
  Size: 1863      Blocks: 8          IO Block: 4096   regular file
Device: 253,1 Inode: 524961      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/  root)   Gid: (    0/  root)
Access: 2026-10-16 22:35:17.544000323 +0800
Modify: 2026-07-21 17:26:29.489665977 +0800
Change: 2026-07-21 17:26:29.494665977 +0800
 Birth: 2026-07-21 17:26:29.489665977 +0800

三个时间戳:Access(访问)、Modify(内容修改)、Change(属性修改)。


四、通配符详解

通配符含义示例
*匹配任意数量字符*.conf
?匹配单个字符?????.conf
[]匹配括号内任意字符[a-c]*
ls /etc/*.conf 2>/dev/null | head -10

真实输出:

/etc/adduser.conf
/etc/ca-certificates.conf
/etc/debconf.conf
/etc/deluser.conf
/etc/dhcpcd.conf
/etc/e2scrub.conf
/etc/fuse.conf
/etc/gai.conf
/etc/hdparm.conf
/etc/host.conf
ls /etc/[a-c]* 2>/dev/null | head -10

真实输出:

/etc/adduser.conf
/etc/bash.bashrc
/etc/bash_completion
/etc/bindresvport.blacklist
/etc/ca-certificates.conf
/etc/ca-certificates.conf.dpkg-old
/etc/crontab
/etc/crypttab

五、inode与硬软链接

5.1 inode

每个文件都有唯一的inode(索引节点),存储元数据但不包含文件名。

ls -lai /root/ 2>/dev/null

真实输出:

total 24
131076 drwx------  4 root root 4096 Jul 21 17:34 .
     2 drwxr-xr-x 24 root root 4096 Oct 16 22:35 ..
184656 -rw-r--r--  1 root root    0 Jul 21 17:34 .bash_history
131078 -rw-r--r--  1 root root 3106 Apr 22  2024 .bashrc
264353 drwx------  2 root root 4096 Jul 21 17:25 .cache
131170 -rw-------  1 root root    0 Jul 21 17:34 .history
131079 -rw-r--r--  1 root root  161 Apr 22  2024 .profile
131077 drwx------  2 root root 4096 Oct 16 22:35 .ssh
df -i

真实输出:

Filesystem      Inodes  IUsed   IFree IUse% Mounted on
tmpfs          1936872    705 1936167    1% /run
/dev/vda1      2621440 104855 2516585    4% /
tmpfs          1936872      1 1936871    1% /dev/shm
tmpfs          1936872      3 1936869    1% /run/lock
tmpfs           387374     33  387341    1% /run/user/0

根分区共262万个inode,已用10万,使用率仅4%。

5.2 硬链接 vs 软链接

ln /etc/passwd /tmp/hardlink_test
ln -s /etc/passwd /tmp/softlink_test
ls -li /tmp/hardlink_test /etc/passwd
ls -li /tmp/softlink_test
readlink /tmp/softlink_test

真实输出:

524961 -rw-r--r-- 2 root root 1863 Jul 21 17:26 /etc/passwd
524961 -rw-r--r-- 2 root root 1863 Jul 21 17:26 /tmp/hardlink_test
1593 lrwxrwxrwx 1 root root 11 Oct 16 22:56 /tmp/softlink_test -> /etc/passwd
/etc/passwd
特性硬链接软链接
inode与原文件相同不同
原文件删除仍可访问失效
跨文件系统不支持支持
链接数+1不变

硬链接inode号(524961)与原文件相同,链接数变为2。软链接有独立inode(1593)。


六、IO重定向

6.1 标准IO三股流

流编号默认方向说明
stdin0键盘标准输入
stdout1屏幕标准输出
stderr2屏幕标准错误

6.2 输出重定向

echo "hello world" > /tmp/redirect_test.txt
cat /tmp/redirect_test.txt
echo "append line" >> /tmp/redirect_test.txt
cat /tmp/redirect_test.txt

真实输出:

hello world
hello world
append line

6.3 错误重定向

ls /nonexist 2> /tmp/error.txt
cat /tmp/error.txt

真实输出:

ls: cannot access '/nonexist': No such file or directory

6.4 合并stdout和stderr

ls /etc/passwd /nonexist 2>&1 | head -5

真实输出:

ls: cannot access '/nonexist': No such file or directory
/etc/passwd

6.5 重定向速查表

语法含义
> filestdout覆盖写入
>> filestdout追加写入
2> filestderr写入
2>&1stderr重定向到stdout
&> filestdout和stderr都写入
< file从file读取stdin
> /dev/null丢弃输出

七、管道技术

管道 | 将前一个命令的stdout连接到后一个命令的stdin。

7.1 统计nologin用户数量

cat /etc/passwd | grep nologin | wc -l

真实输出:

30

7.2 统计/etc下的目录数量

ls -l /etc/ | grep "^d" | wc -l

真实输出:

116

7.3 提取用户名和Shell

cat /etc/passwd | head -5 | cut -d: -f1,7

真实输出:

root:/bin/bash
daemon:/usr/sbin/nologin
bin:/usr/sbin/nologin
sys:/usr/sbin/nologin
sync:/bin/sync

7.4 常用管道组合

组合用途
cmdgrep "pattern"过滤包含模式的行
cmdwc -l统计行数
cmdsortuniq排序去重
cmdhead -n取前n行
cmdcut -d: -f1按分隔符提取字段
cmdawk '{print }'提取第一列

八、总结与实战技巧

核心要点

  1. Linux目录遵循FHS标准,每个目录有明确用途
  2. file命令识别文件真实类型,Linux有7种文件类型
  3. inode是文件的唯一标识,存储元数据
  4. 硬链接共享inode,软链接是独立文件指向目标路径
  5. IO重定向:>覆盖、>>追加、2>错误、2>&1合并
  6. 管道将命令串联,实现数据流式处理

实战技巧

  • stat查看文件三个时间戳,排查文件修改时间很有用
  • 硬链接不能跨文件系统,软链接可以
  • 2>&1顺序很重要:cmd 2>&1 | grep 比 cmd | grep 2>&1 更常用
  • 管道是Linux哲学的核心:每个命令做好一件事,管道负责组合

下一篇:Linux用户和权限管理详解


本文所有命令均在 Ubuntu 24.04.4 LTS 服务器(1.94.230.146)上真实执行。