Terminal mit der Eingabeaufforderung ubuntu@ubuntu und dem Befehl sudo

rsync: The Thing with the Trailing Slash

In the Unix and Linux universe, rsync is a really useful command line tool for copying or synchronizing directories on local or remote systems. While scp can also copy entire directories including their contents recursively, rsync has the big advantage that it can compare files by size and timestamp. That way, only what has changed gets copied, and with the –delete option, whatever no longer exists in the source directory gets deleted. So folders are synchronized, i.e. brought to the same state. The thing I keep tripping over: How did that work again with the trailing slash?

Slash at the end, only the content I send

With this little rhyme I try to remember that a trailing slash at the end of the source directory only copies the folder’s contents:

rsync -avz /lokales/quellverzeichnis/ /lokales/zielverzeichnis/

To a remote system, the command looks like this:

rsync -avz /lokales/quellverzeichnis/ benutzer@zielserver:/entferntes/zielverzeichnis/

To make it clearer, here’s a folder structure. Say the source directory contains two files:

/lokales/quellverzeichnis/
├── file1.txt
└── file2.txt

With the trailing slash on the source, only the two files end up directly in the target directory:

/lokales/zielverzeichnis/
├── file1.txt
└── file2.txt

No slash and rsync is mean, the source folder joins the scene

Unfortunately not quite as catchy, because the rhyme is longer, but a good addition to round off the mnemonic. “Mean” because most of the time you only want the folder’s contents to be copied and not have the directory created once more.

The following command creates the directory:

rsync -avz /lokales/quellverzeichnis /lokales/zielverzeichnis/

The result in the target directory then looks like this:

/lokales/zielverzeichnis/
└── quellverzeichnis/
    ├── file1.txt
    └── file2.txt

What does the trailing slash do on the target directory?

To keep it simple: Nothing relevant! You can ignore it. You don’t need it. It confuses more than it helps.

ChatGPT helped me come up with the rhymes, although the AI didn’t get there completely on its own. By the way, you can also practice your command line skills with ChatGPT, since the AI can simulate a Unix terminal. Here’s the prompt for it:

I want you to act as the terminal of an Ubuntu server. I will type commands and you will reply with what a Linux terminal would show. I want you to only reply with the terminal output inside one unique code block, and nothing else! Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something, I will do so by putting text inside curly brackets {like this}. Then you can answer normally. My first command is pwd.

Practicing on a real operating system is more fun, though, but also more work to set up. And every now and then ChatGPT doesn’t give you any output at all.

This post is also available in: Deutsch

Published by

Christopher Piontek

Technology fascinates me, and I've been writing about it on Bitpage.de since 2012. By day I'm a developer, and on the side I test software and tinker with old hardware. Here you'll find how-tos, hands-on reports and my take on things, sometimes at full nerd level, sometimes as a quick practical tip.

Leave a Reply

Your email address will not be published. Required fields are marked *