Linux的文件管理

📅 2026/8/2 3:07:48
Linux的文件管理
1、文件类型- 新建、删除touch、rm 查看cat、head、tail、lessgrep,sed,awk 编辑vimecho , d 新建、删除mkdir -p,rm -r 查看ls -l,-d,-a,-h 编辑 ​ l软链接符号链接 新建、删除(ln -s) ​ 硬链接不是文件类型 b c s pcd pwd2、文件权限用户操作文件的时候要先看用户属于文件的哪一类人【文件的所属者、文件的所属组、其他人】普通权限rwx目录文件某一类用户可以拥有的权限 没有权限---0 只读r-x,5 读写rwx,7 普通文件对应的权限 没有权限---0 只读r--,4 读写rw-,6 执行r-x,5 读写执行rwx7特殊权限suid给命令的程序文件us权限任何人执行命令的时候该进程的所属者是程序文件的所属者rwsrwxrwx,4777sgid给目录gs权限任何人在该目录下创建的文件的所属组是该目录的所属组,rwxrwsrwx,2777sticky给目录ot权限任何普通用户不能删除别人的文件只能删除自己的文件,rwxrwxrrwt,1777扩展权限faclsetfaclgetfacl3、文件输入、文件输出3.1 输入输出类型标准输入有些命令需要从键盘上接收输入例如cat重定向让命令接收文件内容作为输入,指定输入结束符标准输出是命令执行成功后的输出,重定向到/dir/path_file标准错误输出是命令执行失败后的输出2重定向到/dev/null3.2 对普通文件的输入和输出echo重定向、追加重定向输入重定向vim编辑模式跳转光标复制粘贴插入模式ia末行模式保存文件catheadtailless4、文件的压缩与解压缩文件压缩/打包扩展名压缩/打包解压缩/解包.zipzipunzip.gzgzipgunzip.bz2bzip2bunzip2.xzxzunxz.tar.gztar -czvftar -xvf.tar.bz2tar -cjvftar -xvf.tar.xztar -cJvftar -xvf5、文件的传输5.1 cpmv5.2 scp#从rhel93传输文件到rhel79 [rootrhel93 ~]# scp test.sh root192.168.168.198:/root #从rhel93下载文件到rhel79 [rootrhel79 ~]# scp root192.168.168.197:/test/haha .配置ssh免密登录#配置rhel93免密登录rhel79 [rootrhel93 ~]# ssh-keygen -f /root/.ssh/id_rsa -N [rootrhel93 ~]# ssh-copy-id rootrhel795.3 rsync5.3.1 rsync 概念rsyncremote sync是linux系统下的数据镜像备份工具。是一个可实现全量及增量的本地或远程数据镜像备份的优秀工具。rsync的增量同步是只同步发生变化的数据因此数据传输效率很高rsync有三大传输方式1、主机本地间的数据传输类似于cp2、借助ssh等通道来传输数据类似于scp3、以守护进程socket的方式传送数据rsync的常用选项1、支持拷贝特殊文件如链接、设备等【-l 保持软连接-H 保留源文件的硬链接文件-r 递归模式包含目录及子目录-D 保留设备文件和一些特殊文件】 2、可以排除指定文件或目录同步的功能相当于打包命令tar的排除功能【--excludePATTERN 指定排除不需要传输的文件】 3、可以做到保持原文件或目录的权限、时间、软硬链接、属主、组等所有属性均不改变【-a 归档模式表示以递归方式传输时保持所有文件属性】 4、可以使用rcp、rsh、ssh等方式来配合传输文件 5、可以通过socket进程方式传输文件和数据 6、可支持匿名的或认证无需系统用户的进程模式传输可方便安全的进行数据备份及镜像。5.3.2 本地传输模式命令语法rsync [OPTION...] SRC... [DEST]1把系统的host文件同步到/opt目录[rootrhel93 ~]# rsync /etc/hosts /opt [rootrhel93 ~]# ll /opt/ 总用量 4 -rw-r--r--. 1 root root 158 8月 27 16:39 hosts #增量内容使用rsync进行传输 [rootrhel93 ~]# echo 192.168.168.197 rhel93 /etc/hosts [rootrhel93 ~]# rsync /etc/hosts /opt2把opt目录拷贝到/mnt目录[rootrhel93 ~]# mkdir -p /opt/test1/test11/ [rootrhel93 ~]# echo test111 /opt/test1/test11/file #当opt后面写了/时表示传输该目录下的文件 [rootrhel93 ~]# 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 [rootrhel93 ~]# tree /mnt /mnt ├── hosts └── test1 └── test11 └── file ######以下为说明###### 说明-a归档模式表示以递归方式传输文件并保持文件所有属性 -v详细模式输出 -z对备份的文件在传输时先压缩 ######说明结束###### ​ #当要传输的opt目录后面没有写/时表示传输该目录 [rootrhel93 ~]# rsync -avz /opt /mnt [rootrhel93 ~]# 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:DEST1将本地文件推送到远程主机[rootrhel93 ~]# rsync -avz /etc/hosts root192.168.168.198:/root The authenticity of host 192.168.168.198 (192.168.168.198) cant be established. ED25519 key fingerprint is SHA256:GVgC0haib/uMPFIfkoDLj7/7TUfhlCgL9LN1C6eS8k. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes root192.168.168.198s password: ​ #查看目标主机上是否已收到文件 [rootrhel93 ~]# ssh root192.168.168.198 root192.168.168.198s password: [rootrhel79 ~]# ll /root/hosts -rw-r--r--. 1 root root 181 8月 27 16:40 /root/hosts ​rsync -avz /etc/hosts root192.168.168.198:/root该命令等同于使用以下命令rsync -avz -e ssh -p 22 /etc/hosts root192.168.168.198:/root2将远程主机的文件下载到本地#192.168.168.198 主机上新建文件 [rootrhel79 ~]# echo 192.168.168.198 rhel79 /root/rhel79 [rootrhel93 ~]# rsync -avz root192.168.168.198:/root/rhel79 /root root192.168.168.198s password: [rootrhel93 ~]# cat /root/rhel79 192.168.168.198 rhel79rsync -avz root192.168.168.198:/root/rhel79 /root该命令等同于使用以下命令rsync -avz -e ssh -p 22 root192.168.168.198:/root/rhel79 /root5.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安装软件包并配置服务[rootrhel93 yum.repos.d]# yum install rsync-daemon -y [rootrhel93 ~]# 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 Its 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 ​ #创建共享文件目录并修改权限 [rootrhel93 ~]# mkdir -p /rsync_data [rootrhel93 ~]# useradd rsync -s /sbin/nologin -M [rootrhel93 ~]# chown rsync:rsync /rsync_data #配置用于rsync同步的密码文件 [rootrhel93 ~]# echo rsync_backup:123456 /etc/rsyncd.passwd [rootrhel93 ~]# chmod 600 /etc/rsyncd.passwd #关闭防火墙和selinux [rootrhel93 ~]# systemctl disable --now firewalld [rootrhel93 ~]# setenforce 0 [rootrhel93 ~]# sed -i s/^SELINUX.*/SELINUXdisabled/ /etc/selinux/config ​ [rootrhel93 ~]# systemctl start rsyncd [rootrhel93 ~]# ss -lntup | grep sync tcp LISTEN 0 5 0.0.0.0:873 0.0.0.0:* users:((rsync,pid24324,fd5)) tcp LISTEN 0 5 [::]:873 [::]:* users:((rsync,pid24324,fd6)) ​客户端访问[rootrhel79 ~]# rsync -avz /root/file rsync_backup192.168.168.197::data Password: #服务端可查看到文件已经成功上传 [rootrhel93 ~]# ll /rsync_data/ 总用量 4 -rw-r--r--. 1 rsync rsync 15 8月 27 17:37 file ​客户端配置rsync账号密码文件[rootrhel79 ~]# echo 123456 /etc/rsync.passwd [rootrhel79 ~]# chmod 600 /etc/rsync.passwd [rootrhel79 ~]# echo test password /root/file [rootrhel79 ~]# rsync -avz /root/file rsync_backup192.168.168.197::data --password-file/etc/rsync.passwd ​ #在服务端进行验证 [rootrhel93 ~]# cat /rsync_data/file this is rsyncd test password排除某个文件进行同步上传#新建一个测试目录注意不要在用户家目录下同步 [rootrhel79 ~]# mkdir /test [rootrhel79 ~]# cd /test [rootrhel79 ~]# touch anaconda-ks.cfg hosts .test [rootrhel79 test]# rsync -azv ./ --exclude{anaconda-ks.cfg,hosts,.*} rsync_backup192.168.168.197::data --password-file/etc/rsync.passwd ​ #也可以将要排除的文件写在某个文件中 [rootrhel79 test]# cat exclude anaconda-ks.cfg .* [rootrhel79 test]# rsync -azv ./ --exclude-fromexclude rsync_backup192.168.168.197::data --password-file/etc/rsync.passwd ​服务端做配置排除某些文件[rootrhel93 rsync_data]# grep exclude /etc/rsyncd.conf # exclude lostfound/ exclude haha dir1 [rootrhel93 rsync_data]# systemctl restart rsyncd #客户端进行测试 [rootrhel79 test]# echo haha haha [rootrhel79 test]# mkdir dir1 [rootrhel79 test]# echo dir1_file dir1/file [rootrhel79 test]# echo server_exclude server_exclude.test [rootrhel79 test]# rsync -azv ./ rsync_backup192.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就是这样的一个第三方软件。安装软件并简单使用#确认内核支持 [rootrhel93 ~]# 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仓库提供 [rootrhel93 ~]# cat /etc/yum.repos.d/epel.repo [epel] nameepel baseurlhttps://mirrors.aliyun.com/epel/9/Everything/x86_64/ gpgcheck0 [rootrhel93 ~]# yum install -y inotify-toolsinotify-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/ [rootrhel93 ~]# inotifywait -mrq -e modify,delete,create,attrib /inotify-files/ ​ #另开一个终端在/inotify-files/下创建文件 [rootrhel93 ~]# cd /inotify-files/ [rootrhel93 inotify-files]# echo file file [rootrhel93 inotify-files]# mkdir mulu1 #监控输出如下 [rootrhel93 ~]# inotifywait -mrq -e modify,delete,create,attrib /inotify-files/ /inotify-files/ CREATE file /inotify-files/ MODIFY file /inotify-files/ CREATE,ISDIR mulu1 ​ ​ #监控时加上时间信息 [rootrhel93 ~]# inotifywait -mrq --timefmt %y/%m/%d %H:%M --format %T %e %w%f -e modify,delete,create,attrib /inotify-files/ #再开一个终端进行文件调整 [rootrhel93 inotify-files]# echo test2 mulu1/test2 ​ #监控信息如下 [rootrhel93 ~]# 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.197inotify-rsync客户端192.168.168.198在服务端配置hosts自解析和ssh免密认证[rootrhel93 ~]# tail -1 /etc/hosts 192.168.168.198 rhel79 [rootrhel93 ~]# ssh-keygen -f /root/.ssh/id_rsa -N [rootrhel93 ~]# ssh-copy-id rootrhel79 [rootrhel93 ~]# yum install rsync inotify-tools -y [rootrhel93 ~]# mkdir /inotify_server_data/ [rootrhel93 ~]# 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 rootrhel79:$dst done [rootrhel93 ~]# chmod x /usr/local/bin/inotify_rsync.sh [rootrhel93 ~]# cat /usr/lib/systemd/system/inotify-rsync.service [Unit] Descriptioninotify-rsync service ​ [Service] Typesimple ExecStart/usr/local/bin/inotify_rsync.sh WorkingDirectory/inotify_server_data/ Restarton-failure ​ [Install] WantedBydefault.target [rootrhel93 ~]# systemctl daemon-reload [rootrhel93 ~]# systemctl start inotify-rsync.service测试#服务端创建文件 [rootrhel93 inotify_server_data]# echo this is inotify rsync file #客户端查看是否自动同步 [rootrhel79 ~]# cat /inotify_client_data/file this is inotify rsync