Linux下文件管理

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")}'
相关推荐
无足鸟ICT11 分钟前
【RHCA+】vmstat命令(监控系统资源)
linux·服务器
垆边人似月.18 分钟前
RDMA 文件传输踩坑(五):READY 发送成功后,为什么出现 Work Request Flushed Error?
服务器·网络·tcp/ip
闪亮的路灯22 分钟前
Linux 安装mqtt服务mosquitto
linux·运维·服务器
HideF35 分钟前
nmcli
linux·服务器·网络
qetfw1 小时前
UOS DHCP Relay:跨网段分配与 IP、DNS、网关验证
linux·服务器·网络·tcp/ip·uos
明航咨询-陈老师2 小时前
ITSS运行维护服务能力成熟度认证技术纵深:标准模型、等级路径、2026审核趋势与CS/CMMI对比
服务器·数据库·cmmi
szxinmai主板定制专家3 小时前
X86 工控机 vs RK3588 异构主控:AMR 控制器 BOM、散热、长期运维对比
运维·stm32·单片机·嵌入式硬件·fpga开发·am62x
缓慢更新3 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能
叼烟扛炮3 小时前
Linux 学习笔记(四十一):TCP 并发演进——从单进程到多线程服务器
linux·运维·服务器·网络·笔记·学习·tcp/ip
ynchyong3 小时前
Linux 有两个 “版本“
linux·发行版·内核版本