中文版

📚 文件同步和流编辑工具:rsync 和 sed 的简化指南 📊

高效的文件管理对于现代开发者和系统管理员来说至关重要。无论是同步文件还是编辑文本流,rsync ✨ 和 sed ⚖️ 是必备工具。本指南将详细讲解它们的基础、用例以及实际示例,帮助你优化工作流。


🚀 rsync:掌握文件同步

🔎 什么是 rsync?

rsync 是一个快速且多功能的工具,用于在系统之间同步文件和目录。它支持增量更新、在传输过程中压缩数据,并保留文件元数据。

🔧 语法概述

1
rsync [options] source destination

🌟 常见用例

1. 基础同步

1
rsync -av /source/ /destination/
  • -a:归档模式(保留权限、时间戳等)。
  • -v:详细模式。

2. 远程文件传输

1
rsync -avz /local/ user@remote:/remote/
  • -z:在传输过程中压缩文件。

3. 过滤文件

1
rsync -av --exclude '*.log' --include '*.conf' /src/ /dest/
  • 排除日志文件,同时包括配置文件。

4. 目录镜像

1
rsync -av --delete /src/ /dest/
  • 删除目标目录中多余的文件。

5. 可恢复传输

1
rsync -av --partial /src/ /dest/
  • 恢复中断的文件传输。

🛠️ 高级功能

文件过滤

1
rsync -av --include-from=include.txt --exclude-from=exclude.txt /src/ /dest/
  • 从文件中加载包含/排除规则。

权限管理

1
rsync --chmod=a=rw,Da+x /src/ /dest/
  • 调整文件和目录权限。

完整命令示例

1
2
rsync -ruhi --include-from "include.txt" --exclude-from "exclude.txt" \
      --chmod=a=rw,Da+x /cygdrive/d/context/ user@192.168.1.25::context/
  • -r:递归复制。
  • -u:跳过目标中较新的文件。
  • -h:以人类可读的格式显示文件大小。
  • --chmod:设置特定权限。

🎨 sed:精确转换文本流

🔎 什么是 sed?

sed(流编辑器)是一个命令行工具,用于非交互式地编辑文件中的文本。无论是基本替换还是复杂的文本操作,sed 都不可或缺。

🔧 语法概述

1
sed [options] script [file]

🌟 常见用例

1. 简单替换

1
sed 's/old/new/' file.txt
  • 替换每行中首次出现的“old”为“new”。

2. 全局替换

1
sed 's/old/new/g' file.txt
  • 加上 g,替换每行中所有出现的“old”。

3. 直接编辑文件

1
sed -i 's/old/new/g' file.txt
  • 直接更新文件。

4. 删除行

1
sed '1,5d' file.txt
  • 删除第 1 到第 5 行。

5. 在匹配行前插入文本

1
2
sed '/pattern/i\
新文本' file.txt
  • 在匹配“pattern”的行前插入“新文本”。

🛠️ 高级选项

使用脚本

1
sed -f script.sed file.txt
  • 应用 script.sed 中定义的多个转换。

脚本示例

script.sed:

1
2
s/foo/bar/g
1,5d
  • 将“foo”替换为“bar”,并删除第 1 到 5 行。

组合命令

1
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
  • 链式应用多个转换。

🌐 总结

rsyncsed 是开发者和管理员优化工作流的必备工具。从高效的文件传输到精确的文本操作,它们能帮助你轻松应对复杂任务。

🔗 参考文档

英文版

📚 Efficient File Synchronization & Stream Editing with rsync & sed 📊

Managing files effectively is crucial for system administrators and developers. Two powerful tools, rsync ✨ and sed ⚖️, cater to file synchronization and stream editing needs, respectively. This blog provides a comprehensive guide to these tools, complete with examples and best practices.


🎮 rsync: High-Performance File Synchronization

🔄 Overview

rsync is a utility for efficiently transferring and synchronizing files ⚡ locally or across remote systems. It supports incremental file transfer and preserves metadata, making it indispensable for backups and mirroring tasks.

🔀 Basic Syntax

1
rsync [options] source destination

🎨 Common Use Cases & Examples

1. Basic Local File Synchronization

1
rsync -av /source/ /destination/
  • -a: Archive mode (preserves file structure, permissions, and metadata).
  • -v: Verbose mode for detailed output.

2. Remote File Transfer

1
rsync -avz /local/path/ user@remote:/remote/path/
  • -z: Compress files during transfer.

3. Include and Exclude Files

1
rsync -av --exclude '*.tmp' --include '*.txt' /src/ /dest/
  • Exclude all temporary files while including text files.

4. Mirror Directories

1
rsync -av --delete /src/ /dest/
  • Synchronize directories while deleting extraneous files in the destination.

5. Display Progress

1
rsync -avz --progress /src/ /dest/
  • Use --progress to monitor file transfer status.

6. Resume Interrupted Transfers

1
rsync -av --partial /src/ /dest/
  • Enable resumable file transfers for large files.

🌈 Advanced Options

File Filtering

  • Use --include-from and --exclude-from to apply filters from files:
    1
    
    rsync -av --include-from=include.txt --exclude-from=exclude.txt /src/ /dest/
    

Optimizing Performance

  • Compress files with -z.
  • Limit bandwidth with --bwlimit=KBPS.

Permissions Management

  • Adjust permissions with --chmod:
    1
    
    rsync --chmod=a=rw,Da+x /src/ /dest/
    

Command Example with Explanation

1
2
rsync -ruhi --include-from "include.txt" --exclude-from "exclude.txt" \
      --chmod=a=rw,Da+x /cygdrive/d/context/ user@192.168.1.25::context/
  • 🔄 -r: Recursive.
  • ➡️ -u: Skip files newer on the receiver.
  • 🔥 -h: Human-readable numbers.
  • --include-from: Include patterns from a file.
  • ✖️ --exclude-from: Exclude patterns from a file.

🎨 sed: Stream Editing Mastery

sed (Stream Editor) is a non-interactive text processing tool for editing files, substituting strings, and more.

🔀 Basic Syntax

1
sed [options] script [file]

🎮 Common Use Cases & Examples

1. Replace Text in a File

1
sed 's/old/new/' file.txt
  • Replaces the first occurrence of “old” with “new” in each line.

2. Replace Globally in Each Line

1
sed 's/old/new/g' file.txt
  • Adds g to replace all occurrences in each line.

3. Edit File In-Place

1
sed -i 's/old/new/g' file.txt
  • Updates the file directly.

4. Delete Specific Lines

1
sed '1,5d' file.txt
  • Deletes lines 1 to 5.

5. Insert Text Before a Line

1
2
sed '/pattern/i\
Inserted text' file.txt
  • Adds “Inserted text” before lines matching “pattern”.

🌈 Advanced Options

Using Scripts

  • Create reusable scripts with -f:
    1
    
    sed -f script.sed file.txt
    

Example Script File (script.sed)

1
2
s/foo/bar/g
1,5d
  • Replaces “foo” with “bar” globally and deletes lines 1-5.

Combining Commands

1
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
  • Applies multiple transformations.

🌐 Conclusion

By mastering rsync for file synchronization and sed for stream editing, system administrators and developers can efficiently manage files and automate repetitive tasks. These tools, when combined, offer unparalleled flexibility and performance in handling data.

🔗 Reference Documentation