#Deno.Command arg with an asterisk?

5 messages · Page 1 of 1 (latest)

noble trellis
#

I am trying to execute a shell command from Deno, where one of the command args contains an asterisk. Example:

const output = new Deno.Command("cp", { args: ["source/*", "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))

It yields:

cp: cannot stat 'source/*': No such file or directory

Q: How can I pass an asterisk to one of the Deno.Command args?

dawn bison
#

The asterisk is a shell-only concept, and not something cp can really do. This is a fundamental "limitation" of the fork-exec call happening here. You'd need to manually enumerate the files yourself:

const source = Deno.readDirSync("source/*").map((entry)=>`source/${entry.name}`)
const output = new Deno.Command("cp", { args: [...source, "destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
#

alternatively, if you really want to use shell constructs, you can simply call a shell instead

#
const output = new Deno.Command("sh", { args: ["-c", "cp source/* destination"] }).outputSync()
console.error(new TextDecoder().decode(output.stderr))
noble trellis
#

❤️