news 2026/8/2 3:07:47

Linux的文件管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux的文件管理

1、文件类型

- 新建、删除(touch、rm) 查看(cat、head、tail、less,grep,sed,awk) 编辑(vim,echo >,>>) d 新建、删除(mkdir -p,rm -r) 查看(ls -l,-d,-a,-h) 编辑 ​ l:软链接,符号链接 新建、删除(ln -s) ​ 硬链接:不是文件类型 b c s p
cd pwd

2、文件权限

用户操作文件的时候,要先看用户属于文件的哪一类人【文件的所属者、文件的所属组、其他人】

普通权限:

r

w

x

目录文件某一类用户可以拥有的权限: 没有权限:---,0 只读:r-x,5 读写:rwx,7 普通文件对应的权限: 没有权限:---,0 只读:r--,4 读写:rw-,6 执行:r-x,5 读写执行:rwx,7

特殊权限:

suid:给命令的程序文件u+s权限,任何人执行命令的时候,该进程的所属者是程序文件的所属者,rwsrwxrwx,4777

sgid:给目录g+s权限,任何人在该目录下创建的文件的所属组是该目录的所属组,rwxrwsrwx,2777

sticky:给目录o+t权限,任何普通用户不能删除别人的文件,只能删除自己的文件,rwxrwxrrwt,1777

扩展权限:

facl

setfacl

getfacl

3、文件输入、文件输出

3.1 输入输出类型

标准输入:有些命令需要从键盘上接收输入,例如cat;<重定向让命令接收文件内容作为输入,<<指定输入结束符

标准输出:是命令执行成功后的输出,>,>>重定向到/dir/path_file

标准错误输出:是命令执行失败后的输出,2>重定向到/dev/null

3.2 对普通文件的输入和输出

echo

重定向、追加重定向:>,>>

输入重定向:<,<<

vim:

编辑模式:跳转光标,复制,粘贴

插入模式:i,a

末行模式:保存文件

cat

head

tail

less

4、文件的压缩与解压缩

文件压缩/打包扩展名压缩/打包解压缩/解包
.zipzipunzip
.gzgzipgunzip
.bz2bzip2bunzip2
.xzxzunxz
.tar.gztar -czvftar -xvf
.tar.bz2tar -cjvftar -xvf
.tar.xztar -cJvftar -xvf

5、文件的传输

5.1 cp,mv

5.2 scp

#从rhel93传输文件到rhel79 [root@rhel93 ~]# scp test.sh root@192.168.168.198:/root #从rhel93下载文件到rhel79 [root@rhel79 ~]# scp root@192.168.168.197:/test/haha .

配置ssh免密登录:

#配置rhel93免密登录rhel79 [root@rhel93 ~]# ssh-keygen -f /root/.ssh/id_rsa -N "" [root@rhel93 ~]# ssh-copy-id root@rhel79

5.3 rsync

5.3.1 rsync 概念

rsync(remote sync)是linux系统下的数据镜像备份工具。是一个可实现全量及增量的本地或远程数据镜像备份的优秀工具。

rsync的增量同步是只同步发生变化的数据,因此数据传输效率很高,rsync有三大传输方式:

1、主机本地间的数据传输(类似于cp)

2、借助ssh等通道来传输数据(类似于scp)

3、以守护进程(socket)的方式传送数据

rsync的常用选项:

1、支持拷贝特殊文件如链接、设备等;【-l 保持软连接;-H 保留源文件的硬链接文件;-r 递归模式,包含目录及子目录;-D 保留设备文件和一些特殊文件】 2、可以排除指定文件或目录同步的功能,相当于打包命令tar的排除功能【--exclude=PATTERN 指定排除不需要传输的文件】 3、可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变;【-a 归档模式,表示以递归方式传输时,保持所有文件属性】 4、可以使用rcp、rsh、ssh等方式来配合传输文件; 5、可以通过socket(进程方式)传输文件和数据; 6、可支持匿名的或认证(无需系统用户)的进程模式传输,可方便安全的进行数据备份及镜像。

5.3.2 本地传输模式

命令语法:rsync [OPTION...] SRC... [DEST]

(1)把系统的host文件同步到/opt目录

[root@rhel93 ~]# rsync /etc/hosts /opt [root@rhel93 ~]# ll /opt/ 总用量 4 -rw-r--r--. 1 root root 158 8月 27 16:39 hosts #增量内容使用rsync进行传输 [root@rhel93 ~]# echo "192.168.168.197 rhel93">> /etc/hosts [root@rhel93 ~]# rsync /etc/hosts /opt

(2)把opt目录拷贝到/mnt目录

[root@rhel93 ~]# mkdir -p /opt/test1/test11/ [root@rhel93 ~]# echo test111 >> /opt/test1/test11/file #当opt后面写了/时,表示传输该目录下的文件 [root@rhel93 ~]# rsync -avz /opt/ /mnt sending incremental file list ./ hosts test1/ test1/test11/ test1/test11/file ​ sent 360 bytes received 69 bytes 858.00 bytes/sec total size is 189 speedup is 0.44 [root@rhel93 ~]# tree /mnt /mnt ├── hosts └── test1 └── test11 └── file ######以下为说明###### 说明:-a,归档模式,表示以递归方式传输文件,并保持文件所有属性 -v,详细模式输出 -z,对备份的文件在传输时先压缩 ######说明结束###### ​ #当要传输的opt目录后面没有写/时,表示传输该目录 [root@rhel93 ~]# rsync -avz /opt /mnt [root@rhel93 ~]# tree /mnt /mnt ├── hosts ├── opt │ ├── hosts │ └── test1 │ └── test11 │ └── file └── test1 └── test11 └── file ​

5.3.3 远程shell进行数据传输

命令语法:

Access via remote shell: ​ Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

(1)将本地文件推送到远程主机

[root@rhel93 ~]# rsync -avz /etc/hosts root@192.168.168.198:/root The authenticity of host '192.168.168.198 (192.168.168.198)' can't be established. ED25519 key fingerprint is SHA256:GVgC0haib/uMP+FIfkoDLj7/7TUfhlCgL9LN1C6eS8k. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes root@192.168.168.198's password: ​ #查看目标主机上是否已收到文件 [root@rhel93 ~]# ssh root@192.168.168.198 root@192.168.168.198's password: [root@rhel79 ~]# ll /root/hosts -rw-r--r--. 1 root root 181 8月 27 16:40 /root/hosts ​

rsync -avz /etc/hosts root@192.168.168.198:/root该命令等同于使用以下命令:

rsync -avz -e "ssh -p 22" /etc/hosts root@192.168.168.198:/root

(2)将远程主机的文件下载到本地

#192.168.168.198 主机上新建文件 [root@rhel79 ~]# echo "192.168.168.198 rhel79" >> /root/rhel79 [root@rhel93 ~]# rsync -avz root@192.168.168.198:/root/rhel79 /root root@192.168.168.198's password: [root@rhel93 ~]# cat /root/rhel79 192.168.168.198 rhel79

rsync -avz root@192.168.168.198:/root/rhel79 /root该命令等同于使用以下命令:

rsync -avz -e "ssh -p 22" root@192.168.168.198:/root/rhel79 /root

5.3.4 使用守护进程的方式传输数据

命令语法:

Access via rsync daemon: ​ Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] ​ Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

安装软件包,并配置服务:

[root@rhel93 yum.repos.d]# yum install rsync-daemon -y [root@rhel93 ~]# vim /etc/rsyncd.conf uid = rsync #指定传输文件所使用的用户,默认都是nobody gid = rsync #指定传输文件所使用的组,默认都是nobody use chroot = no max connections = 20 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [data] comment = It's my test data! path = /rsync_data ignore errors read only = false list = false hosts allow = 192.168.168.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup #rsync认证的账号 secrets file = /etc/rsyncd.passwd ​ #创建共享文件目录并修改权限 [root@rhel93 ~]# mkdir -p /rsync_data [root@rhel93 ~]# useradd rsync -s /sbin/nologin -M [root@rhel93 ~]# chown rsync:rsync /rsync_data #配置用于rsync同步的密码文件 [root@rhel93 ~]# echo "rsync_backup:123456" > /etc/rsyncd.passwd [root@rhel93 ~]# chmod 600 /etc/rsyncd.passwd #关闭防火墙和selinux [root@rhel93 ~]# systemctl disable --now firewalld [root@rhel93 ~]# setenforce 0 [root@rhel93 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config ​ [root@rhel93 ~]# systemctl start rsyncd [root@rhel93 ~]# ss -lntup | grep sync tcp LISTEN 0 5 0.0.0.0:873 0.0.0.0:* users:(("rsync",pid=24324,fd=5)) tcp LISTEN 0 5 [::]:873 [::]:* users:(("rsync",pid=24324,fd=6)) ​

客户端访问:

[root@rhel79 ~]# rsync -avz /root/file rsync_backup@192.168.168.197::data Password: #服务端可查看到文件已经成功上传 [root@rhel93 ~]# ll /rsync_data/ 总用量 4 -rw-r--r--. 1 rsync rsync 15 8月 27 17:37 file ​

客户端配置rsync账号密码文件:

[root@rhel79 ~]# echo 123456 > /etc/rsync.passwd [root@rhel79 ~]# chmod 600 /etc/rsync.passwd [root@rhel79 ~]# echo "test password" >> /root/file [root@rhel79 ~]# rsync -avz /root/file rsync_backup@192.168.168.197::data --password-file=/etc/rsync.passwd ​ #在服务端进行验证 [root@rhel93 ~]# cat /rsync_data/file this is rsyncd test password

排除某个文件进行同步上传:

#新建一个测试目录,注意不要在用户家目录下同步 [root@rhel79 ~]# mkdir /test [root@rhel79 ~]# cd /test [root@rhel79 ~]# touch anaconda-ks.cfg hosts .test [root@rhel79 test]# rsync -azv ./ --exclude={anaconda-ks.cfg,hosts,.*} rsync_backup@192.168.168.197::data --password-file=/etc/rsync.passwd ​ #也可以将要排除的文件写在某个文件中 [root@rhel79 test]# cat exclude anaconda-ks.cfg .* [root@rhel79 test]# rsync -azv ./ --exclude-from=exclude rsync_backup@192.168.168.197::data --password-file=/etc/rsync.passwd ​

服务端做配置排除某些文件:

[root@rhel93 rsync_data]# grep exclude /etc/rsyncd.conf # exclude = lost+found/ exclude = haha dir1 [root@rhel93 rsync_data]# systemctl restart rsyncd #客户端进行测试 [root@rhel79 test]# echo haha > haha [root@rhel79 test]# mkdir dir1 [root@rhel79 test]# echo dir1_file >> dir1/file [root@rhel79 test]# echo server_exclude >> server_exclude.test [root@rhel79 test]# rsync -azv ./ rsync_backup@192.168.168.197::data --password-file=/etc/rsync.passwd sending incremental file list ERROR: daemon refused to receive file "haha" ./ server_exclude.test ERROR: daemon refused to receive directory "dir1" *** Skipping any contents from this failed directory ***

5.3.5 定期同步文件

与传统的cp、scp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以满足对实时性要求不高的数据备份需求,例如使用crontab定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异。此时可以利用内核中的 inotify 监控指定目录,当目录中的文件或数据发生变化时,立即调用 rsync 服务将数据推送到远程主机上。

linux内核从2.6.13起,加入了Inotify支持。 Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,inotify-tools就是这样的一个第三方软件。

安装软件并简单使用:

#确认内核支持 [root@rhel93 ~]# ll /proc/sys/fs/inotify/ 总用量 0 -rw-r--r--. 1 root root 0 8月 27 18:03 max_queued_events -rw-r--r--. 1 root root 0 8月 27 18:03 max_user_instances -rw-r--r--. 1 root root 0 8月 27 18:03 max_user_watches #inotify-tools工具由epel仓库提供 [root@rhel93 ~]# cat /etc/yum.repos.d/epel.repo [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/9/Everything/x86_64/ gpgcheck=0 [root@rhel93 ~]# yum install -y inotify-tools

inotify-tools指令: inotifywait 用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录。 inotifywatch 用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。

Inotifywait是一个监控等待事件,可以配合shell脚本使用,常用的一些参数: -m, 即--monitor,表示始终保持事件监听状态 -r, 即--recursive,表示递归查询目录 -q, 即--quiet,表示打印出监控事件 --timefmt 指定日期显示格式 --format 指定输出信息格式 %w 显示被监控文件的路径 %f 显示具体发生改变的文件的文件名 %e 发生的事件类型 -e, 即--event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等

inotify-tools工具使用方式:

#监控目录/inotify-files/ [root@rhel93 ~]# inotifywait -mrq -e modify,delete,create,attrib /inotify-files/ ​ #另开一个终端在/inotify-files/下创建文件 [root@rhel93 ~]# cd /inotify-files/ [root@rhel93 inotify-files]# echo file > file [root@rhel93 inotify-files]# mkdir mulu1 #监控输出如下: [root@rhel93 ~]# inotifywait -mrq -e modify,delete,create,attrib /inotify-files/ /inotify-files/ CREATE file /inotify-files/ MODIFY file /inotify-files/ CREATE,ISDIR mulu1 ​ ​ #监控时加上时间信息 [root@rhel93 ~]# inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %e %w%f' -e modify,delete,create,attrib /inotify-files/ #再开一个终端进行文件调整 [root@rhel93 inotify-files]# echo test2 >> mulu1/test2 ​ #监控信息如下: [root@rhel93 ~]# inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %e %w%f' -e modify,delete,create,attrib /inotify-files/ 25/08/27 18:14 CREATE /inotify-files/mulu1/test2 25/08/27 18:14 MODIFY /inotify-files/mulu1/test2

配置数据自动同步至rsync服务端:

inotify-rsync服务器:192.168.168.197

inotify-rsync客户端:192.168.168.198

在服务端配置hosts自解析和ssh免密认证:

[root@rhel93 ~]# tail -1 /etc/hosts 192.168.168.198 rhel79 [root@rhel93 ~]# ssh-keygen -f /root/.ssh/id_rsa -N "" [root@rhel93 ~]# ssh-copy-id root@rhel79 [root@rhel93 ~]# yum install rsync inotify-tools -y [root@rhel93 ~]# mkdir /inotify_server_data/ [root@rhel93 ~]# cat /usr/local/bin/inotify_rsync.sh #!/bin/bash #注意src后面的目录文件一定要写结尾的/ src=/inotify_server_data/ dst=/inotify_client_data inotifywait -mrq -e modify,delete,create,attrib $src | while read file do #-X 保留扩展属性,包括selinux上下文 #-A 保留访问控制列表 #--delete删除目标端(destination)存在而源端(source)不存在的文件 rsync -avzXA --delete $src root@rhel79:$dst done [root@rhel93 ~]# chmod +x /usr/local/bin/inotify_rsync.sh [root@rhel93 ~]# cat /usr/lib/systemd/system/inotify-rsync.service [Unit] Description=inotify-rsync service ​ [Service] Type=simple ExecStart=/usr/local/bin/inotify_rsync.sh WorkingDirectory=/inotify_server_data/ Restart=on-failure ​ [Install] WantedBy=default.target [root@rhel93 ~]# systemctl daemon-reload [root@rhel93 ~]# systemctl start inotify-rsync.service

测试:

#服务端创建文件 [root@rhel93 inotify_server_data]# echo "this is inotify rsync" > file #客户端查看是否自动同步 [root@rhel79 ~]# cat /inotify_client_data/file this is inotify rsync
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/2 3:06:43

ESP32-S3-LCD-1.28开发板:一体化硬件与LVGL GUI开发全解析

1. 项目概述&#xff1a;ESP32-S3-LCD-1.28是什么&#xff1f;如果你最近在逛一些硬件开发者社区或者开源硬件平台&#xff0c;大概率会刷到“ESP32-S3-LCD-1.28”这个关键词。这玩意儿乍一看像是一串产品型号&#xff0c;但它背后代表的&#xff0c;其实是一类非常热门且实用的…

作者头像 李华
网站建设 2026/8/2 3:02:30

Grove可串联RGB LED模块全解析:从P9813驱动到Arduino/树莓派实战

1. 项目概述&#xff1a;从单点炫彩到无限延展的灯光艺术如果你玩过Arduino或者树莓派&#xff0c;大概率接触过RGB LED。那种通过红绿蓝三原色混合&#xff0c;能变幻出千万种色彩的小灯珠&#xff0c;是很多创客项目里营造氛围、传递信息的灵魂。但单个LED的玩法终究有限&…

作者头像 李华
网站建设 2026/8/2 3:00:12

基于OpenCV的围棋终局识别与胜负判定系统实现

1. 项目概述&#xff1a;当围棋遇上计算机视觉最近在整理一个业余围棋比赛的录像资料&#xff0c;发现手动统计终局胜负实在是个体力活&#xff0c;尤其是遇到那种盘面复杂、双方目数接近的对局&#xff0c;数一遍不放心还得数第二遍。这让我萌生了一个想法&#xff1a;能不能写…

作者头像 李华
网站建设 2026/8/2 2:59:50

ESPHome集成Espectre:在XIAO ESP32上构建嵌入式Web UI

1. 项目缘起&#xff1a;为什么要在XIAO ESP32上折腾Espectre&#xff1f;最近在捣鼓智能家居的本地化控制&#xff0c;想找一个既轻量又能跑在ESP32上的Web界面框架&#xff0c;用来做设备状态看板或者简单的控制面板。市面上常见的方案要么太重&#xff08;比如用MicroPython…

作者头像 李华
网站建设 2026/8/2 2:54:04

常用WordPress主题推荐 中文英文及小语种

WordPress作为全球最受欢迎的内容管理系统&#xff0c;占据了全球超过43%的网站市场份额。无论是个人博客、企业官网还是跨境电商独立站&#xff0c;选择一款合适的WordPress主题都是建站成功的关键一步。本文将为大家推荐几款常用的WordPress主题&#xff0c;分为中文主题和外…

作者头像 李华
网站建设 2026/8/2 2:45:23

Python爬虫与数据分析实战:从零基础到项目整合的完整学习路线

很多朋友在入门Python时&#xff0c;面对海量的教程和资料&#xff0c;常常感到无从下手&#xff0c;不知道如何规划学习路径&#xff0c;更不清楚如何将所学知识串联起来应用到实际项目中。本文旨在为你梳理一条从零基础到掌握Python核心应用&#xff08;爬虫与数据分析&#…

作者头像 李华