在Linux中,可以使用rsync
命令的--exclude
選項來排除特定文件或目錄
創建一個名為exclude_dynamic.sh
的shell腳本,內容如下:
#!/bin/bash
# 定義要排除的文件或目錄
exclude_file="file_to_exclude"
exclude_dir="directory_to_exclude"
# 使用rsync命令同步文件,并使用--exclude選項排除指定的文件或目錄
rsync -avz --exclude="$exclude_file" --exclude="$exclude_dir" source/ destination/
為腳本添加可執行權限:
chmod +x exclude_dynamic.sh
運行腳本:
./exclude_dynamic.sh
find
和rsync
命令結合:find source/ -maxdepth 1 -type f ! -name "file_to_exclude" ! -type d ! -name "directory_to_exclude" -exec rsync -avz {} destination/ \;
這個命令會在source
目錄下查找除指定文件和目錄之外的所有文件,并將它們同步到destination
目錄。
rsync
命令的--exclude-from
選項:創建一個名為exclude_list.txt
的文件,內容如下:
file_to_exclude
directory_to_exclude
使用rsync
命令同步文件,并使用--exclude-from
選項排除指定的文件或目錄:
rsync -avz --exclude-from=exclude_list.txt source/ destination/
當你需要動態修改排除列表時,只需編輯exclude_list.txt
文件即可。