I'm trying to use the --delete
option in rsync
to delete files in the target directory which isn't present in the original directory
Here is the command I'm using:
rsync -avz --ignore-existing --recursive --delete /var/www/* [email protected]:/var/www
So my question is, how can I delete all files in target directory which aren't present in the original directory?
Use this command:
You do not need a "*" and should not use it too.
To exclude/include files or directories, you should use this parameters:
Your command was not working because when you were using
/var/www/*
as the source, your shell is performing globbing on it i.e. shell is expanding*
to all files in that directory and the copying the files one by one, so here individual files have become the sources rather than the parent directory.So, if you use
/var/www/*
, then you don't need--recursive
option as*
will causes the files to be copied (along with any directories with their contents), not the parent directory that contains the files. Because of the same reason--delete
is not working, as--delete
will remove files from destination directory that are not in the source directory, but you are copying files so its not removing files (expectedly).This will make you more clear:
As you can see, i have used the source as
/foo/*
hence thersync
command being executed iswith
*
making shell to expand it and make all files individually as source arguments, not the parent directory as a whole (and you also don't need--recursive
in this case).So, if you want to make
--delete
work, run it as: