
文件管理与IO重定向实战从目录结构到管道魔法发布日期2026-10-16 | 标签Linux文件管理、IO重定向、管道、inode、硬链接目录导航一、Linux文件系统目录结构二、文件类型识别三、文件操作命令实战四、通配符详解五、inode与硬软链接六、IO重定向七、管道技术八、总结与实战技巧一、Linux文件系统目录结构1.1 FHS标准目录Linux遵循FHSFilesystem 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 lostfound 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 var1.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.txtmkdir-p/tmp/test_dir/subdirls-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.txtls-la/tmp/test_copy.txtmv/tmp/test_copy.txt /tmp/test_moved.txtls-la/tmp/test_moved.txtrm/tmp/test_file.txtls-la/tmp/test_file.txt21真实输出-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 directory3.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/*.conf2/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.confls/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 .sshdf-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_testln-s/etc/passwd /tmp/softlink_testls-li/tmp/hardlink_test /etc/passwdls-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。软链接有独立inode1593。六、IO重定向6.1 标准IO三股流流编号默认方向说明stdin0键盘标准输入stdout1屏幕标准输出stderr2屏幕标准错误6.2 输出重定向echohello world/tmp/redirect_test.txtcat/tmp/redirect_test.txtechoappend line/tmp/redirect_test.txtcat/tmp/redirect_test.txt真实输出hello world hello world append line6.3 错误重定向ls/nonexist2/tmp/error.txtcat/tmp/error.txt真实输出ls: cannot access /nonexist: No such file or directory6.4 合并stdout和stderrls/etc/passwd /nonexist21|head-5真实输出ls: cannot access /nonexist: No such file or directory /etc/passwd6.5 重定向速查表语法含义 filestdout覆盖写入 filestdout追加写入2 filestderr写入21stderr重定向到stdout filestdout和stderr都写入 file从file读取stdin /dev/null丢弃输出七、管道技术管道 | 将前一个命令的stdout连接到后一个命令的stdin。7.1 统计nologin用户数量cat/etc/passwd|grepnologin|wc-l真实输出307.2 统计/etc下的目录数量ls-l/etc/|grep^d|wc-l真实输出1167.3 提取用户名和Shellcat/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/sync7.4 常用管道组合组合用途cmdgrep “pattern”cmdwc -lcmdsortcmdhead -ncmdcut -d: -f1cmdawk ‘{print }’八、总结与实战技巧核心要点Linux目录遵循FHS标准每个目录有明确用途file命令识别文件真实类型Linux有7种文件类型inode是文件的唯一标识存储元数据硬链接共享inode软链接是独立文件指向目标路径IO重定向覆盖、追加、2错误、21合并管道将命令串联实现数据流式处理实战技巧stat查看文件三个时间戳排查文件修改时间很有用硬链接不能跨文件系统软链接可以21顺序很重要cmd 21 | grep 比 cmd | grep 21 更常用管道是Linux哲学的核心每个命令做好一件事管道负责组合下一篇Linux用户和权限管理详解本文所有命令均在 Ubuntu 24.04.4 LTS 服务器1.94.230.146上真实执行。