From the course: Learning Rsync
Using rsync with directories - rsync Tutorial
From the course: Learning Rsync
Using rsync with directories
- [Instructor] Instead of just working with one file, let's work with a directory instead. I'll copy our website directory into the rsync work directory. I'll write rsync Website rsync-work, and well, that didn't work. By default, rsync ignores directories, so we need to tell it to use recursion to go inside directories and consider their contents. To do this, we can use the dash R option for recursive, and if we're using dash A for archive, that option also includes recursion among the various other modes and conditions that it sets. So if I write rsync-avhP Website rsync-work, now that worked. Here I can see that my website directory was copied into the rsync test directory. And with the progress mode enabled we can see not only the details of each transfer but this part here on the right begins to make more sense. XFR is the transfer number in the file list. To check is the number of files that the receiver still has yet to check, and the last number is the total number of files in the job. We can use these values to estimate how close a job is to being finished. If we're doing an incremental copy this will say IR check instead. If I recall my previous command and run it again, I can see that nothing was transferred. Because the files are the same in the source and the destination. Rsync compared each file in the source with a corresponding one in the destination and found that they had the same size and modification times so it didn't transfer anything. Let's edit a file and see what happens. I'll write VI Website/index.html. Here in my code editor, I'll make a small change. I'll change this dash to two colons, and then I'll save the file with escape, colon, WQ. I'll recall my rsync command and run it again. Now we see that one file was transferred because it's different on the source than it was on the destination. But the rest weren't transferred because they haven't changed since the last operation. When we're working with directories it's important to pay attention to slashes. This command, as I've been writing it, says to copy this directory into this other directory. So we ended up with a directory called Website inside the directory called rsync-work. If the destination directory doesn't exist, rsync will create it for us. And whether we put a slash on the end of the destination directory or not doesn't really matter. But it does matter if we put a slash on the end of the source. With no slash, rsync treats this directory as a directory, but with a slash on the end, rsync will move inside the directory before it starts copying, meaning that the contents of this directory, not the directory itself, will be copied to the destination folder. And that can create a bit of a mess if that's not what we want to have happen. To demonstrate this, I'll recall my previous command and put a slash at the end of the source and I'll run it. Then I'll take a look inside my rsync-work directory. Here's the original copy of the directory that I made earlier. But here, most of these files are the contents of the website directory because I used slash in the previous command. That might be the behavior you want, or it may not, but it's important to be aware of. I'll open up my file browser, and I'll delete these files and I'll clear the screen. Next, I'll delete a file from my source directory. Let's move inside Website and take a look, and let's delete the robot's file. I'll move back up to my working directory and recall my previous rsync command, making sure to get my slashes right this time. If I run this again, and take a look inside my destination directory, I'll see that the file that I deleted from the source remains on the destination. In a lot of cases, like backups, this is exactly what we want because it protects our files from accidental loss. But sometimes it's more important for the destination to exactly match the source. I can use the dash dash delete option to tell rsync to remove files from the destination that no longer exist on the source. I'll run this command, and notice that the file I deleted from the source was removed from the destination. Rsync provides many useful features for copying files locally. In that way, it can act as a powerful replacement for the regular CP or copy command. But it can do quite a bit more, and we'll explore that throughout the rest of the course.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.