0

I have a directory with many files in it. The only ones I care about are those with the extension .jar. However, there are other files in the directory as well.

I have a source directory with only .jar files. I want to achieve the equivalent of:

rm destdir/*.jar
cp sourcedir/*.jar destdir

However, I want to do this without actually removing the files that are already there, if they are the right files already.


In other words, I want it to be possible to run a single command that will:

  1. For any .jar files present in sourcedir but not in destdir, copy them over.
  2. For any .jar files present in both sourcedir and destdir, ensure that the copy in destdir matches the copy in sourcedir and overwrite it if it doesn't.
  3. For any .jar files present in destdir but not in sourcedir, delete the file.
  4. For any other files present in destdir (without a .jar extension), ignore them—do not delete them or change them.

It seems that this should be possible with rsync. How can I do this?

2

You can use:

rync -av --include='*.jar' --exclude'*' --delete sourcedir/ destdir/

The -a option is archive mode, it preserves things like links and timestamps (check the man page for a full explanation). The -v is for verbosity, remove if you don't care about logs.

That should handle your first three option. The --delete option will take care of your fourth requirement while ignoring excluded files.

If you wish to include directories that contain .jar files you can use

rync -av --include='*.jar' --include='*/' --exclude'*' --delete sourcedir/ destdir/

Based on the answer located here, if you have further questions it should help you: Rsync filter: copying one pattern only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.