Linux下文件管理
1、常用命令
touch、mkdir、rm、cp、mv、cat、head、less、vim、file、wc、ls
2.二级目录的作用
在 Linux 系统中,二级目录 指的是挂载在根目录(/)下的第一层子目录(如 /usr、/var、/home 等),它们是 Linux 文件系统层级标准(FHS)的核心组成部分,作用是对系统资源和数据进行标准化分类管理,保障系统稳定、高效运行,同时提升文件查找和维护的便捷性。
功能分区,职责明确
每个二级目录都有严格的用途定义,避免文件混乱存放。
/bin:存放所有用户都能使用的基础命令(如ls、cp),系统启动时就会加载。/sbin:存放系统管理员专用的管理命令(如fdisk、ifconfig)。/usr:存放用户安装的应用程序、文档和共享资源,相当于系统的 "软件仓库"。/var:存放动态变化的数据 ,如日志文件(/var/log)、数据库文件、邮件缓存等。/home:普通用户的个人主目录,每个用户都有独立的子目录,用于存放个人文件和配置。/etc:存放系统和应用程序的配置文件 (如passwd、fstab),是系统的 "配置中心"。
提升系统稳定性与安全性
- 部分二级目录(如
/var、/home)可以单独挂载在独立的磁盘分区上。这样即使某个分区被写满或损坏,也不会影响根分区和系统核心目录的运行。 - 权限隔离:不同目录设置不同的访问权限,比如
/root(管理员主目录)仅 root 用户可访问,/bin允许所有用户执行,保障系统安全。
方便系统维护与备份
- 标准化的目录结构让管理员可以快速定位文件,比如日志一定在
/var/log,配置文件一定在/etc。 - 备份时可以针对性操作,比如只备份
/home(用户数据)和/etc(配置文件),无需备份/usr(可重装的软件)。
支持多用户与多环境
/home目录为多用户提供独立的存储空间,避免用户间文件互相干扰。/tmp目录用于存放系统和程序的临时文件,所有用户都可读写,且系统重启后会自动清空。
3.文件寻址 相对路径、绝对路径
在 Linux 文件系统中,绝对路径 和相对路径 是两种定位文件 / 目录的方式,核心区别在于寻址的起点不同。
绝对路径定义 :以根目录(/)为起点,描述文件或目录在系统中的完整位置,路径的第一个字符一定是 /。
无论当前处于哪个目录,通过绝对路径都能精准定位到目标文件 / 目录,具有唯一性。
相对路径定义 :以当前工作目录 为起点,描述目标文件 / 目录相对于当前位置的路径,路径的第一个字符不是 /。
相对路径的有效性依赖于当前所在目录,同一目标文件在不同工作目录下,相对路径的写法不同。
4、文件批处理
*、?、[[:alpha:]]、[[:aigit:]]、[[:lower:]]、[[:upper:]]、[[:alnum:]]、[[:punct:]]、[[:space:]]
[]、{}
bash
#实验
#说明:建议开启两个shell,在一个shell中开启监控
[root@localhost mnt]# watch -n 1 ls -Rl
#在另外一个shell中操作
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr lee
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]#
[root@localhost mnt]# rm -fr *
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr ???
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr ????
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le*e
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee file
[root@localhost mnt]# rm -fr le*e
[root@localhost mnt]# rm -fr *
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:digit:]]e
[root@localhost mnt]# touch le8e leAe le@e leae le\ e lee
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# touch le66e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:digit:]]e
[root@localhost mnt]# rm -fr le[[:digit:]][[:digit:]]e
[root@localhost mnt]# touch le66e leAe le@e leae le\ e lee
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:digit:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:punct:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:punct:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]#
[root@localhost mnt]# rm -fr le[[:punct:]]*e
[root@localhost mnt]# rm -fr le[[:space:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:alpha:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:upper:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:lower:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:alnum:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[[:punct:][:space:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[![:punct:][:space:]]*e
[root@localhost mnt]# touch le6e leAe le@e leae le\ e lee
[root@localhost mnt]# rm -fr le[^[:punct:][:space:]]*e
复制指定目录中符合指定条件的文件到指定为止并加入特定后缀
bash
#复制/etc/中以.conf 结尾的文件到/mnt/conf.bak/中并加入.bak后缀
[root@localhost mnt]# find /etc -type f -name "*.conf" -exec sh -c 'cp "$1" "/mnt/conf.bak/$(basename "$1").bak"' _ {} \;
或
[root@localhost mnt]# ls /etc | awk '/\.conf$/{system("cp -r /etc/"$0 " /mnt/conf.bak/"$0".bak")}'