【rsync】使用rsync备份文件/目录并排除特定文件/目录

本文详细介绍了如何在Linux环境下使用rsync命令,通过--exclude选项排除特定目录,包括相对路径的使用方法,以及源目录带/与不带/的区别。适合对rsync迁移目录时希望剔除部分文件的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 下复制(cp)目录时排除一个或者多个目录的方法

cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现。

# rsync --help

-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)

       --no-OPTION            turn off an implied OPTION (e.g. --no-D)

-r, --recursive            recurse into directories

-p, --perms preserve permissions

注意:--exclude后面的路径不能为绝对路径,必须为相对路径才可以,否则出错。

# ll /root/install_fio/

源目录结尾带 /,会将此目录下文件&目录拷贝到目标目录下,如果没有目标目录,会自动创建

# rsync -rap --exclude="repodata"    /root/install_fio/    /tmp/install_fio_exclude_repodata

# rsync -rap --exclude="repodata"    ./install_fio/    /tmp/install_fio_exclude_repodata

# ll  /tmp/install_fio_exclude_repodata

源目录结尾不带 /,会将此目录整个目录拷贝到目标目录下,如果没有目标目录,会自动创建

# rsync -rap --exclude="repodata"     /root/install_fio     /tmp/install_fio_exclude_repodata

# rsync -rap --exclude="repodata"   ./install_fio    /tmp/install_fio_exclude_repodata

# ll  /tmp/install_fio_exclude_repodata

参考 

How to exclude directories with certain names from rsync on Linux?

https://www.systutorials.com/how-to-exclude-directories-with-certain-names-from-rsync-on-linux

rsync 排除指定目录或文件进行同步

https://www.cnblogs.com/xiao-xue-di/p/13901756.html

rsync 排除指定目录

https://www.cnblogs.com/bigtreei/p/11763865.html

Linux下cp目录时排除一个或者多个目录的方法

https://www.osyunwei.com/archives/2626.html

https://blog.51cto.com/lxsym/831393

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值