Search code examples
google-zx

google/zx tar Gives "No such file or directory"


I am using google/zx to create a simple backup script using tar. The code I'm using to generate the tar command looks like this:

  const tarCommandString = `tar czf "${absoluteBackupFile}" -P "${folder}"`;
  const tarCommand = await $`${tarCommandString}`.exitCode;

I keep getting the following error /usr/bin/bash: line 1: tar czf "/home/syssu/backup/home-syssu-testToBackup/1692150304.tar.gz" -P "/home/syssu/testToBackup": No such file or directory.

However I can literally take that command tar czf "/home/syssu/backup/home-syssu-testToBackup/1692150304.tar.gz" -P "/home/syssu/testToBackup" and manually run it without any issue.


Solution

  • The documentation says:

    Everything passed through ${...} will be automatically escaped and quoted.

    In other words

    $`${tarCommandString}`
    

    here entire tarCommandString is treated as a single command.

    You should

    await $`tar czf "${absoluteBackupFile}" -P "${folder}"`
    

    instead.