How to RSYNC a single file?

LinuxFileRsync

Linux Problem Overview


Currently i only RSync-ing the Directories as like:

* * * * * rsync -avz /var/www/public_html/images root@<remote-ip>:/var/www/public_html

So how do i rsync one single file like, /var/www/public_html/.htaccess ?

Linux Solutions


Solution 1 - Linux

You do it the same way as you would a directory, but you specify the full path to the filename as the source. In your example:

rsync -avz   --progress  /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/

As mentioned in the comments: since -a includes recurse, one little typo can make it kick off a full directory tree transfer, so a more fool-proof approach might to just use -vz, or replace it with -lptgoD.

Solution 2 - Linux

Basic syntax

rsync options source destination

Example

rsync -az /var/www/public_html/filename root@<remote-ip>:/var/www/public_html

Read more

Solution 3 - Linux

Michael Place's answer works great if, relative to the root directory for both the source and target, all of the directories in the file's path already exist.

But what if you want to sync a file with this source path:

/source-root/a/b/file

to a file with the following target path:

/target-root/a/b/file

and the directories a and b don't exist?

You need to run an rsync command like the following:

rsync -r --include="/a/" --include="/a/b/" --include="/a/b/file" --exclude="*" [source] [target]

Solution 4 - Linux

To date, two of the answers aren't quite right, they'll get more than one file, and the other isn't as simple as it could be, here's a simpler answer IMO.

The following gets exactly one file, but you have to create the dest directory with mkdir. This is probably the fastest option:

mkdir -p ./local/path/to/file
rsync user@remote:/remote/path/to/file/ -zarv --include "filename" --exclude "*" ./local/path/to/file/

If there is only one instance of file in /remote/path, rsync can create directories for you if you do the following. This will probably take a little more time because it searches more directories. Plus it's will create empty directories for directories in /remote/path that are not in ./local

cd ./local
rsync user@remote:/remote/path -zarv --include "*/" --include "filename" --exclude "*" .

Keep in mind that the order of --include and --exclude matters.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question夏期劇場View Question on Stackoverflow
Solution 1 - LinuxMichael PlaceView Answer on Stackoverflow
Solution 2 - LinuxTechieView Answer on Stackoverflow
Solution 3 - LinuxKenny EvittView Answer on Stackoverflow
Solution 4 - LinuxSamuelView Answer on Stackoverflow