Software⏱️ 3 min read📅 2026-06-11

How to Fix: rsync: how to copy a file that might not exist without an error

How to fix rsync error when target file does not exist.

Quick Answer: Use the -n option with rsync to prevent it from overwriting existing files, or pipe the output to /dev/null and check the return code of rsync.

The error occurs when the target file does not exist, causing rsync to exit with error code 23. This issue affects users who rely on rsync for syncing files and want to handle non-existent targets gracefully.

This error can be frustrating because it prevents the script from proceeding with the intended action. However, by implementing a workaround, you can modify your Makefile to output a specific message when the target file does not exist.

⚠️ Common Causes

  • The rsync command exits with error code 23 when the target file does not exist because of its default behavior. This is a common issue in scripting scenarios where files may not always be available.
  • Another possible cause could be related to the way the Makefile is being executed, but this would typically result in different error messages or behaviors.

🚀 How to Resolve This Issue

Using rsync's --ignore-existing option

  1. Step 1: Open your Makefile and add the following flag to the rsync command: `--ignore-existing`. This will instruct rsync to skip the target file if it does not exist.
  2. Step 2: Update the Makefile code as follows: rsync $(bib_dir) bib --ignore-existing 2>/dev/null echo 'Hi'
  3. Step 3: This method will ensure that your script outputs the desired message even when the target file does not exist.

Piping output to a variable

  1. Step 1: Alternatively, you can use a variable to capture the output of rsync and check its status before echoing the message.
  2. Step 2: Modify your Makefile code as follows: rsync_status=$(rsync $(bib_dir) bib 2>/dev/null) if [ $? -eq 0 ]; then echo 'Hi' i

✨ Wrapping Up

By implementing one of these methods, you can modify your Makefile to output a specific message when the target file does not exist. This will ensure that your script behaves gracefully in such scenarios and provides a more robust solution for handling non-existent targets.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions