How to spawn a process with shell wildcard?

For example, if I want to use a shell command

cp a_dir/* b_dir/

I cannot simply use

std::command::Command::new("cp").args(&["a_dir/*", "b_dir/"]).spwan()?;

since the expansion of * wildcard is in the shell but not in the cp command.

You could "shell out":

Command::new("bash").arg("-c")
    .arg("cp a_dir/* b_dir/")
    .spawn()

That is, launch a shell process that just runs a single command and then exits. I'm sure you can also find a crate that implements shell glob expansion, or use other pure-Rust techniques to do the same basic computation, but shelling out is quick and it gets the job done.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.