CentOS7 实操干货一文吃透 Linux 文件系统中 所有文件操作命令 通配符1、文件系统层次结构FHS—— Linux的“小区规划图”FHS 文件系统层次结构标准通俗理解Linux系统就像一个大小区FHS就是政府出的“统一规划图”规定/etc 必须放配置文件就像小区配电房/bin 必须放命令就像门卫室 这样不管谁装的Linux找东西都在老地方目录通俗大白话解释/usr“软件安装区”—— 系统安装的软件都放这儿。/usr/bin是“用户命令工具箱”/usr/local是“自己加的私货”/etc“系统设置中心”—— 所有配置文件的老窝相当于Windows的注册表/var“动态数据仓库”—— 老变的数据放这里比如日志文件/var/log、缓存、数据库/run“本次开机临时记录”—— 记录本次开机跑起来的进程一重启就清空合并了老版的/var/run/home“普通用户的卧室”—— 每个用户有自己的小房间比如/home/xuhaoyu就是xuhaoyu的家/root“管理员皇帝的卧室”—— root用户专属普通用户进不去/tmp“临时垃圾场”—— 谁都能往里扔东西10天没人动就自动清理/boot“开机启动区”—— 存着开机需要的“钥匙”和“引导程序”/dev“硬件映射区”—— 把硬盘、U盘等硬件变成“文件”系统通过操作这些文件来操作硬件2、文件路径导航一、 便捷的符号符号含义记忆口诀.当前目录“我在这儿”..上一级目录“我上面有人”~家目录“回我自己家”/根目录“万物起源”-上一个目录“我刚从那儿来”*通配符所有“管你叫啥”二、导航路径 cd 和 pwd一、常见路径说明绝对路径以根(/)开头的路径用于指定文件的确切位置。当前工作路径当前所处的位置。相对路径不是以根(/)开头的路径相对当前工作目录的路径。一般情况是怎么方便怎么来。但是在变量定义文件的路径时候尽量使用绝对路径。# pwd Print Working Directory 打印当前工作目录# 通俗说问问系统“我现在在哪个房间”[xuhaoyucentos7 ~]$pwd#/home/xuhaoyu#输出结果当前/home/xuhaoyu 这个位置# cd Change Directory 切换目录# 通俗说从当前房间走到另一个房间[xuhaoyucentos7 ~]$cd/usr/bin[xuhaoyucentos7 bin]$pwd/usr/bin# /usr/bin# ls LiSt 列出当前房间里的东西[xuhaoyucentos7 bin]$ls#输出结果#[ mpartition#a2p mpls_dump#ab mpris-proxy#... ...二、重点符号通俗解读核心符号场景大白话解释~cd ~“我的家”—— 当前用户的家目录。xuhaoyu用户就是/home/xuhaoyuroot就是/root//usr/bin“根目录”—— 整棵目录树的树根所有路径的起点.cp /etc/hosts .“当前目录”—— 代表“我现在站的位置”。cp file . “把文件复制到我当前站的地方”..cd ..“上一级目录”—— 代表“我现在的上一级文件夹”。cd .. “退回到上一层”-cd -“刚离开的地方”—— 代表“上一次所在的目录”。cd - “回到我上一秒站着的地方”# 切换到上一次所在目录比如从/usr/bin跑到/usr/share/doc再跑回来[xuhaoyucentos7 doc]$cd-#/usr/bin[xuhaoyucentos7 bin]$pwd/usr/bin# 切换到家目录两种写法等价[xuhaoyucentos7 bin]$cd#或[xuhaoyucentos7 bin]$cd~[xuhaoyucentos7 ~]$pwd#/home/xuhaoyu# 两种方式都回到了家# .. 代表上一级目录父目录[xuhaoyucentos7 ~]$pwd#/home/xuhaoyu[xuhaoyucentos7 ~]$cd..# “往上走一层”[xuhaoyucentos7 home]$pwd#/home # 现在在 /home 了# . 代表当前目录可以省略不写[xuhaoyucentos7 ~]$file./.bash_history# 明确写“当前目录下的.bash_history”[xuhaoyucentos7 ~]$file.bash_history# 省略 . 也一样# 用 .. 相对路径“爬楼梯”# 从 /usr/share/doc/at-spi2-core-2.28.0/ 爬到/usr/share/[xuhaoyucentos7 ~]$cd/usr/share/doc/at-spi2-core-2.28.0/[xuhaoyucentos7 at-spi2-core-2.28.0]$cd../../# 解读.. 到 at-spi2-core-2.28.0 的上一层doc再 .. 到 doc 的上一层share[xuhaoyucentos7 share]$pwd#/usr/share# 从 /usr/share/doc/at-spi2-core-2.28.0/ 爬到 /etc/yum[xuhaoyucentos7 ~]$cd/usr/share/doc/at-spi2-core-2.28.0/#相当于从深层目录一路“爬楼梯”回到根再下去[xuhaoyucentos7 share]$cd../../../../etc/yum# 解读.. x4 回到 /usr再 /etc/yum[xuhaoyucentos7 yum]$pwd/etc/yum三、ls 命令补充 —— “看文件的十八般武艺”ls 默认按文件名“字母顺序”正向排序-l 选项长列表格式显示文件的详细信息-d 选项只查看目录本身不查看目录里面的内容# -1数字1选项单列显示一行一个# 适合在图形界面或想看清楚每个文件时用[xuhaoyucentos7 ~]$ls-1#公共#模板#视频#... ...# -R 选项递归查看把子目录里的东西也全部挖出来[xuhaoyucentos7 ~]$ls-R/etc/yum#/etc/yum:#fssnap.d pluginconf.d protected.d vars version-groups.conf#/etc/yum/fssnap.d: # 空目录里面啥也没有#/etc/yum/pluginconf.d: # pluginconf.d 里面有这两个文件#fastestmirror.conf langpacks.conf#/etc/yum/protected.d:#systemd.conf#/etc/yum/vars:#contentdir infra-l选项长列表格式显示文件的详细信息# 输出权限 硬链接数 所有者 所属组 大小 修改时间 文件名[xuhaoyucentos7 ~]$ls-l/etc/hosts#-rw-r--r--. 1 root root 197 11月 3 14:35 /etc/hosts# 通俗解读这是个普通文件-所有者root可读写rw-其他人只读r--# 大小197字节最后修改于11月3日14:35ls默认按文件名“字母顺序”正向排序[xuhaoyucentos7 ~]$ls-l#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 11月 3 17:18 abc#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 11月 3 17:17 file-1#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 11月 3 17:18 file-2#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 11月 3 17:18 hello# 按名字 abc → file-1 → file-2 → hello-r选项反向排序reverse[xuhaoyucentos7 ~]$ls-rl#hello → file-2 → file-1 → abc倒过来了-t选项按修改时间排序最新的排最前面[xuhaoyucentos7 ~]$ls-tl#hello最新修改的排第一#abc#file-2#file-1最老-d选项只查看目录本身不查看目录里面的内容[xuhaoyucentos7 ~]$ls-ld/etc/yum#drwxr-xr-x. 6 root root 100 12月 13 10:42 /etc/yum# d 开头表示它是一个目录而不是把里面的东西全列出来四、tree 命令 —— “以树形图看目录结构”[xuhaoyucentos7 ~]$ tree /etc/yum#/etc/yum#├── fssnap.d # 空目录#├── pluginconf.d # 子目录#│ ├── fastestmirror.conf#│ └── langpacks.conf#├── protected.d#│ └── systemd.conf#├── vars#│ ├── contentdir#│ └── infra#└── version-groups.conf五、stat 命令 —— “查看文件的身份证信息”# stat status查看文件的“元数据”文件本身的属性不是文件内容[xuhaoyucentos7 ~]$stat/etc/fstab# 文件/etc/fstab#大小541 块8 IO 块4096 普通文件#设备fd00h/64768d Inode67160130 硬链接1#权限(0644/-rw-r--r--) Uid( 0/ root) Gid( 0/ root)#环境system_u:object_r:etc_t:s0#最近访问2022-12-14 11:09:49 # 最后一次“看”这个文件的时间#最近更改2022-12-13 10:38:44 # 最后一次“改内容”的时间#最近改动2022-12-13 10:53:38 # 最后一次“改权限/所有者”的时间#创建时间-# 通俗说stat 能看到这个文件的“出生信息”和“三份时间戳”3、命令行管理文件一、touch 命令 —— “摸一下更新打卡时间”作用更新文件的时间戳为“现在”​ 如果文件不存在默认会创建一个新文件除非加 -c 选项如更新现有文件的时间戳[xuhaoyucentos7 ~]$ls-l111.txt#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 12月 14 10:51 zhang.txt # 旧时间是10:51[xuhaoyucentos7 ~]$touch111.txt# “摸一下”[xuhaoyucentos7 ~]$ls-l111.txt#-rw-rw-r--. 1 xuhaoyu xuhaoyu 0 12月 14 13:36 111.txt # 时间更新为13:36了文件不存在创建空文件mkl[xuhaoyucentos7 ~]$touchsong1.mp3[xuhaoyucentos7 ~]$lssong1.mp3#song1.mp3 # 新文件诞生了二、命令行管理文件 —— “增删改查总览表”操作单一来源多个来源大白话复制文件cp file1 file2cp file1 file2 file3 dir/抄送一份移动文件mv file1 file2mv file1 file2 file3 dir/搬走或改名删除文件rm file1rm file1 file2 file3扔进垃圾桶真删了创建目录mkdir dirmkdir -p dir1/dir2/dir3新建文件夹多层用-p复制目录cp -r dir1 dir2cp -r dir1 dir2 dir3 dir4/连锅端复制移动目录mv dir1 dir2mv dir1 dir2 dir3 dir4/连锅端搬走删除目录rm -r dir1或rmdir dir1rm -rf dir1 dir2 dir3连锅端删掉小心三、mkdir —— “新建文件夹”1、创建一个目录[xuhaoyucentos7 ~]$mkdirlab[xuhaoyucentos7 ~]$ls#lab ... ...2、一次创建多个目录[xuhaoyucentos7 ~]$mkdirlab1 lab23、创建上下级目录运用 -p 选项[xuhaoyucentos7 ~]$mkdirdir1/dir2/dir3/dir4#mkdir: 无法创建目录dir1/dir2/dir3/dir4: 没有那个文件或目录 # 报错因为dir1不存在[xuhaoyucentos7 ~]$mkdir-pdir1/dir2/dir3/dir4# 加-p一路自动创建[xuhaoyucentos7 ~]$ tree dir1#dir1#└── dir2#└── dir3#└── dir4四、cp —— “复制文件/目录”复制文件先进入再复制1、复制单个文件到目标位置( . 当前位置不加”点“的话无法复制)# 复制单个文件到目标位置[xuhaoyucentos7 ~]$cdlab[xuhaoyucentos7 lab]$ls[xuhaoyucentos7 lab]$cp/etc/hosts.[xuhaoyucentos7 lab]$lshosts[xuhaoyucentos7 lab]$cp/etc/hosts.# 解读把 /etc/hosts 复制到我当前站的这个位置#以上是先进入lab中这样写 如若不进入可这样写[xuhaoyucentos7 ~]$cp/etc/hostsdir#把 hosts 复制到 dir 目录2、 复制过来并且重命名[xuhaoyucentos7 lab]$cp/etc/hosts ./hosts-1# 解读复制过来顺便改名叫 hosts-1[xuhaoyucentos7 ~]$cp/etc/hosts dir/hosts-1#把 /etc/hosts 复制到当前 dir 目录文件重命名为 hosts-1。3、通过 -i 选项提示是否覆盖当前目录下有同名称文件不会提示直接覆盖[xuhaoyucentos7 lab]$cp-i/etc/hosts ./hosts-1#cp是否覆盖./hosts-1 yes# 问你同不同意防止手误覆盖4、复制多个文件目标必须是目录# 复制多个文件 → 目标必须是目录[xuhaoyucentos7 lab]$cp/etc/passwd /etc/host.allow.# 解读把 passwd 和 host.allow 两个文件都复制到当前目录.5、若复制目录则需要运用 -r 选项其他同样过程# cp 复制目录的核心注意点 # 1. 复制目录必须加 -rrecursive否则报错[xuhaoyucentos7 lab]$cp/etc/yum.cp: 略过目录/etc/yum# 不加 -r 就会“略过目录”[xuhaoyucentos7 lab]$cp-r/etc/yum.# 加 -r 才能复制[xuhaoyucentos7 lab]$lshosts.allow hosts hosts-1passwdyum# 2. 复制目录并重命名目标名字不存在时直接创建新目录[xuhaoyucentos7 lab]$cp-r/etc/yum ./yum-1[xuhaoyucentos7 lab]$lshosts.allow hosts hosts-1passwdyum yum-1# 新目录 yum-1# 3. ⚠️ 大坑目标目录已存在时不是覆盖而是把源目录“塞进去”[xuhaoyucentos7 lab]$lsyum fssnap.d pluginconf.d protected.d vars version-groups.conf[xuhaoyucentos7 lab]$cp-r/etc/yum ./yum# 目标 yum 已存在[xuhaoyucentos7 lab]$lsyum fssnap.d pluginconf.d protected.d vars version-groups.conf yum# 里面又多了一个 yum# 解读相当于把源目录 /etc/yum 整个复制到 ./yum/ 下变成 ./yum/yum/# 4. 复制多个目录时目标必须是已经存在的目录[xuhaoyucentos7 lab]$cp-r/etc /home ./mydir cp: 目标./mydir不是目录# 因为 mydir 不存在不能作为目标[xuhaoyucentos7 lab]$cp-r/etc /home.# 目标为 .当前目录是存在的可以...... cp: 无法打开/etc/sudo.conf读取数据: 权限不够# 有些文件普通用户无权读正常cp: 无法访问/etc/sudoers.d:权限不够......# 以上权限错误可忽略普通用户复制 /etc 下部分文件会无权限[xuhaoyucentos7 lab]$lsetc home hosts.allow hosts hosts-1passwdyum yum-1[xuhaoyucentos7 lab]$lsetc home# 查看复制来的 etc 和 home 目录内容五、mv —— “移动/重命名”1、我们使用前面创建的lab1目录运用ls -d只输出文件名[xuhaoyucentos7 lab]$mvhosts-1../lab12、移动单个文件[xuhaoyucentos7 lab]$mvhosts-1../lab1# 解读把 hosts-1 搬到上一级目录的 lab1 文件夹里3、 移动多个文件目标位置只能是目录[xuhaoyucentos7 lab]$mvpasswdhosts /home/xuhaoyu/lab1/# 解读把 passwd 和 hosts 都搬到 lab1 去4、重命名[xuhaoyucentos7 lab]$mvhost.allow host.allow-new# 解读在同一目录下把 host.allow 改名叫 host.allow-new# 通俗说mv 就是“改地址”如果在同一个文件夹就是“改名”六、rmdir —— “删除空目录”# rmdir remove directory只能删空目录[xuhaoyucentos7 lab]$rmdir../lab2# lab2 是空的可以删[xuhaoyucentos7 lab1]$rmdiretc rmdir: 删除etc失败: 目录非空# etc 里面有东西不让删# 结论rmdir 只删“空房子”有家具的屋子不让用这个命令七、rm —— “删除”# rm remove删除文件或目录# ⚠️ 警告Linux删除 直接消失不像Windows有回收站# 删除文件[xuhaoyucentos7 lab1]$rmhosts# 删掉hosts文件[xuhaoyucentos7 lab1]$rmhosts-1passwd# 一次删多个文件# 强制删除-f 不询问[xuhaoyucentos7 lab1]$cp/etc/pki/ca-trust/extracted/java/cacerts.[xuhaoyucentos7 lab1]$rmcacerts#rm是否删除有写保护的普通文件 cacertsyes # 系统会问[xuhaoyucentos7 lab1]$rm-fcacerts# 加-f直接删啥也不问# 删除目录必须加 -r[xuhaoyucentos7 lab1]$rm-ryum# 递归删除 yum 目录及其所有内容# 递归 强制 rm -rf[xuhaoyucentos7 lab1]$rm-fretc# 强删 etc 目录啥也不问# 这就是传说中的“删库跑路”组合拳用前千万确认路径# 删除多个目录[xuhaoyucentos7 lab1]$rm-frhome/ yum-1/# 清空实验环境[xuhaoyucentos7 ~]$rm-frlab lab1 lab2 dir1# 删除所有实验目录4、软连接与硬连接一、软连接符号链接—— Linux里的“快捷方式”软连接 一个指向别人的“快捷方式”文件。它本身不存数据只存了目标文件的路径。如果目标被删了这个快捷方式就“坏掉”变红闪烁。# 软连接符号链接完整演示 # 目标演示软连接就像Windows的“快捷方式”指向另一个文件或目录# 1. 查看系统自带的软连接/bin 指向 /usr/bin[xuhaoyucentos7 ~]$ls-dl/bin#lrwxrwxrwx. 1 root root 7 11月 2 17:32 /bin - usr/bin# 输出解读lrwxrwxrwx 中的 l 表示这是一个软连接箭头指向 usr/bin# 2. 创建软连接给 /var/tmp/ 目录创建一个名叫 mytmp 的快捷方式[xuhaoyucentos7 ~]$ln-s/var/tmp/ mytmp# -s 表示 symbolic符号链接# 3. 查看创建的软连接[xuhaoyucentos7 ~]$ls-lmytmp#lrwxrwxrwx. 1 xuhaoyu xuhaoyu 9 11月 4 11:40 mytmp - /var/tmp/# 解读mytmp 指向 /var/tmp/本身只存了路径字符串# 4. 访问软连接ls mytmp 等价于 ls /var/tmp/[xuhaoyucentos7 ~]$lsmytmp#abrt#systemd-private-...省略# 输出内容和直接 ls /var/tmp/ 完全一样# 5. 通过软连接操作文件实际影响目标目录[xuhaoyucentos7 ~]$touchmytmp/myfile# 在 mytmp即 /var/tmp下创建空文件 myfile# 验证真实目录 /var/tmp/ 下也出现了 myfile[xuhaoyucentos7 ~]$ls/var/tmp/myfile#/var/tmp/myfile # 文件存在说明软连接操作的是目标# 6. 错误示范软连接指向自身. 表示当前目录[xuhaoyucentos7 ~]$cd/home/xuhaoyu[xuhaoyucentos7 ~]$ln-s.tmp1# 创建指向当前目录的软连接[xuhaoyucentos7 ~]$ls-ltmp1#lrwxrwxrwx. 1 xuhaoyu xuhaoyu 1 11月 4 11:43 tmp1 - .# 解读tmp1 指向 .即 /home/xuhaoyu/访问 tmp1 会看到家目录的内容[xuhaoyucentos7 ~]$lstmp1#mytmp tmp1 公共 模板 视频 图片 文档 下载 音乐 桌面# 实际上等价于 ls /home/xuhaoyu/形成了循环指向一般不这样用二、硬连接 —— 同一个数据多个名字硬连接 同一个文件数据块的多个门牌号。每个硬连接都平等没有“原文件”和“快捷方式”之分。删掉任何一个硬连接数据不会丢失只要还有一个硬连接存在数据就在。修改任何一个硬连接的内容其他所有硬连接都会同步变化因为它们指向同一块数据。# 硬连接完整演示 # 目标演示硬连接是同一个数据块的多个名字修改任一所有同步# 1. 准备测试文件复制 /etc/hosts 到当前目录lab 目录下[xuhaoyucentos7 ~]$mkdir-plab[xuhaoyucentos7 ~]$cdlab[xuhaoyucentos7 lab]$cp/etc/hosts ./hosts-1# 2. 创建硬连接ln不加 -s[xuhaoyucentos7 lab]$lnhosts-1 hosts-2# 此时 hosts-1 和 hosts-2 指向同一份数据# 3. 查看两个文件的信息注意第三列“硬链接数”变为 2[xuhaoyucentos7 lab]$ls-lhosts-1 hosts-2 -rw-r--r--.2xuhaoyu xuhaoyu15811月413:34 hosts-1 -rw-r--r--.2xuhaoyu xuhaoyu15811月413:34 hosts-2# 第三列的 2 表示该数据块有两个名字引用# 4. 验证它们拥有相同的 inode身份证号[xuhaoyucentos7 lab]$ls-i1hosts-1 hosts-233554556hosts-133554556hosts-2# inode 相同证明指向同一个数据块# 5. 修改其中一个文件更新时间和内容# 5.1 更新 hosts-1 的时间戳[xuhaoyucentos7 lab]$touchhosts-1[xuhaoyucentos7 lab]$ls-lhosts-1 hosts-2 -rw-r--r--.2xuhaoyu xuhaoyu15811月413:36 hosts-1 -rw-r--r--.2xuhaoyu xuhaoyu15811月413:36 hosts-2# 两个文件的时间同步更新# 5.2 向 hosts-1 追加一行内容[xuhaoyucentos7 lab]$echohello worldhosts-1# 6. 验证 hosts-2 的内容也同步变化[xuhaoyucentos7 lab]$cathosts-1127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 hello world[xuhaoyucentos7 lab]$cathosts-2127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 hello world# 修改 hosts-1hosts-2 自动出现相同内容证明硬连接共享数据# 7. 额外说明删除一个硬连接不影响数据[xuhaoyucentos7 lab]$rmhosts-1[xuhaoyucentos7 lab]$cathosts-2127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 hello world# 数据依然存在因为 hosts-2 还在引用该数据块5、shell扩展匹配文件模式含义大白话示例匹配什么*“任意长度的任意字符”不包括开头的.星号可不占一个位置*.txt→ 匹配所有以.txt结尾的文件file*→ 匹配以file开头的所有文件*→ 匹配所有非隐藏文件不匹配.bashrc?“任意一个字符”必须占一个位置file?.txt→ 匹配file1.txt、fileA.txt但不匹配file12.txt因为?只能匹配一个字符[abc…]“方括号中列出的任意一个字符”单选file[123].txt→ 匹配file1.txt、file2.txt、file3.txt[aeiou]*→ 匹配以元音字母开头的文件[^abc…]或[!abc…]“不在方括号中的任意一个字符”排除file[^0-9].txt→ 匹配fileA.txt但不匹配file1.txt因为数字被排除了~“当前用户的家目录**”**cd ~→ 回到/home/xuhaoyuls ~/Documents→ 查看自己家目录下的 Documents 文件夹~username“指定用户的家目录”前提是有权限ls ~root→ 查看 root 的家目录/rootls ~xuhaoyu→ 查看用户 xuhaoyu 的家目录[[:alpha:]]“任意一个字母”不分大小写[[:alpha:]]*.txt→ 匹配以字母开头的.txt文件如a.txt、Z.txt[[:lower:]]“任意一个小写字母”[[:lower:]]→ 等价于[a-z][[:upper:]]“任意一个大写字母”[[:upper:]]→ 等价于[A-Z][[:alnum:]]“任意一个字母或数字”[[:alnum:]]→ 等价于[a-zA-Z0-9][[:punct:]]“任意一个标点符号”不包括空格和字母数字匹配!、,、.、?、等[[:digit:]]或[0-9]“任意一个数字”0-9file[[:digit:]].txt→ 匹配file1.txt、file9.txt[[:space:]]“任意一个空白字符”空格、Tab、换行等用于匹配带空格的奇怪文件名但不常用因为一般不用空格命名文件{...}“枚举多个字符串”花括号展开不是匹配文件而是生成多个字符串echo {a,b,c}.txt→ 输出a.txt b.txt c.txtmkdir -p /project/{src,bin,doc}→ 一次性创建三个目录→ 等价于[a-zA-Z0-9][[:punct:]]“任意一个标点符号”不包括空格和字母数字匹配!、,、.、?、等[[:digit:]]或[0-9]“任意一个数字”0-9file[[:digit:]].txt→ 匹配file1.txt、file9.txt[[:space:]]“任意一个空白字符”空格、Tab、换行等用于匹配带空格的奇怪文件名但不常用因为一般不用空格命名文件{...}“枚举多个字符串”花括号展开不是匹配文件而是生成多个字符串echo {a,b,c}.txt→ 输出a.txt b.txt c.txtmkdir -p /project/{src,bin,doc}→ 一次性创建三个目录