How to RSYNC a single file?
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
.
How to rsync only a specific list of files?
Edit: atp's answer below is better. Please use that one!
You might have an easier time, if you're looking for a specific list of files, putting them directly on the command line instead:
# rsync -avP -e ssh `cat deploy/rsync_include.txt` root@0.0.0.0:/var/www/
This is assuming, however, that your list isn't so long that the command line length will be a problem and that the rsync_include.txt
file contains just real paths (i.e. no comments, and no regexps).
Unable to sync a single file using rsync on linux
You are missing the -e
flag, that will instruct rsync
to use ssh
with the ssh private key.
Change "-avz"
to "-avze"
and you are done.
rsync copy over only certain types of files using include option
I think --include
is used to include a subset of files that are otherwise excluded by --exclude
, rather than including only those files.
In other words: you have to think about include meaning don't exclude.
Try instead:
rsync -zarv --include "*/" --exclude="*" --include="*.sh" "$from" "$to"
For rsync version 3.0.6 or higher, the order needs to be modified as follows (see comments):
rsync -zarv --include="*/" --include="*.sh" --exclude="*" "$from" "$to"
Adding the -m
flag will avoid creating empty directory structures in the destination. Tested in version 3.1.2.
So if we only want *.sh files we have to exclude all files --exclude="*"
, include all directories --include="*/"
and include all *.sh files --include="*.sh"
.
You can find some good examples in the section Include/Exclude Pattern Rules of the man page
Using rsync to delete a single file
Try this:
rsync -rv --delete --include=foo.txt '--exclude=*' /home/user/ user@remote:/home/user/
(highly recommend running with --dry-run
first to test it) Although it seems like it would be easier to use ssh...
ssh user@remote "rm /home/user/foo.txt"
rsync include specific files and exclude directory
You need to put --exclude='build'
before --include '*/'
. Both of these rules could apply to the "build" directory, and whichever is given first takes precedence, so to get the --exclude
rule to override the --include
rule, you need to list it first.
From the rsync
man page, in the FILTER RULES section (with my emphasis):
As the list of files/directories to transfer is built, rsync checks
each name to be transferred against the list of include/exclude
patterns in turn, and the first matching pattern is acted on: if it is
an exclude pattern, then that file is skipped; if it is an include
pattern then that filename is not skipped; if no matching pattern is
found, then the filename is not skipped.
Related Topics
Find the Files Existing in One Directory But Not in the Other
Rename Files and Directories (Add Prefix)
Unzip All Files in a Directory
Abuse Curl to Communicate with Redis
What's the Difference Between "Env" and "Set" (On MAC Os X or Linux)
How to Force 'Cp' to Overwrite Directory Instead of Creating Another One Inside
Error: Can't Open Display: (Null) When Using Xclip to Copy Ssh Public Key
Recursively Find Files with a Specific Extension
What Is Preemption/What Is a Preemtible Kernel? What Is It Good For
Need Explanations for Linux Bash Builtin Exec Command Behavior
Create a Dedicated Folder for Every Zip Files in a Directory and Extract Zip Files
Bash History Without Line Numbers
Bash, Linux, Need to Remove Lines from One File Based on Matching Content from Another File
How to Force Linking with a Static Library When a Shared Library of Same Name Is Present